首页 > 解决方案 > R从strip.background函数中删除边框的两侧

问题描述

数据框

df <- data.frame(
structure(list(biological_group = structure(1:15, .Label = c("A", 
"B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", 
"O"), class = "factor"), norm_expression = c(2L, 3L, 4L, 6L, 
1L, 5L, 7L, 8L, 9L, 3L, 2L, 6L, 7L, 8L, 1L), SE = c(0.171499719, 
0.089692493, 0.153777208, 0.188012958, 0.153776128, 0.192917199, 
0.224766056, 0.231338325, 0.121716906, 0.094763028, 0.09635363, 
0.069986333, 0.113681329, 0.094614957, 0.391182473), Group = structure(c(1L, 
1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("", 
"Plant Products", "Sugars"), class = "factor")), class = "data.frame", row.names = c(NA, 
-15L), .Names = c("biological_group", "norm_expression", "SE", 
"Group")))

示例代码

library(ggplot2)

DFplot <-  ggplot(df, aes(biological_group,norm_expression)) + 
  ylab("Relative Normalized\nExpression (∆∆Cq)") + 
  geom_bar(fill="black",stat="identity") +
  theme(axis.title.x = element_blank())

DFplot2 <- DFplot+geom_errorbar(aes(ymin=norm_expression-SE,ymax=norm_expression+SE),width=0.5) + 
  geom_boxplot() + 
  facet_grid(~Group, scales = "free_x", switch = "x", space = "free_x") + 
  scale_y_continuous(expand = c(0,0), labels = scales::number_format(accuracy = 0.1)) + 
  theme_classic()

这给了我这个图表: 在此处输入图像描述

我想从带状文本标签(由 strip.background 指定)中删除垂直线,如下所示: 在此处输入图像描述

我意识到我可以只使用 photoshop 或其他东西,但我有几个图表要制作,所以如果我可以在代码中指定它会更容易。

标签: rggplot2facet-wrap

解决方案


这个答案很有帮助。

在你的情况下,你想找到strip-b你的底部条来替代。

编辑:替换了意外移除的底部条的顶部栏。

library(grid)

q <- ggplotGrob(DFplot2)

lg <- linesGrob(x=unit(c(1,0),"npc"), y=unit(c(1,1),"npc"), gp=gpar(col="black", lwd=2))

for (k in grep("strip-b",q$layout$name)) {
  q$grobs[[k]]$grobs[[1]]$children[[1]] <- lg
}

grid.draw(q)

固定底部条带矩形,顶部/底部已移除


推荐阅读