Home > other >  Error: geom_path: Each group consists of only one observation
Error: geom_path: Each group consists of only one observation

Time:09-17

I have the following dataset in R :

month =c("January","February","March","April","May","June",
                 "July",    "August" ,"September","October","November","December")
value = seq(1:12)
d = data.frame(month,value);d
ggplot(data = d ,aes(x = month,y=value)) geom_line()

But when I run it, R reports me an error message :

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

Why? How can I fix this?

CodePudding user response:

library(tidyverse)

months <- c(
  "January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
)


value <- seq(1:12)
d <-
  data.frame(months, value) %>%
  # prevent lexicographical sorting
  mutate(months = months %>% factor(levels = months))
d
#>       months value
#> 1    January     1
#> 2   February     2
#> 3      March     3
#> 4      April     4
#> 5        May     5
#> 6       June     6
#> 7       July     7
#> 8     August     8
#> 9  September     9
#> 10   October    10
#> 11  November    11
#> 12  December    12
ggplot(data = d, aes(x = months, y = value, group = 1))  
  geom_line()  
  scale_x_discrete(labels = months, breaks = months)

Created on 2021-09-16 by the reprex package (v2.0.1)

  • Related