我正在尝试在facet_grid函数中自定义条带。 我的图目前看起来像:
我成功地将条带的背景更改为紫色,但是,即使我在功能中将颜色设置为“黑色”,我也无法将边框颜色更改为黑色。
此外,我希望文本和矩形之间的间距更大,以便看起来更好。我应该如何实现?
我的代码如下:
plot.density <- ggplot(df_densityWindow, aes(x = idx, y = density, color = factor(location))) +
geom_bar(stat = 'identity', fill = 'white') +
facet_grid(marker ~ case, scales = 'free') +
theme(strip.background = element_rect(colour="red", fill="#CCCCFF")) +
scale_color_manual(name = 'Regions',values = c("#F26969", "#02f527",'#F2F2C6')) +
background_grid(major = 'y', minor = "none") + # add thin horizontal lines
xlab('Index') +
ylab(expression(paste('Density/', mm^2, ))) +
theme(axis.title = element_text(size = 28)) +
theme(axis.text = element_text(size = 26)) +
theme(legend.text = element_text(size = 16)) +
theme(legend.title = element_text(size = 18)) +
panel_border() # and a border around each panel
plot(plot.density)
如有必要,可以在此处下载数据: data 谢谢!
答案 0 :(得分:1)
您对色带的颜色规格对我来说没有错误。可以通过在margin
主题上设置strip.text
参数来增加带状文本和框之间的间距:
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() +
facet_grid(Species ~ rev(Species)) +
theme(strip.background = element_rect(colour="red", fill="#CCCCFF"),
strip.text = element_text(margin = margin(10, 10, 10, 10)))
可能的调试策略:
1A。如果那不起作用,请检查您的ggplot版本是否为〜3.2-ish
1B。如果可行,请继续执行2。
我特别不熟悉panel_border()
和background_grid()
,因此您可以先尝试那些。
编辑:根据提供的数据和代码进行绘图
ggplot(data, aes(x = idx, y = density, colour = factor(location))) +
geom_col(fill = "white") +
scale_color_manual(name = 'Regions',values = c("#F26969", "#02f527",'#F2F2C6')) +
facet_grid(marker ~ case, scales = "free") +
xlab('Index') +
ylab(expression(paste('Density/', mm^2, ))) +
theme(strip.background = element_rect(colour = "red", fill = "#CCCCFF"),
strip.text = element_text(margin = margin(10, 10, 10, 10)),
axis.title = element_text(size = 28),
axis.text = element_text(size = 26),
legend.text = element_text(size = 16),
legend.title = element_text(size = 18),
# Improvised based on missing functions
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour = "grey90"),
panel.background = element_rect(fill = "white", colour = NA),
panel.border = element_rect(colour = "grey90", fill = NA))