ggplot2折线图给出了"geom_path:每组只包含一个观察.你需要调整群体美感吗?"

meg*_*ger 144 r ggplot2

使用此数据框("df"):

year pollution
1 1999 346.82000
2 2002 134.30882
3 2005 130.43038
4 2008  88.27546
Run Code Online (Sandbox Code Playgroud)

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

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

我得到的错误是:

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")
Run Code Online (Sandbox Code Playgroud)

小智 291

您只需要添加group = 1到ggplot或geom_line aes()中.

对于折线图,必须对数据点进行分组,以便知道要连接的点.在这种情况下,它很简单 - 所有点都应该连接,所以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")
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,分组必须使用“group”参数来完成。仅按“颜色”进行分组是不够的。我刚刚遇到了这个麻烦,希望这可以帮助遇到同样问题的人 (5认同)
  • 如果省略“geom_line()”,是否有任何原因无法*假设*“group = 1”? (4认同)
  • 这个答案还有效吗?在美学中添加 group=1 似乎不再起作用了。 (2认同)
  • @Giacomo——为我工作,在 Mac 上运行 3.6.2。收到可怕的警告,但添加 group=1 解决了问题。ggplot(lakemeta,映射= aes(x =湖,y =面积,组= 1))+ geom_line(大小= 2,颜色=“蓝色”) (2认同)

age*_*nis 20

您得到此错误,因为您的一个变量实际上是一个因子变量.执行

str(df) 
Run Code Online (Sandbox Code Playgroud)

检查这个.然后执行此双变量更改以保留年份数而不是转换为"1,2,3,4"级别数:

df$year <- as.numeric(as.character(df$year))
Run Code Online (Sandbox Code Playgroud)

编辑:看来你的data.frame有一个类"array"的变量,它可能会导致pb.然后尝试:

df <- data.frame(apply(df, 2, unclass))
Run Code Online (Sandbox Code Playgroud)

和plto

  • 这对我来说是一个方便的答案,因为它解决了根本问题 (3认同)
  • 我进行了转换,但仍然无法正常工作。 (2认同)

Xin*_*Niu 9

我对数据框有类似的问题:

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
Run Code Online (Sandbox Code Playgroud)

我认为 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
Run Code Online (Sandbox Code Playgroud)

然后就可以了。