chevron_left

Problem :

Getting following issue ggplot2 error

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

Solution :

Startup R in a fresh session and please use bellow code:

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

This will solve your problem

Another Solution:

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

Explanation:
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.

More Posts

Problem of Error in .call.graphics(c_palette2, .call(c_palette2, null)) : invalid graphics state Alecxe01 - Nov 8
Error in as.posixct.numeric(value) : 'origin' must be supplied. Alecxe01 - Nov 10
I used the web browsers Edge and Brave exclusively for 1 week each here are my thoughts andrewbaisden - Jun 29
What is the syntax of the For Each loop in case of associative array ? sakshi - May 18
How to unify name of the months with each date in MySQL Tushar Shuvro - Jul 16, 2020
How do i adjust the volume on google chrome amna - Nov 1
How to store each line from many .txt files into one variable with Ruby? Tushar Shuvro - Jul 19, 2020
Integers can only be combined with integers of the same class, or scalar doubles. Farkhanda Athar - Jun 13, 2020
A network segment where only one device can communicate at a time is known as a _____. sanchi - May 2
Incorrect table definition; There can be only one auto column and it must be defined as a key Farkhanda Athar - Apr 13, 2020