Home > Enterprise >  geom_line() error geom_path: Each group consists of only one observation. Do you need to adjust the
geom_line() error geom_path: Each group consists of only one observation. Do you need to adjust the

Time:07-22

I have a DF that looks something like this

df<-structure(list(treatment = c(0, 0, 1, 1), year = c(2002, 2006, 
2002, 2006), avg = c(1.41427998341858, 1.48990147417549, 1.37854380583991, 
1.44826789361515)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -4L), groups = structure(list(
    treatment = c(0, 1), .rows = structure(list(1:2, 3:4), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))
df
 treatment  year   avg
      <dbl> <dbl> <dbl>
1         0  2002  1.41
2         0  2006  1.49
3         1  2002  1.38
4         1  2006  1.45

I would like to create a scatter plot of year by average, with line of different colours for treatment.

I have tried to use this approach but it gives me an error and only shows dots.
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

ggplot(df,aes(x=as.character(year),y=avg,colour=as.character(treatment))) 
  geom_point() 
  geom_line()

I saw a previous post that suggests using group=1, but unfortunately, it does not work. here is the plot produced by the following command. I would like lines to be drawn by the treatment group.

ggplot(df,aes(x=as.character(year),y=avg,colour=as.character(treatment),group=1)) 
  geom_point() 
  geom_line()

enter image description here

CodePudding user response:

You can add in the same colour argument as a grouping argument to get two distinct lines. As you just want to use it to force the year and treatment variable as discrete (non-continuous) measures, transforming them to character or factor doesn't really matter (will work for both).

ggplot(df, aes(as.character(year), avg, 
              group=as.character(treatment), 
              colour=as.character(treatment)))   
  geom_line()   
  geom_point()

enter image description here

#same example with factor instead of character

ggplot(df, aes(factor(year), avg, 
              group=factor(treatment), 
              colour=factor(treatment)))   
  geom_line()   
  geom_point()

enter image description here

  • Related