1

I'm always having this problem where I need to plot two lines on a chart, but there are different number of rows in my data. I keep getting this error and I wish I could solve it once and for all:

Error: (converted from warning) Removed 5 row(s) containing missing values (geom_path).

Here is some sample data (I didn't manually add the NAs):

datamre <- structure(list(xR = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, NA, NA, NA, NA), received = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1666667, 0.1666667, 0.1666667, 
0.1666667, 0.1666667, 0.1666667, 0.1666667, 0.1666667, 0.1666667, 
0.1666667, 0.1666667, 0.1666667, 0.1666667, 0.1666667, 0.1666667, 
0.1666667, 0.1666667, 0.1666667, 0.1666667, 0.1666667, NA, NA, 
NA, NA), xD = 0:34, demand = c(0, 0.08333333, 0.08333333, 0.08333333, 
0.08333333, 0.08333333, 0.08333333, 0.08333333, 0.16666667, 0.25, 
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
0.25, 0.25, 0.25, 0.25, 0.33333333, 0.33333333, 0.33333333, 0.33333333, 
0.33333333, 0.33333333, 0.33333333, 0.41666667, 0.41666667, 0.41666667
)), row.names = c(NA, 35L), class = "data.frame")

And the simple code I'm using:

df <- data.frame(datamre)
ggplot(df) +
   geom_line(aes(xR,received)) +
   geom_line(aes(xD,demand)) 
Angus
  • 345
  • 1
  • 11
  • 1
    The warning is not an error. It's just reminding you that 5 data points were not plotted because there are `NA`s present in the `x` or `y` aesthetic. This is not something to be concerned about *per se*. Perhaps the `NA` were introduced when you cast the data into long form? – Ian Campbell Apr 24 '20 at 13:56
  • But it's listed as an error and nothing is plotted. – Angus Apr 24 '20 at 13:57
  • Interesting. I get a plot. See this [image](https://i.stack.imgur.com/8i2aO.png). – Ian Campbell Apr 24 '20 at 13:58
  • What am I doing wrong because I get this all the time? – Angus Apr 24 '20 at 13:59
  • 2
    This question may help: https://stackoverflow.com/questions/46605672/error-converted-from-warning-ignoring-unknown-aesthetics – Dave2e Apr 24 '20 at 14:08

1 Answers1

1

Try this:

ggplot(df[complete.cases(df),]) +
  geom_line(aes(xR,received)) +
  geom_line(aes(xD,demand))
Mohanasundaram
  • 2,551
  • 1
  • 6
  • 17
  • That worked - thanks, but I still don't understand why Ian Campbell has a solution without adding "complete.cases" (I assume). – Angus Apr 24 '20 at 14:06
  • 1
    He may be having a valid reason. There a difference between both cases. If we plot without adding complete cases, received and demand lines will be drawn with available data individually in each variable. If you compare the plot you have got with the one produced my Ian Campbell, you could see the difference. – Mohanasundaram Apr 24 '20 at 14:10