• Register
0 votes

Problem :

Getting following issue  ggplot2 error

geom_path issue: each group consists of only one observation. Should i adjust the group aesthetic?

6 5 3
6,930 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

Start up R in a fresh session and please use bellow code:


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")
This will solve your problem
9 7 4
38,600 points
0 votes


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.

Follow this below code

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

The "Each group consists of only one observation" error message happens because your x aesthetic is a factor. ggplot takes that to mean that your independent variable is categorical, which doesn't make sense in conjunction with geom_line.

In this case, the right way to fix it is to convert that column of the data to a Date vector. ggplot understands how to use all of R's date/time classes as the x aesthetic.

Converting from a factor to a Date is a little tricky. A direct conversion,

jpycpi$DATE <- as.Date(jpycpi$DATE)

works in R version 3.3.1, but, if I remember correctly, would give nonsense results in older versions of the interpreter, because as.Date would look only at the ordinals of the factor levels, not at their labels. Instead, one should write

jpycpi$DATE <- as.Date(as.character(jpycpi$DATE))

Conversion from a factor to a character vector does look at the labels, so the subsequent conversion to a Date object will do the Right Thing.

You probably got a factor for $DATE in the first place because you used read.table or read.csv to load up the data set. The default behavior of these functions is to attempt to convert each column to a numeric vector, and failing that, to convert it to a factor. (See ?type.convert for the exact behavior.) If you're going to be importing lots of data with date columns, it's worth learning how to use the colClasses argument to read.table; this is more efficient and doesn't have gotchas like the above.

10 6 4
31,120 points

Related questions

0 votes
1 answer 161 views
0 votes
1 answer 189 views
I am trying to run the rWBclimate package in RStudio. I copied the following code from ROpenSci and pasted it into RStudio. But I get an error saying 'I don't know how to automatically choose scale for list of type objects. Default error to continuous error: geom_point requires the following ... 50 50 50 50 50 50 ... $ locator: chr "GBR" "GBR" "GBR" "GBR" ... Looking for your helpful answers.
asked Aug 17, 2020 game 4.6k points
0 votes
1 answer 1.4K views
Problem : I think my dataframe is correct and my code is also okay. But I am facing following error: Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : invalid graphics state What is wrong with my below data? date trt var val 1/12/2019 cc sw5 0.2684138 1/12/2019 ... ] Also I want to make the scatterplot as follows: ggplot(data = df,aes(x = date,y = val)) + geom_point(aes(group = trt))
asked Dec 6, 2019 alecxe 7.5k points
1 vote
1 answer 739 views
Problem: I have limited knowledge of R and ggplot. I have recently written a function and trying to run the function as shown below: ggplot(datfr, aes(x = dat1[1:951,], y = dat2[1:951,])) + geom_point() After executing above function I am facing below error: Don't ... and when I try to use nrow on my two datasets it again returns the same number of rows. How can I fix above ggplot related error.
asked May 26, 2020 Martin K 6.6k points
0 votes
1 answer 94 views
0 votes
1 answer 175 views
Problem : I am searching the working solution for following C# issue. only one usage of each socket address (protocol/network address/port) is normally permitted.
asked Nov 16, 2019 peterlaw 6.9k points
0 votes
2 answers 118 views
Problem : I am new to the SQL. What am I doing wrong here? SELECT LEFT(SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%', batchinfo.datapath), 8000), PATINDEX('%[^0-9]%', SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%',batchinfo. ... I am trying to do group by the first, second, and third columns having the max rid. It is works perfectly fine without the group by and having.
asked Nov 27, 2019 alecxe 7.5k points
0 votes
1 answer 24 views
1 vote
1 answer 54 views
Problem: I am a naive to R. I am trying to create the bar graph using the ggplot2 to compare a type of 2016 primary election and the Bernie Sanders's respective percentage of the votes in that election. I have below code: y = $ Type : Factor w/ 2 levels ... data of class uneval. I have already tried to search the solution on above error around but because of some unknown reasons to me I am stuck.
asked May 27, 2020 Martin K 6.6k points
1 vote
1 answer 40 views
Problem : I am trying to understand R and I am very new to Programming as wel. If I try to create the box chart with the use of standard error bars I often face below error message: “Error: ggplot2 doesn't know how to deal with data of class numeric”. Looking for help in quick fixing the above error.
asked May 5, 2020 stewart 4k points