gpt4 book ai didi

r - ggplot2:两个横杆之间的颜色区域

转载 作者:行者123 更新时间:2023-12-01 12:09:18 26 4
gpt4 key购买 nike

是否可以在 ggplot2 中只对部分条形图着色?
我想要横杆之间的彩色区域:“平均每日最大值”和“平均每日最小值”。这是我的数据:

temperature <- read.table(text = 'X  1  2  3  4  5  6
1 "Highest temperature" 31 40 39 34 39 42
2 "Mean daily maximum" 27 34 32 30 34 35
3 "Mean daily minimum" 26 31 31 30 31 35
4 "Lowest temperature" 18 20 20 20 20 24', header =T)

t <- dcast(melt(temperature), variable~X)
ggplot(t, aes(x=variable,ymin = `Lowest temperature`,
ymax = `Highest temperature`, lower = `Lowest temperature`,
upper = `Highest temperature`, middle = `Mean daily maximum`)) +
geom_boxplot(stat = 'identity') +
xlab('Number') +
ylab('Temperature') +
geom_crossbar(aes(y = `Mean daily maximum` ))+
geom_crossbar(aes(y = `Mean daily minimum`))

最佳答案

library(ggplot2)
library(reshape2)

read.table(
text = 'X 1 2 3 4 5 6
1 "Highest temperature" 31 40 39 34 39 42
2 "Mean daily maximum" 27 34 32 30 34 35
3 "Mean daily minimum" 26 31 31 30 31 35
4 "Lowest temperature" 18 20 20 20 20 24',
header = TRUE
) -> temperature

temp_long <- reshape2::dcast(reshape2::melt(temperature), variable~X)

ggplot(
temp_long,
aes(
x = variable,
ymin = `Lowest temperature`,
ymax = `Highest temperature`,
lower = `Lowest temperature`,
upper = `Highest temperature`,
middle = `Mean daily maximum`
)
) +
geom_boxplot(stat = 'identity') +
geom_rect(
aes(
xmin = as.numeric(variable)+0.45,
xmax = as.numeric(variable)-0.45,
ymin = `Mean daily minimum`,
ymax = `Mean daily maximum`
),
fill = "#a50f15"
) +
geom_crossbar(aes(y = `Mean daily maximum` )) +
geom_crossbar(aes(y = `Mean daily minimum`)) +
labs(x = 'Number', y = 'Temperature')

enter image description here

关于r - ggplot2:两个横杆之间的颜色区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53461653/

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