删除条带背景保持面板边框

时间:2013-01-06 19:14:50

标签: r ggplot2

我有以下类型的情节,并希望将每个条形文字保留在各个方面框的上方作为各种各样的“标题”,但没有strip.background周围的默认灰色背景和黑色边框。我将其颜色设置为接近我想要的颜色,但希望strip.background的底边或panel.border的上边缘为黑色。

enter image description here

library(ggplot2)

ggplot(mtcars, aes(mpg, hp)) + geom_point() +
    facet_wrap(~carb, ncol = 3) + theme_bw() +
    theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(colour="white", fill="white"),
        panel.border = element_rect(colour = "black"))

3 个答案:

答案 0 :(得分:38)

如果您为element_blank()设置strip.background并为element_rect(colour="black")设置panel.border,那么panel.border的上边缘将为黑色。

ggplot(mtcars, aes(mpg, hp)) + geom_point() +
  facet_wrap(~carb, ncol = 3) + theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black"))

enter image description here

答案 1 :(得分:9)

Setting panel.border might cover your graph unless you also add fill=NA to panel.border :

theme(panel.border = element_rect(fill = NA, colour = "black"))

One can also use:

theme(panel.background = element_rect(fill = NA, color = "black")

source (section "Panel Attributes")

答案 2 :(得分:3)

您可以尝试:

 strip.background = element_rect(colour=NA, fill=NA),
 panel.border = element_rect(fill = NA, color = "black"))