-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
  • 91,771
  • 6
  • 28
  • 55
rr19
  • 5
  • 4
  • 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
  • 91,771
  • 6
  • 28
  • 55
  • Hi, many thanks for your comment and time! I tried what you did but the lines are not plotted/visible: library(tidyverse).. I don't know how to attach the picture of my R here, but the code I ran was: fig2m %>% mutate(Sex = "Men") %>% bind_rows(fig2k %>% mutate(Sex = "Women")) %>% pivot_longer(c("DK", "SE", "NO")) %>% ggplot(aes(Year, value, colour = name)) + geom_line() + facet_grid(Sex~.) + theme_light() – rr19 Apr 22 '22 at 19:29
  • I got this comment, when I ran the code: geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? geom_path: – rr19 Apr 22 '22 at 19:31
  • @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
  • Thank you!! It worked by using as.numeric() But now I have another question. I added + ylim(20,50) at the end of the code and its only for one of the panels.. DO you know how I can adjust the axis limits for both panels? – rr19 Apr 22 '22 at 21:07