R语言 ggplot具有两个x轴文本堆栈

ss2ws0br  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(145)

我现在的图是这样的:

但我想在情节中添加一两个其他系列的文本,看起来像这样:

使用ggplot做这样的事情甚至是可能的吗?或者我应该去找另一个包吗?
更新:以下是我当前绘图的代码:

ggplot() + geom_col(data = s, aes(x = V1, y = V3, fill = V3), show.legend = F) + 
coord_flip() + 
labs(x = "", y = "") + 
scale_fill_gradient(low = "yellow", high="darkgreen") + 
theme(axis.text.y = element_text(vjust = 0, hjust = 0), plot.margin = margin(1, 0.3, 0.1, 0.3, "cm"), panel.background = element_rect(fill = 'white', colour = 'white'), axis.ticks.y = element_blank()) + ylim(0, 6)

这是一些假数据
这是真实的数据的输出结果

structure(list(V1 = c("regulation of locomotion", "regulation of cell migration", 
"skin development", "negative regulation of biological process", 
"cell adhesion", "response to oxygen-containing compound"), V2 = c(7.74e-05, 
0.000143, 0.000165, 0.000176, 0.00019, 0.00019), V3 = c(4.111259039, 
3.844663963, 3.782516056, 3.754487332, 3.721246399, 3.721246399
)), row.names = c(NA, 6L), class = "data.frame")
g9icjywg

g9icjywg1#

如果只有一列,最简单的方法之一就是通过geom_text图层为轴文本添加第二列。您只需确保为轴文本设置正确的大小和颜色:

library(ggplot2)

ggplot(data = s) +
  geom_col(aes(x = V3, y = V1, fill = V3), show.legend = F) +
  geom_text(aes(x = -1, y = V1, label = scales::label_scientific()(V2)),
    hjust = 0, size = .8 * 11 / .pt, vjust = 0, color = "grey30"
  ) +
  labs(x = "", y = "") +
  scale_fill_gradient(low = "yellow", high = "darkgreen") +
  theme(
    axis.text.y = element_text(vjust = 0, hjust = 0),
    plot.margin = margin(1, 0.3, 0.1, 0.3, "cm"),
    panel.background = element_rect(fill = "white", colour = "white"),
    axis.ticks.y = element_blank()
  ) +
  xlim(-1, 6)

相关问题