使用ggplot2 R软件包我创建了一个带有6条彩色线条的线条图(指的是6个级别的因子),我想用黑色和白色来改变它,制作3条BLACK实线,虚线和虚线以及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)
所以..有人可以帮我用一条黑色实线,一条黑色虚线,一条黑色虚线,一条灰色实线等创建相同的情节?
答案 0 :(得分:5)
映射到colour
和linetype
,并设置手动缩放。
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"))
答案 1 :(得分:2)
正如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)