No line in geom_line plot (missing values in some categories)

Hi,

I'm trying to generate an overlaying plot including bar plot and line plot.

The data for line plot, however, has some missing values in some categories. I was able to create bar plot with geom_point but no line was plotted when using geom_line - I also added group = 1 in aes but no luck.

a <- data.frame(score = c("2.51-3.00", "3.01-3.50", "3.51-4.00", "4.01-4.50", "4.51-5.00"),
                count = c(2, 1, 4, 6, 2),
                ch = c(1, NA, 3, NA, 4))

And here is the code I tried:

ggplot(a) + 
  geom_bar(aes(x=score, y=count), stat = "identity") +
  geom_point(aes(x = score, y = ch), color="red") +
  geom_line(aes(x = score, y = ch, group = 1), stat = "identity", color="red")

Any comments and suggestions on how to add a line to the plot are appreciated!
Thank you!

A quick solution is to remove the rows that have NA values in the ch column.

ggplot(a) + 
  geom_bar(aes(x=score, y=count), stat = "identity") +
  geom_point(aes(x = score, y = ch), color="red") +
  geom_line(aes(x = score, y = ch, group = 1), data = na.omit(a), stat = "identity", color="red")

This worked perfectly! Thank you so much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.