ggplot2折线图给出了“geom_path:每个组只包含一个观察值。你需要调整团体审美吗?“ [英] ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"
问题描述
有了这个数据框(df):
年份污染
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()+
实验室(x =Year,y =颗粒物排放量(吨),title =巴尔的摩的汽车排放量)
我得到的错误是:
geom_path:每个组只包含一个观察值。您是否需要
调整团体美学?
即使我想要一个折线图。我尝试用 geom_line(aes(group = year))
替换 geom_line()
,但那不起作用。
在一个答案中,我被告知将年份换算成一个因子变量。我做了,问题依然存在。这是 str(df)
和 dput(df)
的输出:
'data.frame':4 obs。 2个变量:
$ year:num 1 2 3 4
$污染:num [1:4(1d)] 346.8 134.3 130.4 88.3
..- attr(*,dimnames )= 1
.. .. $:$ chr1999200220052008
结构(列表(年份= c(1,2,3, 4),污染=结构(c(346.82,
134.308821199349,130.430379885892,88.275457392443),.Dim = 4L,.Dimnames = list(
c(1999,2002,2005, )),.Names = c(year,
pollution),row.names = c(NA,-4L),class =data.frame)
您只需添加 对于折线图,必须对数据点进行分组,以便它知道指向连接。在这种情况下,它很简单 - 所有点都应该连接,所以group = 1。当使用更多变量并绘制多行时,行的分组通常由变量完成。 参考: Cookbook for R,Chapter:Graphs Bar_and_line_graphs_(ggplot2),Line graphs。 试试这个: With this data frame ("df"): I try to create a line chart like this: The error I get is: geom_path: Each group consist of only one observation. Do you need to
adjust the group aesthetic? The chart appears as a scatter plot even though I want a line chart. I tried to replace In an answer I was told to convert year to a factor variable. I did and the problem persists. This is the output of
You only have to add For line graphs, the data points must be grouped so that it knows which points to connect. In this case, it is simple -- all points should be connected, so group=1. When more variables are used and multiple lines are drawn, the grouping for lines is usually done by variable. Reference: Cookbook for R, Chapter: Graphs Bar_and_line_graphs_(ggplot2), Line graphs. Try this:
这篇关于ggplot2折线图给出了“geom_path:每个组只包含一个观察值。你需要调整团体审美吗?“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! group = 1
放入ggplot或geom_line aes()。
lockquote
plot5 < - ggplot(df,aes(year ,污染,组= 1))+
geom_point()+
geom_line()+
实验室(x =Year,y =颗粒物排放量(吨),
title =在巴尔的摩的汽车排放量)
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_line()
with geom_line(aes(group = year))
but that didn't work. str(df)
and 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")
group = 1
into the ggplot or geom_line aes().
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")