ggplot2 line chart gives “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

With this data frame ("df"):

year pollution 1 1999 346.82000 2 2002 134.30882 3 2005 130.43038 4 2008  88.27546

I try to create a line chart like 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 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 geom_line() with geom_line(aes(group = year)) but that didn't work.

In an answer I was told to convert year to a factor variable. I did and the problem persists. This is the output of 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")

回答1:

You only have to add group = 1 into the ggplot or geom_line aes().

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:

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")


回答2:

You get this error because one of your variables is actually a factor variable . Execute

str(df) 

to check this. Then do this double variable change to keep the year numbers instead of transforming into "1,2,3,4" level numbers:

df$year <- as.numeric(as.character(df$year))

EDIT: it appears that your data.frame has a variable of class "array" which might cause the pb. Try then:

df <- data.frame(apply(df, 2, unclass))

and plto



回答3:

Start up R in a fresh session and paste this in:

library(ggplot2)  df <- 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"        
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!