Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No line in plot chart despite + geom_line()

Tags:

r

ggplot2

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()

enter image description here

like image 794
uncool Avatar asked Aug 04 '15 20:08

uncool


People also ask

Why does my Ggplot not have a line?

The answer is that the X variable is a factor type.

How do I connect dots in ggplot2?

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.


2 Answers

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()
like image 118
tim Avatar answered Oct 13 '22 02:10

tim


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.

like image 38
davechilders Avatar answered Oct 13 '22 00:10

davechilders