3

I'm trying to create multiple line graphs using ggplot, geom_line and facet_wrap. The chart is created and displayed successfully, however the following message is also displayed along with the chart:

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

enter image description here

I've already tried restarting my session, restarting R, searching for answers by Googling, searching here on Stack Overflow (including the following question: ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?") and can't find anything that helps.

Adding:

group=1

...doesn't eliminate the warning message.

The version of RStudio I'm using is: Version 1.1.463.

The code is in an RMarkdown file that I'm knitting to create an HTML document.

The libraries I'm using in this Rmarkdown file are:

library(tidyverse)
library(operators)
library(magrittr)
library(dplyr)
library(knitr)
library(sf)
library(usmap)
library(waffle)

The structure of the data set that I'm using in the chart is:

'data.frame':   75 obs. of  3 variables:
 $ Year         : int  1998 1993 1998 1999 2005 2007 2014 2018 1989 1991 ...
 $ Age_Bracket  : chr  "1-12" "13-19" "13-19" "13-19" ...
 $ Num_Shootings: num  1 1 1 1 1 2 1 2 1 1 ...

The code that generates the warning message is:

# Create a faceted line graph showing the number of mass shootings per year for each age bracket

faceted_line_graph_year_age <- ggplot(data=shootings_per_year_age, aes(x=Year, y = Num_Shootings, colour = Age_Bracket, group=1)) + 
  geom_line()+
  geom_point()+
  geom_smooth(method = "lm", colour = "#666666", linetype = 2, size = 0.5 )+
  facet_wrap(~Age_Bracket)


# Display the faceted line graph

faceted_line_graph_year_age 

The message I see is:

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

I'm expecting the chart to be displayed without a warning message. What I'm getting is the chart displayed with a warning message.

code_to_joy
  • 569
  • 1
  • 9
  • 27
  • 2
    I *think* the error is caused by `geom_line` in the first group, because it cannot draw a line with only a single point there. This is also a warning/message (not sure which exactly here) but not an error, which would not have let you produce the graph at all. Warnings/messages are mostly to let you know you might be doing something wrong, not fatal – Calum You Apr 04 '19 at 17:20
  • Thanks Calam. I removed the first age category and now it displays the charts without the message. Thank you! I've edited my description to replace error with warning message. I don't suppose you know if there's a way I could tell R to not display the message if I wanted to leave the first age category in? – code_to_joy Apr 04 '19 at 17:24
  • 1
    @Teaching_myself_how_to_code The message (which is technically neither an error nor a warning, but a diagnostic message) is hard-coded into the underlying plotting function over at `GeomPath$draw_panel`, & I don't see anything there that could suppress the message. – Z.Lin Apr 05 '19 at 06:30
  • Run your code inside `suppressWarnings(...)` if you don't want to see warnings in a particular chunk of code. Or set `options(warn=...)` to -1 - see `help(options)` to see what the other values do. – Spacedman Apr 07 '19 at 17:24
  • 1
    @CalumYou It is definitely caused by `geom_line`. If this geom can be removed without compromising the intent of the plot that is the simplest way to go. – DaveRGP Nov 07 '19 at 10:48
  • @Spacedman I have not had success with those options as the print out isn't a warning, it's created from this function: https://github.com/tidyverse/ggplot2/blob/48580251e5cb006863edf102376815c379ae1fcf/R/utilities.r#L305-L309 which is called here: https://github.com/tidyverse/ggplot2/blob/10fa0014d6be81a7e641b8c2c3b48e1df1b7e2f5/R/geom-path.r#L153-L156 which itself calls `message` from base. Therefore `suppressMessage()` _should_ correct for this, but doesn't for reasons I don't understand. I can only guess that somewhere between `geom_path` + `message_wrap` it changes type. – DaveRGP Nov 07 '19 at 10:56
  • @Teaching_myself_how_to_code I think there is a 'solution' here: https://stackoverflow.com/a/48197537/2902740 Note this may not be suitable as it requires an explicit print call to an assigned plot object, so YMMV. – DaveRGP Nov 07 '19 at 10:58
  • If you are in knitr, you can use `message=FALSE` for the chunk. – DaveRGP Nov 07 '19 at 11:07

1 Answers1

2

Thanks to user calum-you for the answer:

I think the error is caused by geom_line in the first group, because it cannot draw a line with only a single point there. This is also a warning/message (not sure which exactly here) but not an error, which would not have let you produce the graph at all. Warnings/messages are mostly to let you know you might be doing something wrong, not fatal

I removed the plot representing the fist age category (as there was only one data value for that plot) and the issue was resolved.

code_to_joy
  • 569
  • 1
  • 9
  • 27