“Geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?” – How To Fix

“geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic”

The “geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?” error often occurs when our variable is a factor variable. Check out our tutorial below to find the best solution. 

What causes the “geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?” error?

Reproduce the “geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?” error

Example 1:

Let’s say we will have this data set that contains information about the number of passengers taking underground railway systems per year in six major cities.

dataFrame <- data.frame(city=c("London", "Paris", "Tokyo", "Washington DC", "Kyoto", "Los Angeles"),
                 number_of_passengers=c(775, 1191, 1928, 144, 45, 50))
dataFrame

Output:

               city            number_of_passengers
1            London                             775
2             Paris                            1191
3             Tokyo                            1928
4     Washington DC                             144
5             Kyoto                              45
6       Los Angeles                              50

Now we will draw a line plot with ggplot2 to represent the numbers of passengers taking underground railway systems in the six cities. First, we have to install and load the ggplot2 package:

install.packages("ggplot2")
library(ggplot2)

Then, we will create the line plot with these codes:

ggplot(dataFrame, aes(city, number_of_passengers)) +
  geom_point() + 
  geom_line() + 
  ggtitle("Number of passengers taking underground railway systems per year (in millions)") + 
  theme(plot.title = element_text(size = 12, face = "bold", color = "blue"))

Output: 

As you can see, instead of a line plot like we want, we get a scatterplot. Alongside, we also so get an error message: 

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

This is because, in order to form a line graph, R must know what are the points to connect. 

How to solve this problem?

Solution 1: Group the data points

Because R needs to know which points to connect, we have to group the data points. Therefore, we can specify group=1 in the aes()

Fix code for example 1: 

dataFrame <- data.frame(city=c("London", "Paris", "Tokyo", "Washington DC", "Kyoto", "Los Angeles"),
                 number_of_passengers=c(775, 1191, 1928, 144, 45, 50))
dataFrame

library(ggplot2)

ggplot(dataFrame, aes(city, number_of_passengers, group=1)) +
  geom_point() + 
  geom_line() + 
  ggtitle("Number of passengers taking underground railway systems per year (in millions)") + 
  theme(plot.title = element_text(size = 12, face = "bold", color = "blue"))

Output:

Solution 2: Use numeric x-variable

Let’s take a look at example 2: 

We will draw a line plot for this data set giving information about the number of factories in England and Wales from 1851 to 1901. 

dataFrame <- data.frame(year=as.numeric(c("1851", "1861", "1871", "1881", "1891", "1901")),
                 number_of_factories=c(225, 227, 622, 721, 625, 600))
dataFrame

library(ggplot2)
ggplot(dataFrame, aes(as.numeric(year), number_of_factories)) +
  geom_point() + 
  geom_line() + 
  ggtitle("Number of factories in England and Wales from 1851 to 1901") + 
  theme(plot.title = element_text(size = 12, face = "bold", color = "green"))

Output:

In this example, we can try to convert the year into numeric using as.numeric(). And since the x-variable is numeric, we don’t have to use group=1.

Summary

“geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?” is common error in R. It often occurs when R does not know which points to connect. The two simple solutions are to group the data points using group=1 or coerce the value to numeric.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *