I've read documentation and I think that my code should be right, but still there is no line between the points in the output. What is wrong?
The x'axis is discrete and y'axis is continuous.
My code
point.sqrmPrice <- ggplot(overview.df, aes(x = areaSize, y = sqrmPrice)) +
geom_line() +
geom_point() +
scale_y_continuous(breaks = c(seq(min(overview.df$sqrmPrice), max(overview.df$sqrmPrice), by = 10000) )) +
theme_bw()
The answer is that the X variable is a factor type.
Connecting Paired Points with lines using geom_line() In ggplot2 we can add lines connecting two data points using geom_line() function and specifying which data points to connect inside aes() using group argument. Now we get a scatter plot connecting paired data with lines.
The underlying issue here is a duplicate of this stack post.
Here's a reproducible example showing what @SN248 meant about adding group to the code
ggplot(iris, aes(x = factor(Sepal.Length), y = Sepal.Width)) +
geom_line(aes(group=1)) + geom_point() + theme_bw()
You are not getting a line because areaSize
is a factor. Convert to numeric with
overview.df$areaSize <- as.numeric(as.character(overview.df$areaSize))
and then make the plot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With