gpt4 book ai didi

r - 如何在堆积条形图中的两个水平条之间插入文本

转载 作者:行者123 更新时间:2023-12-04 08:14:33 24 4
gpt4 key购买 nike

我想显示 label 中的四个值栏目高于或低于 每个单杠。我知道如何使用 geom_text (或使用 ggrepel )在条的左侧或右侧显示文本,但我想不出在条之间显示文本的方法。

# library
library(ggplot2)

# 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))
label <- c("t1", "t2", "t3", "t4")
data <- data.frame(specie, condition, value)

# Stacked + percent
ggplot(data, aes(fill = condition, y = value, x = specie)) +
geom_bar(position = "fill", stat = "identity") +
coord_flip()

创建于 2021-01-18 由 reprex package (v0.3.0)
所以,这就是我的期望。关于如何做到这一点的任何想法?
enter image description here

最佳答案

这可能是一个技巧:

library(ggplot2)
# 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))
label <- c("t1", "t2", "t3", "t4")
data <- data.frame(specie, condition, value)
# Stacked + percent
ggplot(data, aes(fill = condition, y = value, x = specie,label=specie)) +
geom_bar(position = "fill", stat = "identity") +
geom_text(aes(y=0.5),position = position_nudge(0.55))+
coord_flip()
输出:
enter image description here
当然,您的文本应该在您的数据框中。

关于r - 如何在堆积条形图中的两个水平条之间插入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65775076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com