Adam
Adam

Reputation: 107

How do you make a line graph with multiple lines from multiple variables in R

I have two dataframes and I want to plot a comparison between them. The plot and dataframes look like so

df2019 <- data.frame(Role = c("A","B","C"),Women_percent = c(65,50,70),Men_percent = c(35,50,30), Women_total = 
c(130,100,140), Men_total = c(70,100,60))
df2016 <- data.frame(Role= c("A","B","C"),Women_percent = c(70,45,50),Men_percent = c(30,55,50),Women_total = 
c(140,90,100), Men_total = c(60,110,100))

all_melted <- reshape2::melt(
  rbind(cbind(df2019, year=2019), cbind(df2016, year=2016)),
  id=c("year", "Role"))

Theres no reason I need the data in melted from, I just did it because I was plotting bar graphs with it, but now I need a line graph and I dont know how to make line graphs in melted form, and dont know how to keep that 19/16 tag if not in melted frame. When i try to make a line graph I dont know how to specify what "variable" will be used. I want the lines to be the Women,Men percent values, and the label to be the totals.Should look like this with year in legend, 2016 dotted (in this picture the geom_text is the percent values, I want it to use the total values)

Crucially I want the linetype to be dotted in 2016 and for the legend to show that

Upvotes: 1

Views: 345

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174476

I think it would be simplest to rbind the two frames after labelling them with their year, then reshape the result so that you have columns for role, year, gender, percent and total.

I would then use a bit of alpha scale trickery to hide the points and labels from 2016:

df2016$year <- 2016
df2019$year <- 2019

rbind(df2016, df2019) %>%
  pivot_longer(cols = 2:5, names_sep = "_", names_to = c("Gender", "Type")) %>% 
  pivot_wider(names_from = Type) %>%
  ggplot(aes(Role, percent, color = Gender, 
             linetype = factor(year), 
             group = paste(Gender, year))) +
  geom_line(size = 1.3) +
  geom_point(size = 10, aes(alpha = year)) +
  geom_text(aes(label = total, alpha = year), colour = "black") +
  scale_colour_manual(values = c("#07aaf6", "#ef786f")) +
  scale_alpha(range = c(0, 1), guide = guide_none()) +
  scale_linetype_manual(values = c(2, 1)) +
  labs(y = "Percent", color = "Gender", linetype = "Year")

enter image description here

Upvotes: 3

Related Questions