对于这个数据帧("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:每个组只包含一个观测值。你需要 调整群体审美?< / p >
虽然我想要折线图,但图表显示为散点图。我尝试用geom_line(aes(group = year))
替换geom_line()
,但这行不通。
在回答中,我被告知要将年份转换为一个因子变量。我做了,但问题仍然存在。这是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")