The ggplot2 line diagram gives "geom_path: each group consists of only one observation. Do I need to customize group aesthetics?"

With this data frame ("df"):

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

I am trying to create a line chart as follows:

  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 consists of only one observation. Do you need to adjust the aesthetics of the group?

The chart displays as a scatter plot, although I need a line chart. I tried replacing geom_line() with geom_line(aes(group = year)) , but that didn't work.

In response, I was told to convert the year into 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") 
+130
r ggplot2
Nov 22 '14 at 21:10
source share
4 answers

You only need to add group = 1 to ggplot or geom_line aes ().

For line graphs, data points must be grouped so that they know which points to connect. In this case, everything is simple - all points must be connected, therefore group = 1. When more variables are used and several lines are drawn, grouping of lines is usually performed by a variable.

Ref: Cookbook for R, Chapter: Bar_and_line_graphs_ Charts (ggplot2), Line Charts.

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") 
+261
Mar 12 '15 at 19:58
source share

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

 str(df) 

to check it out. Then double-change the variable to save the numbers of the year instead of converting to numbers of the level "1,2,3,4"

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

EDIT: it seems that your data.frame has an array variable that can call pb. Try then:

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

and again a conspiracy?

+20
Nov 22 '14 at 21:36
source share

Run R in a new session and paste it into:

 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"), row.names = c(NA, -4L), class = "data.frame") df[] <- lapply(df, as.numeric) # make all columns numeric ggplot(df, aes(year, pollution)) + geom_point() + geom_line() + labs(x = "Year", y = "Particulate matter emissions (tons)", title = "Motor vehicle emissions in Baltimore") 
+1
Nov 22 '14 at 21:50
source share

I had a similar problem with the data frame:

 group time weight.loss 1 Control wl1 4.500000 2 Diet wl1 5.333333 3 DietEx wl1 6.200000 4 Control wl2 3.333333 5 Diet wl2 3.916667 6 DietEx wl2 6.100000 7 Control wl3 2.083333 8 Diet wl3 2.250000 9 DietEx wl3 2.200000 

I think the variable for the x axis should be numeric so that geom_line knows how to connect the dots to draw a line.

after I changed the 2nd column to numeric:

  group time weight.loss 1 Control 1 4.500000 2 Diet 1 5.333333 3 DietEx 1 6.200000 4 Control 2 3.333333 5 Diet 2 3.916667 6 DietEx 2 6.100000 7 Control 3 2.083333 8 Diet 3 2.250000 9 DietEx 3 2.200000 

then it works.

0
Mar 05 '19 at 18:36
source share



All Articles