-1

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

example of data set

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:

idea of plot

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
rr19
  • 79
  • 1
  • 9
  • 1
    Please post data as copy/pasteable code, not a pictures. Have you tried to make the plots on your own? Where did you get stuck? I would suggest trying `ggplot2` with facets by sex. – Gregor Thomas Apr 22 '22 at 19:00
  • We have a [nice tutorial for minimal working examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) I suggest you to read, before posting actually. Cheers! – jay.sf Apr 22 '22 at 19:02

1 Answers1

0

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)))
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • @rr19 I'm guessing one or more of your columns are character rather than numeric. Try changing `aes(Year` to `aes(as.numeric(Year)` – Allan Cameron Apr 22 '22 at 19:34
  • But it would be better not to guess. It's really hard to help when you use images of data rather than copy/paste able text, which is easy to do. – Allan Cameron Apr 22 '22 at 19:35