gpt4 book ai didi

r - 将中值添加到每个框(不是 ggplot)

转载 作者:行者123 更新时间:2023-12-05 05:52:45 24 4
gpt4 key购买 nike

我有一个来自下面代码的箱线图,我想添加中值。

boxplot(ndvi_pct_sep~edge_direction, data= data_sample, subset = edge_direction %in% c(64,4, 1,16),ylab="NDVI2028-2016", xlab="森林边缘方向",names =c("北", "南", "东", "西"))

enter image description here

.我想将中值添加到箱线图中,知道如何操作吗?

最佳答案

它可能涉及使用图例 - 因为我没有您的数据,所以我无法使其完美,但下面的代码应该可以让您开始使用 R 中包含的 ToothGrowth 数据。我正在展示一个基础 R 和 ggplot 示例(我知道你说没有 ggplot,但其他人可能会使用它)。

# Load libraries
library(dplyr); library(ggplot2)

# get median data
mediandata <- ToothGrowth %>% group_by(dose) %>% summarise(median = median(len, na.rm = TRUE))
l <- unname(unlist(mediandata))
tg <- ToothGrowth # for convenience
tg$dose <- as.factor(tg$dose)

### Base R approach
boxplot(len ~ dose, data = tg,
main = "Guinea Pigs' Tooth Growth",
xlab = "Vitamin C dose mg",
ylab = "tooth length", col = "red")
for (i in 1:3){
legend(i-0.65,l[i+3]+5, legend = paste0("Median: ",l[i+3]), bty = "n")
}

### ggplot approach
ggplot(data = tg, aes(dose, len)) +
theme_classic() + theme(legend.position = "none") +
geom_boxplot()+
annotate("text",
x = c(1,2,3),
y = l[4:6]+1, # shit so you can read it
label = l[4:6])


基数 R: BaseR

图表: enter image description here

关于r - 将中值添加到每个框(不是 ggplot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70041817/

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