I have two data sets, one is for males and one for females. They both have 4 variables, which are: Year, DK, SE NO. So they both looks like this (of course just with different values):
I want to make line plots for Males and Females stacked and grouped into contries, DK SE and NO. So it can be stacked plots, like this:
CodePudding user response:
You haven't told us what your data frames are called, but let's call them men
and women
. As long as they both have exactly the same columns (including names), you can do:
library(tidyverse)
men %>%
mutate(Sex = "Men") %>%
bind_rows(women %>% mutate(Sex = "Women")) %>%
pivot_longer(c("DK", "SE", "NO")) %>%
ggplot(aes(Year, value, colour = name))
geom_line()
facet_grid(Sex~.)
theme_light()
Created on 2022-04-22 by the reprex package (v2.0.1)
Made up data with same structure as question data
set.seed(1)
men <- data.frame(Year = 1995:2022,
DK = 35 cumsum(rnorm(28)),
SE = 40 cumsum(rnorm(28)),
NO = 38 cumsum(rnorm(28)))
women <- data.frame(Year = 1995:2022,
DK = 35 cumsum(rnorm(28)),
SE = 40 cumsum(rnorm(28)),
NO = 38 cumsum(rnorm(28)))