3
votes

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.

1
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 fatalCalum You
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
@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
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
@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

1 Answers

2
votes

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.