如何将我的方面标签 (strip.background) 的颜色更改为与其条形相同的颜色?
How can I change the color of my facet label (strip.background) to be the same as its bars?
我有一个 facet_grid
图总结了每个类别的基因分布,如下所示。我想更改刻面标签/strip.background
(代码中带有红色框和“类别”的那个)的颜色,使其与其对应的图相似。
如何增加条形之间的间距(而不是面之间的间距)以使其更具可读性?正如您在 y 轴标签(被黑框覆盖)上看到的那样,由于有很多数据,它正在被压缩并且标签变得不可读。
ggplot(allVFs, aes(x=Dist,y=genes)) +
geom_bar(stat="identity", width = 0.75, size = 0.05 ) +
facet_grid(Categories ~., scales = "free", space = "free") +
labs( x = "Distribution (%)", y = "Genes", title = "VFs per category" ) +
theme( strip.text = element_text( size = 8, color = "white", hjust = 0.5),
strip.text.y = element_text(angle=0),
strip.background = element_rect( fill = "#5675D6"), # I tried changing this but couldn't make it similar to the bar colors
panel.background = element_rect( fill = "#efefef"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line( color = "#C0C0C0" ),
panel.spacing.x = unit( 0.8, "cm" ),
panel.spacing.y = unit( 0.5, "cm" ),
legend.position = "none" ) +
geom_text(aes( label = paste0( Dist, "%" ), y = genes),
vjust = 0, size = 2, color = "black" ) +
aes(fill = as.factor(Categories)) #This gives the color to each facet that I want to replicate with the facet labels
如有任何改进此情节的建议,我们将不胜感激!
这在 ggplot2
中是不可能的,但我们可以深入研究 gtable
对象来实现结果。
我将使用示例图和数据。但这也适用于您的示例:
library(ggplot2)
g <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_bar() +
facet_grid(Species ~ .)
# Find the colors used
fill_colors <- unique(ggplot_build(g)$data[[1]]$fill)
# Find strips glob
gt<-ggplot_gtable(ggplot_build(g))
strips <- which(startsWith(gt$layout$name,'strip'))
# Change the fill color of each strip
for (s in seq_along(strips)) {
gt$grobs[[strips[s]]]$grobs[[1]]$children[[1]]$gp$fill <- fill_colors[s]
}
plot(gt)
由 reprex package (v0.3.0)
于 2020-11-26 创建
我们正在做的是找到包含每个条带的网格对象,并使用从绘图中提取的颜色手动更改其填充属性。
我有一个 facet_grid
图总结了每个类别的基因分布,如下所示。我想更改刻面标签/strip.background
(代码中带有红色框和“类别”的那个)的颜色,使其与其对应的图相似。
如何增加条形之间的间距(而不是面之间的间距)以使其更具可读性?正如您在 y 轴标签(被黑框覆盖)上看到的那样,由于有很多数据,它正在被压缩并且标签变得不可读。
ggplot(allVFs, aes(x=Dist,y=genes)) +
geom_bar(stat="identity", width = 0.75, size = 0.05 ) +
facet_grid(Categories ~., scales = "free", space = "free") +
labs( x = "Distribution (%)", y = "Genes", title = "VFs per category" ) +
theme( strip.text = element_text( size = 8, color = "white", hjust = 0.5),
strip.text.y = element_text(angle=0),
strip.background = element_rect( fill = "#5675D6"), # I tried changing this but couldn't make it similar to the bar colors
panel.background = element_rect( fill = "#efefef"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line( color = "#C0C0C0" ),
panel.spacing.x = unit( 0.8, "cm" ),
panel.spacing.y = unit( 0.5, "cm" ),
legend.position = "none" ) +
geom_text(aes( label = paste0( Dist, "%" ), y = genes),
vjust = 0, size = 2, color = "black" ) +
aes(fill = as.factor(Categories)) #This gives the color to each facet that I want to replicate with the facet labels
如有任何改进此情节的建议,我们将不胜感激!
这在 ggplot2
中是不可能的,但我们可以深入研究 gtable
对象来实现结果。
我将使用示例图和数据。但这也适用于您的示例:
library(ggplot2)
g <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_bar() +
facet_grid(Species ~ .)
# Find the colors used
fill_colors <- unique(ggplot_build(g)$data[[1]]$fill)
# Find strips glob
gt<-ggplot_gtable(ggplot_build(g))
strips <- which(startsWith(gt$layout$name,'strip'))
# Change the fill color of each strip
for (s in seq_along(strips)) {
gt$grobs[[strips[s]]]$grobs[[1]]$children[[1]]$gp$fill <- fill_colors[s]
}
plot(gt)
由 reprex package (v0.3.0)
于 2020-11-26 创建我们正在做的是找到包含每个条带的网格对象,并使用从绘图中提取的颜色手动更改其填充属性。