带灰色和黑色虚线的线图

Line plot with gray and black dotted lines

用ggplot2 R包我做了一个有6条彩色线的线图(指一个因素的6个水平),我想把它改成黑色和白色,制作3条黑色实线、虚线和点线加上 3 条灰色实线、虚线和点线。 我尝试使用 scale_linetype_manual() 和 scale_color_grey() 但我无法混合灰色和黑色虚线。

这里是灰阶选项的代码:

ggplot() + 
  geom_line(data = f[!is.na(f$fr),], aes(x=date2, y=fr, colour=locality, group=locality), 
            size = 1.0) + 
  scale_color_grey(start = 0, end = 0.9, guide="legend", name="Locality", 
                   labels=c("a","b","c","d","e","f")) + 
  xlab("") + 
  ylab("") + 
  theme_bw() +
  theme(legend.position = "top", panel.background = element_rect(fill = "white"), 
        panel.grid.major = element_line(colour = "white"), 
        axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + 
  facet_grid(.~year)    

虽然这是带虚线的代码:

ggplot() + 
  geom_line(data = f[!is.na(f$fr),], aes(x=date2, y=fr, linetype=locality, group=locality), 
            size = 1.0) + 
  scale_linetype_manual(name="Locality", 
                        values=c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash"), 
                        labels=c("a","b","c","d","e","f")) +
  xlab("") + 
  ylab("") + 
  theme_bw()+ 
  theme(legend.position = "top", panel.background = element_rect(fill = "white"), 
        panel.grid.major = element_line(colour = "white"), 
        axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
  facet_grid(.~year)    

所以..有人可以帮我用一根黑色实线、一根黑色虚线、一根黑色点线、一根灰色实线等来创建同样的图吗?

正如 MLavoie 所说,我认为您应该尝试创建显式向量。我无法尝试,但也许这样的方法应该有效:

vectCol=c("a"="black", "b"="black", "c"="black", "d"="grey","e"="grey", "f"="grey")
vectTyp=c("a"="solid", "b"="dashed", "c"="dotted", "d"="solid","e"="dashed", "f"="dotted")

然后,在 ggplot 中添加 "linetype" 作为参数,并使用 scale_manual 作为颜色和线型

ggplot(data = f[!is.na(f$fr),], aes(x=date2, y=fr, colour=locality,group=locality, linetype=locality)) + 
geom_line(size = 1.0) + 
scale_color_manual(values=vectCol, guide="legend", name="Locality", labels=c("a","b","c","d","e","f"))+ 
scale_linetype_manual(values=vectTyp, guide="legend", name="Locality", labels=c("a","b","c","d","e","f"))+
xlab("") + ylab("") + theme_bw() + 
theme(legend.position = "top", panel.background = element_rect(fill ="white"), panel.grid.major = element_line(colour = "white"), 
    axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + facet_grid(.~year) 

同时映射到 colourlinetype,并设置手动比例。

d <- data.frame(locality = rep(letters[1:6], each = 2),
            x = 1:2,
            y = rep(1:6, each = 2))

ggplot(d, aes(x = x, y = y,colour = locality, linetype = locality)) + 
  geom_line() +
  theme_bw() +
  scale_color_manual(name = "Locality",
                     values = c('black', 'black', 'black', 'grey', 'grey', 'grey'),
                     labels = c("a","b","c","d","e","f")) +
  scale_linetype_manual(name = "Locality", 
                        values = c("solid", "dashed", "dotted", "solid", "dashed", "dotted"), 
                        labels = c("a","b","c","d","e","f"))