It is my first post, therefore I have an absolute beginner question. I tried to find help elsewhere, but couldn't make even the first steps.
I have the following data frame:
My aim is to plot a geom_line or geom_smooth, with a timeline on the x-axis from the column "Verstorbene" in my df (from 01-01 to 31-12), on the y-axis I would like to have a value-range from (0-1000) and the factors should be the years 2015-2022 for each year to be it's own factor (line).
CodePudding user response:
It is difficult without a reproducible example to know the exact format of your data, but from the picture of your data, the steps you need to take are:
- Pivot your data into long format, so that there is a single column for the value, and a factor column telling you which year the observation came from
- Turn your date strings into actual dates.
- Plot the result
I have created an example data set that should have the same names and structure as your own data (see footnote), so that the following code should work for you too (as long as your data frame is called
my_df
).
If this does not work for you, please include a reproducible version of your data rather than an image. dput(my_df)
will be helpful to create this.
library(tidyverse)
my_df %>%
pivot_longer(-1, names_to = 'Year') %>%
mutate(Verstorbene = as.POSIXct(strptime(paste(Verstorbene, 2015),
'%e %B %Y'))) %>%
ggplot(aes(Verstorbene, value, color = Year))
geom_line()
scale_x_datetime(date_labels = '%e %B')
theme_minimal(base_size = 15)