Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

r - How to change color of facet borders when using facet_grid

When using facet_grid, ggplot2 divides the major categories that make up the facet variables with a broader-than-usual white line. This serves most purposes well. Sometimes I want to more clearly show the divisions between these major categorizations and would like to shade the facet division with another color. Is there a way to do that? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Although a year late, I found this to be an easy fix:

ggplot(mpg, aes(cty, hwy, color = factor(year)))+ 
  geom_point()+
  facet_grid(cyl ~ drv) +
  theme(panel.margin=unit(.05, "lines"),
        panel.border = element_rect(color = "black", fill = NA, size = 1), 
        strip.background = element_rect(color = "black", size = 1))

UPDATE 2021-06-01


As of ggplot2 3.3.3, the property panel.margin is deprecated, and we should use panel.spacing instead. Therefore, the code should be:

ggplot(mpg, aes(cty, hwy, color = factor(year)))+ 
  geom_point()+
  facet_grid(cyl ~ drv) +
  theme(panel.spacing = unit(.05, "lines"),
        panel.border = element_rect(color = "black", fill = NA, size = 1), 
        strip.background = element_rect(color = "black", size = 1))

facet wrap example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...