提问人:confusedindividual 提问时间:11/17/2023 最后编辑:Philconfusedindividual 更新时间:11/17/2023 访问量:26
R 条形图中每个条形和每个组的轴标签
axis labels for each bar and each group in R barchart
问:
我正在尝试在带有避视图的条形图中为每个条形和每个组创建轴标签。此处的相关问题:条形图中每个条形和每个组的轴标签,带有避视图组
我希望最终结果看起来像这样,但有 3 组,每组有 4 个小节
我在下面添加了示例数据
我玩过,但似乎不起作用。scale_x_discrete
scale_x_continuous
# create a dataset
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
df <- data.frame(specie,condition,value)
df
# Grouped
set.seed(123)
df
ggplot(df, aes(fill = specie, y = value, x = condition)) +
geom_bar(stat="identity", position = position_dodge(width = 0.9), width = 0.6) +
scale_fill_manual(
"specie",
values = c("black", "black", "black", "black"),
) +
theme_classic() +
theme(panel.border = element_rect(colour = "black", fill=NA, size=1),
text = element_text(size = 8, family = "sans"),
) + labs(x = "Nitrogen normal stress", y = expression(paste("Total Phytoplankton Abundance (", x10^{6}, "cells/L)"))) +
scale_y_continuous(expand = c(0,0)) +
theme(legend.position="none") + scale_x_discrete("", breaks=c(-0.25,0.25,0.75,1.25,1.75,2.25,2.75,3.25,3.75,4.25,4.75,5.25),
labels=rep(c("sorgho","poacee", "banana", "triticum"),times=3))
'''scale_x_discrete'' 出于某种原因删除所有 x 轴标签。
答:
0赞
Allan Cameron
11/17/2023
#1
也许是这样的事情?
ggplot(df, aes(specie, value, fill = specie)) +
geom_col(fill = "gray80") +
geom_col(alpha = 0.5) +
facet_grid(.~condition, switch = "x") +
scale_x_discrete(NULL, expand = c(0.1, 0.5)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 30)) +
scale_fill_brewer(palette = "Set1", guide = "none") +
theme_minimal(base_size = 20) +
theme(strip.placement = "outside",
panel.spacing.x = unit(0, "mm"),
panel.grid.major.x = element_blank(),
axis.text.x = element_text(size = 12),
axis.ticks.x = element_line(color = "gray"),
axis.line.x = element_line())
评论