我有一个类似于这个的情节:
b <- data.frame(x=c(1,2,3,1,2,3,1,2,3,1,2,3),y=c(1,2,3,1.5,1.9,2.5,3,2,1,2.9,1.8,1.5),c=c("1","1","1","2","2","2","1","1","1","2","2","2"),f=c("b","b","b","b","b","b","a","a","a","a","a","a"))
ggplot(b,aes(x=x,y=y,color=c,group=c))+geom_line()+facet_grid(f ~ .)
现在我只希望上面“a”中的行“1”更粗并且虚线。这可能吗?
答案 0 :(得分:7)
您将要了解的关于 ggplot2 的第一个也是最重要的事情之一是,当您想要在绘图上显示某些内容时,您通常会在数据中创建一个变量表示您希望显示的视觉信息的框架。
在您的情况下,您需要一个变量,仅从面板a,第1行中选择那些观察结果:
b$grp <- with(b,(f == "a") & (c == 1))
然后,您可以将size
和linetype
映射到此变量,并手动调整比例:
library(scales)
ggplot(b,aes(x=x,y=y)) +
geom_line(aes(color=c,group=c,size = grp,linetype = grp)) +
facet_grid(f ~ .) +
scale_size_manual(values = c(0.5,1.2),guide = "none") +
scale_linetype_manual(values = c('solid','dashed'),guide = "none")