简体   繁体   中英

Legend linetypes not displaying properly ggplot

I have the following plot:

ggplot(proba[108:140,], aes(c,four, color="a1")) + 
   geom_line(linetype=1, size=0.3) + 
   scale_x_continuous(breaks=seq(110,140,5)) + 
   theme_bw() + 
   theme(axis.line = element_line(colour = "black", size=0.25),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         panel.border = element_blank(),
         panel.background = element_blank()) + 
   theme(axis.text.x = element_text(angle = 0, hjust = +0.5, size=6,color="black")) + 
   theme(axis.text.y = element_text(angle = 0, hjust = -100, size=6, color="black")) + 
   theme(axis.ticks=element_line(colour="black",size=0.25)) + 
   xlab("\nTime-steps")+ylab("Proportion correct\n") + 
   theme(axis.text=element_text(size=8),
         axis.title=element_text(size=8)) + 
   geom_line(aes(c,three, color="a2"), size=0.2, linetype=2) + 
   geom_line(aes(c,one, color="a3"),linetype=3, size=0.8) + 
   geom_line(aes(c,two, color="a4"), linetype=1, size=0.6) + 
   scale_color_manual(values=c("a1"="red3", "a2"="red3","a3"="blue3","a4"="blue3")) + 
   theme(legend.title = element_text(colour="black", size=7)) + 
   theme(legend.position="bottom" ,
         legend.direction="horizontal", 
         legend.title=theme_blank()) + 
   theme(legend.text=theme_text(size=7), 
         legend.background=theme_blank(), 
         legend.key=theme_blank())

The four lines on the plot are displayed in different linetypes, however the legend does not show these different linetypes only the different colours. I am obviously missing a very simple argument in theme() but I cannot figure out what it is. Maybe I need to specify the linetypes again in scale_colour_manual()?

Here's the data: https://dl.dropboxusercontent.com/u/22681355/proba.csv

proba<-read.csv("proba.csv",head=T)

Here, this is the correct way to do this in ggplot2 :

proba <- read.csv("~/Downloads/proba.csv")

proba1 <- proba[108:140,]
proba1 <- melt(proba1,id.vars = 1:2)

ggplot(proba1,aes(x = c,y = value,colour = variable,linetype = variable,size = variable)) + 
    geom_line() + 
    scale_x_continuous(breaks=seq(110,140,5)) +
    scale_colour_manual(values=c("blue3","blue3","red3","red3")) + 
    scale_linetype_manual(values = c(2,1,3,1)) + 
    scale_size_manual(values = c(0.2,0.3,0.8,0.6)) + 
    xlab("\nTime-steps") + 
    ylab("Proportion correct\n") +
    theme_bw() +
    theme(axis.text=element_text(size=6),
          axis.title=element_text(size=8),
          axis.line = element_line(size=0.25),
          axis.ticks=element_line(size=0.25),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          panel.background = element_blank(),
          legend.position="bottom" ,
          legend.direction="horizontal", 
          legend.title=element_blank(),
          legend.text=element_text(size=7), 
          legend.background=element_blank(), 
          legend.key=element_blank())

在此输入图像描述

Try to keep all your theme adjustments in one call to theme , otherwise things get pretty messy. Also, you had some calls to theme_* that should have been element_* .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM