My code is
ggplot(classen, aes(x=Year, y=, colour= class)) geom_line()
My df is:
Country Name | Year | SupDem |
---|---|---|
Turkiye | 1990 | 5 |
Turkiye | 1991 | 45 |
Turkiye | 1993 | 50 |
US | 1990 | 80 |
US | 1991 | 85 |
US | 1993 | 84 |
I need to choose the country's name and then want to plot it, but I am not sure if I can choose the country within the ggplot() function.
CodePudding user response:
Yes, the color
should be Country Name
rather than class
, and y
should be SupDem
library(ggplot2)
ggplot(df, aes(Year, SupDem, colour = `Country Name`))
geom_line()
Or, if you want more modern styling, something like:
library(ggplot2)
ggplot(df, aes(Year, SupDem, colour = `Country Name`))
geom_line(size = 1)
geom_point(size = 3)
scale_color_manual(values = c('orange', 'deepskyblue4'))
theme_minimal(base_size = 16)
EDIT
To add the mean value to the plot, we can do:
ggplot(df, aes(Year, SupDem))
geom_line(aes(colour = `Country Name`), size = 1)
geom_point(aes(colour = `Country Name`), size = 3)
geom_line(stat = 'summary', fun = mean, aes(color = 'mean'), linetype = 2)
scale_color_manual(values = c('gray', 'orange', 'deepskyblue4'))
theme_minimal(base_size = 16)
Created on 2022-10-04 with reprex v2.0.2
Data taken from question in reproducible format
df <- structure(list(`Country Name` = c("Turkiye", "Turkiye", "Turkiye",
"US", "US", "US"), Year = c(1990L, 1991L, 1993L, 1990L, 1991L,
1993L), SupDem = c(5L, 45L, 50L, 80L, 85L, 84L)), class = "data.frame",
row.names = c(NA, -6L))