ggplot2 折线图给出“geom_path:每组仅包含一个观测值。你需要调整群体审美吗?

ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"

提问人:megashigger 提问时间:11/23/2014 最后编辑:Gregor Thomasmegashigger 更新时间:2/20/2022 访问量:413398

问:

使用此数据框 (“df”):

year pollution
1 1999 346.82000
2 2002 134.30882
3 2005 130.43038
4 2008  88.27546

我尝试创建这样的折线图:

  plot5 <- ggplot(df, aes(year, pollution)) +
           geom_point() +
           geom_line() +
           labs(x = "Year", y = "Particulate matter emissions (tons)", title = "Motor vehicle emissions in Baltimore")

我得到的错误是:

geom_path:每组仅包含一个观测值。你需要吗 调整团体审美?

即使我想要折线图,图表也显示为散点图。我试图用替换,但没有用。geom_line()geom_line(aes(group = year))

在回答中,我被告知将年份转换为因子变量。我做到了,问题仍然存在。这是 和 的输出:str(df)dput(df)

'data.frame':   4 obs. of  2 variables:
 $ year     : num  1 2 3 4
 $ pollution: num [1:4(1d)] 346.8 134.3 130.4 88.3
  ..- attr(*, "dimnames")=List of 1
  .. ..$ : chr  "1999" "2002" "2005" "2008"

structure(list(year = c(1, 2, 3, 4), pollution = structure(c(346.82, 
134.308821199349, 130.430379885892, 88.275457392443), .Dim = 4L, .Dimnames = list(
    c("1999", "2002", "2005", "2008")))), .Names = c("year", 
"pollution"), row.names = c(NA, -4L), class = "data.frame")
GGPLOT2 R-常见问题

评论

1赞 G. Grothendieck 11/23/2014
当我运行它时,它不会出现错误。这很可能不是你想象的那样。请以可重现的形式陈述您的问题,即显示 的输出。dfdput(df)
0赞 erc 11/23/2014
可能是你的变量是因子,那么你需要将它们转换为数字
0赞 megashigger 11/23/2014
@G.格罗滕迪克,我发布了你说的话。我也转换为数字,但仍然有问题。
1赞 Mario Becerra 4/25/2018
你真的应该以可重复的形式陈述问题。如果我们不能重现错误,就很难为您提供帮助。
0赞 AdIan 3/29/2021
是否可以按“污染”的降序排列线点?

答:

49赞 agenis 11/23/2014 #1

您收到此错误是因为其中一个变量实际上是一个因子变量 .执行

str(df) 

来检查这一点。 然后进行此双变量更改以保留年份数字,而不是转换为“1,2,3,4”级别的数字:

df$year <- as.numeric(as.character(df$year))

编辑:似乎您的 data.frame 有一个类为“array”的变量,这可能会导致 pb。 然后尝试:

df <- data.frame(apply(df, 2, unclass))

又阴谋了?

评论

4赞 Medhat 11/17/2017
这对我来说是一个方便的答案,因为它从根源上解决了问题
1赞 G. Grothendieck 11/23/2014 #2

在新会话中启动 R 并将其粘贴到:

library(ggplot2)

df <- structure(list(year = c(1, 2, 3, 4), pollution = structure(c(346.82, 
134.308821199349, 130.430379885892, 88.275457392443), .Dim = 4L, .Dimnames = list(
    c("1999", "2002", "2005", "2008")))), .Names = c("year", 
"pollution"), row.names = c(NA, -4L), class = "data.frame")

df[] <- lapply(df, as.numeric) # make all columns numeric

ggplot(df, aes(year, pollution)) +
           geom_point() +
           geom_line() +
           labs(x = "Year", 
                y = "Particulate matter emissions (tons)", 
                title = "Motor vehicle emissions in Baltimore")

评论

0赞 G. Grothendieck 11/23/2014
在新会话中启动 R,并将我帖子中的代码粘贴到其中。
0赞 Hoang Le 7/27/2017
你有没有弄清楚这个问题。我和你有同样的问题,每个 x 值只有一个值。等待您的回复。谢谢。
1赞 Rafs 11/20/2020
您能解释一下为什么将所有内容转换为数字可以解决问题吗?我的有序因子变量是字符变量,所以我不能用数字来代替它。
0赞 G. Grothendieck 11/20/2020
pollution是一个一维数组,而不是一个普通向量。看str(df)
523赞 Mario Barbé 3/13/2015 #3

你只需要添加到 ggplot 或geom_line aes()。group = 1

对于折线图,必须对数据点进行分组,以便知道要连接哪些点。在这种情况下,它很简单——所有点都应该连接起来,所以 group=1。当使用更多变量并绘制多条线时,线的分组通常按变量完成。

参考资料:R 说明书,章节:图形 Bar_and_line_graphs_(ggplot2)、折线图。

试试这个:

plot5 <- ggplot(df, aes(year, pollution, group = 1)) +
         geom_point() +
         geom_line() +
         labs(x = "Year", y = "Particulate matter emissions (tons)", 
              title = "Motor vehicle emissions in Baltimore")

评论

8赞 tjebo 8/9/2019
需要注意的是,分组必须用参数来完成。仅按分组是不够的。我只是遇到了这个麻烦,希望这可以帮助遇到同样问题的人groupcolor
8赞 Giacomo 4/22/2020
这个答案仍然有效吗?在美学中添加 group=1 似乎不再起作用了。
2赞 Jenn D. 5/25/2020
@Giacomo -- 对我有用,在 Mac 上的 3.6.2 上。 收到可怕的警告,但添加 group=1 解决了问题。ggplot(lakemeta, mapping=aes(x=Lake, y=Area, group=1)) + geom_line(size=2, color=“blue”)
0赞 AdIan 3/29/2021
是否可以按“污染”的降序排列该点?
4赞 stevec 7/17/2022
如果省略了,有什么理由不能假设吗?geom_line()group = 1
9赞 Xin Niu 3/6/2019 #4

我在数据框中遇到了类似的问题:

group time weight.loss
1 Control  wl1    4.500000
2    Diet  wl1    5.333333
3  DietEx  wl1    6.200000
4 Control  wl2    3.333333
5    Diet  wl2    3.916667
6  DietEx  wl2    6.100000
7 Control  wl3    2.083333
8    Diet  wl3    2.250000
9  DietEx  wl3    2.200000

我认为 x 轴的变量应该是数字,这样geom_line知道如何连接点来画线。

在我将第二列更改为数字之后:

 group time weight.loss
1 Control    1    4.500000
2    Diet    1    5.333333
3  DietEx    1    6.200000
4 Control    2    3.333333
5    Diet    2    3.916667
6  DietEx    2    6.100000
7 Control    3    2.083333
8    Diet    3    2.250000
9  DietEx    3    2.200000

然后它起作用了。

1赞 Areeha 5/23/2020 #5

我得到了类似的提示。这是因为我以某个百分比指定了 x 轴(例如:10%A、20%B,....)。 因此,另一种方法是将这些值相乘并以最简单的形式编写它们。

1赞 qwr 10/24/2020 #6

我发现,如果绘制的大部分数据都超出了轴限制,也会发生这种情况。在这种情况下,请相应地调整轴缩放。