Adjust the line type of One Line in facet_grid

I have a plot similar to this:

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 face of “a” to be thicker and dashed. Is it possible?

+3
source share
1 answer

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

In your case, you need a variable that selects 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 manually adjust the scales:

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

Source: https://habr.com/ru/post/950828/


All Articles