How can I plot a line for one country throughout time via ggplot?

user19562955

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.

Allan Cameron

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

enter image description here

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) 

enter image description here


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) 

enter image description here

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I plot the number of attendees throughout a meeting using R and ggplot?

how can i add points to geom_line plot in ggplot

In ggplot, how can I plot data as a line graph?

In ggplot, how can I plot time series data like this picture?

How can I make one plot from multiple variables in ggplot?

How can I jump 2 words at a time throughout Windows?

How can i plot two column vectors in 2 different y axis in one plot in ggplot in R?

How can I read one line at a time from a trio ReceiveStream?

How can I get the contents of a file one line at a time?

How can I pass variables to a script one line at a time in bash?

How can I make a contour plot doesn't overlap on the line plot in one axes?

How can I plot two different spaced time series on one same plot in Python

How can I plot bounding boxes in ggplot?

How can I plot the residuals of lm() with ggplot?

How can I add a legend to this plot (with ggplot())?

How can I plot a closed line using two vector in ggplot2?

How can I draw line segment across plot axis in ggplot2?

How to plot a line chart in ggplot with a date and time axis?

How do can i plot time series data with error bars with GGplot in r?

How to highlight / plot one latitude line on map ggplot2

How can I plot several series as lines and one of them as area using ggplot2?

How can I add a border along only one side of a ggplot2 plot?

How to plot a time series with multiple country?

How can I get the "current" IANA time zone abbreviation throughout time in ICU4J?

How can I plot the label on the line of a lineplot?

How can i plot line chart in python?

how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?

How can I plot a whole scaled data set with one line per row?

How do I connect the points in this plot with a line using ggplot?