使用此数据框("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(*, …Run Code Online (Sandbox Code Playgroud)