1

I have the following plot:

ggplot(age.meaning.gender, 
       aes(Birthyear, tou.rate, color=Gender, linetype=Grammatical.Function)) +
   geom_point()+
   ylim(c(0,1)) +
   geom_smooth(method = "lm", size=2, se=FALSE)+
   xlab("Year of Birth") +
   scale_linetype_manual(values=c("dashed", "solid")) +
   scale_color_brewer(type='qual', palette = 2)+
   theme_bw()+
   theme(panel.border = element_blank(),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         axis.line = element_line(colour = "black"))+
   guides(fill = guide_legend(keywidth = 1, keyheight = 1), 
          linetype = guide_legend(keywidth = 3, keyheight = 1), 
          colour = guide_legend(keywidth = 3, keyheight = 1))  

My current plot

I am wondering if it is possible to customize the legend in a way that combines the 2 variables: "Grammatical function" and "Gender" such that we can see 4 labels:

  1. spatial.male: linetype 1(e.g. green solid);
  2. spatial.female: linetype 2 (green dashed);
  3. temporal.female: linetype 3;
  4. temporal.male: linetype 4.
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
LAN
  • 13
  • 3
  • 1
    Hi, Welcome to Stack Overflow. You may want to look at making a reproducible example [eg](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), in your case demonstrating with a builtin data set like **iris** for example. This link may answer your question though https://stackoverflow.com/a/12411595/4927395 – Mark Neal Jul 20 '20 at 04:26

1 Answers1

0

I have revised my answer. If you could provide example data, others might be able to get what you expected exactly. One possible way is to start:

library(tidyverse)
df<-mtcars
df$am <- as.factor(df$am)
df$vs <- as.factor(df$vs)


ggplot(df, 
       aes(mpg, disp, color=am:vs,
           linetype = am:vs)) +
  geom_point()+
  geom_line()+
  xlab("Year of Birth") +
  scale_linetype_manual(values=c("dashed", "solid", "dashed", "solid")) +
  scale_color_manual(values = c(2, 2, 3,3)) +
  theme_bw()+
  theme(panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"))+
  guides(fill = guide_legend(keywidth = 1, keyheight = 1), 
         linetype = guide_legend(keywidth = 3, keyheight = 1), 
         colour = guide_legend(keywidth = 3, keyheight = 1))  

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27