r - ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?" -
with data frame ("df"):
year pollution 1 1999 346.82000 2 2002 134.30882 3 2005 130.43038 4 2008 88.27546 i try create line chart this:
plot5 <- ggplot(df, aes(year, pollution)) + geom_point() + geom_line() + labs(x = "year", y = "particulate matter emissions (tons)", title = "motor vehicle emissions in baltimore") the error is:
geom_path: each group consist of 1 observation. need adjust group aesthetic?
the chart appears scatter plot though want line chart. tried replace geom_line() geom_line(aes(group = year)) didn't work.
in answer told convert year factor variable. did , problem persists. output of 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")
you have add group = 1 ggplot or geom_line aes().
for line graphs, data points must grouped knows points connect. in case, simple -- points should connected, group=1. when more variables used , multiple lines drawn, grouping lines done variable.
reference: cookbook r, chapter: graphs bar_and_line_graphs_(ggplot2), line graphs.
try this:
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")
Comments
Post a Comment