4

I'm iterating through multiple data sets to produce line plots for each set. How can I prevent ggplot from complaining when I use geom_line over one point?

Take, for example, the following data:

mydata = data.frame(
  x = c(1, 2),
  y = c(2, 2),
  group = as.factor(c("foo", "foo"))
)

Creating line graph looks and works just fine because there are two points in the line:

ggplot(mydata, aes(x = x, y = y)) + 
  geom_point() + 
  geom_line(aes(group = group))

However, plotting only the fist row give the message:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

ggplot(mydata[1,], aes(x = x, y = y)) + 
      geom_point() + 
      geom_line(aes(group = group))

Some of my figures will only have one point and the messages cause hangups in the greater script that produces these figures. I know the plots still work, so my concern is avoiding the message. I'd also like to avoid using suppressWarnings() if possible in case another legitimate and unexpected issue arises.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Nancy
  • 3,989
  • 5
  • 31
  • 49
  • 1
    It's not an error or a warning (those will self-identify by saying `Error:...` or `Warning:...`), it's just a message. Are you sure the messages are causing hangups? That would be surprising. I tried `suppressMessages()` but it didn't seem to help, which is surprising. – Gregor Thomas May 24 '16 at 19:43
  • You can see that it is indeed a `message()` generated on [line 156 here](https://github.com/hadley/ggplot2/blob/59c503b8e1cacf1f9264d1e233b7a305916905d6/R/geom-path.r) and the utility on [line 264 here](https://github.com/hadley/ggplot2/blob/59c503b8e1cacf1f9264d1e233b7a305916905d6/R/utilities.r). – Gregor Thomas May 24 '16 at 19:47
  • @Gregor Hangups in the sense that when I put my big function that dispatches these individual plots into a tryCatch() I still see the message which a) obscures that the plot ran successfully and b) depending on the context can make tryCatch() skip that iteration altogether. – Nancy May 24 '16 at 20:27
  • @Gregor also replace "error" with message in question body. Though, the "message" shows up in tryCatch() as an error, but that might be outside the scope of this question. – Nancy May 24 '16 at 20:28

3 Answers3

4

Per an answer to this question: suppressMessages(ggplot()) fails because you have to wrap it around a print() call of the ggplot object--not the ggplot object itself. This is because the warning/message only occurs when the object is drawn.

So, to view your plot without a warning message run:

p <- ggplot(mydata[1,], aes(x = x, y = y)) + 
  geom_point() + 
  geom_line(aes(group = group))

suppressMessages(print(p))
John J.
  • 1,450
  • 1
  • 13
  • 28
0

I think the following if-else solution should resolve the problem:

if (nrow(mydata) > 1) {
    ggplot(mydata, aes(x = x, y = y)) + 
        geom_point() + 
        geom_line(aes(group = group))
} else {
    ggplot(mydata, aes(x = x, y = y)) + 
        geom_point()
}
  • This won't work because the problem is not that mydata only has one row, but one of the groups within mydata only having one row. So you still want the `geom_line`, just not for all of the groups. I think the solution of @John-J is the best. – Peter Ellis Jan 21 '19 at 03:03
-1

On the community.RStudio.com, John Mackintosh suggests a solution which worked for me:

Freely quoting:

Rather than suppress warnings, change the plot layers slightly.

  1. Facet wrap to create empty plot

  2. Add geom_point for entire data frame

  3. Subset the dataframe by creating a vector of groups with more than one data point, and filtering the original data for those groups. Only plot lines for this subset.

Details and example code in the followup of the link above.

vinnief
  • 742
  • 10
  • 13
  • This method is better than suppressing warnings as you can then see other warnings that might appear. – vinnief Jan 30 '23 at 20:22