Adjust linetype of One Line in a facet_grid

。_饼干妹妹 提交于 2019-12-04 15:18:43

问题


I have a plot similar to this one:

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 ~ .)

Now I want only the line "1" in the upper facet "a" to be thicker and dashed. Is this possible?


回答1:


One of the first, and most important, things you're going to learn about ggplot2 is that when you want something to appear on your plot, you will in general create a variable in your data frame that represents the visual information you wish to display.

In your case, you need a variable that picks out only those observations from panel a, line 1:

b$grp <- with(b,(f == "a") & (c == 1))

Then you can map both size and linetype to this variable, and adjust the scales manually:

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")


来源:https://stackoverflow.com/questions/10870475/adjust-linetype-of-one-line-in-a-facet-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!