【问题标题】:Error: geom_path: Each group consists of only one observation [duplicate]错误:geom_path:每个组仅包含一个观察值[重复]
【发布时间】:2021-11-11 17:26:00
【问题描述】:

我在 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()

但是当我运行它时,R 会向我报告一条错误消息:

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

为什么?我该如何解决这个问题?

【问题讨论】:

  • 您的意思是 geom_col() 而不是 geom_line() 或 geom_point。根据数据,x 为“月”时只有一个点
  • 我每个月都需要一条线。并且要与点相连。这可能吗?

标签: r date ggplot2


【解决方案1】:
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)

reprex package (v2.0.1) 于 2021-09-16 创建

【讨论】:

  • nice.very good。但我想从一月份开始
  • 我相应地更新了我的答案。请下次在您的问题中说明这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 2015-01-20
  • 2021-04-08
  • 2021-08-31
  • 2023-03-23
相关资源
最近更新 更多