gpt4 book ai didi

r - ggplot2:添加线和点显示手段(stat_summary)

转载 作者:行者123 更新时间:2023-12-02 15:20:17 24 4
gpt4 key购买 nike

所以我正在使用这个数据框:

xym <- data.frame(
Var1 = c("vloga", "odločitve", "dolgoročno",
"krizno", "uživa v", "vloga", "odločitve",
"dolgoročno", "krizno", "uživa v", "vloga",
"odločitve","dolgoročno", "krizno", "uživa v",
"vloga","odločitve", "dolgoročno", "krizno",
"uživa v"),
Var2 = c("Nad","Nad", "Nad", "Nad", "Nad", "Pod",
"Pod", "Pod", "Pod", "Pod", "Enak","Enak",
"Enak", "Enak", "Enak", "Sam.", "Sam.", "Sam.",
"Sam.", "Sam."),
value = c(4, 3, 4, 4, 3, 3, 3, 2, 3, 3, 3, 2.5, 2.5,
2, 3.5 ,5 ,6 ,6 ,5 ,6))

使用这段代码:

p <- ggplot(xym, aes(x = Var1, y = value, fill = Var2)) + coord_flip()+
theme_bw() + scale_fill_manual(values = c("yellow", "deepskyblue1", "yellowgreen","orchid4")) + xlim(rev(levels(xym$Var1)))+ theme(axis.title=element_blank(),axis.ticks.y=element_blank(),legend.position = "bottom",
axis.text.x = element_text(angle = 0,vjust = 0.4)) +
geom_bar(stat = "identity", width = 0.7, position = position_dodge(width=0.7)) +
geom_text(aes(x = Var1, y =max(value), label = round(value, 2), fill = Var2),
angle = 0, position = position_dodge(width = 0.7), size = 4.2)
p + labs(fill="")

p + stat_summary(fun.y=mean, colour="red", geom="line", aes(group = 1))

我产生输出:

enter image description here

但在红线旁边,它按问题标记总平均值(即“dolgoročno”、“krizno”等),我想加分并在旁边各个问题组的条形图和标签均值

我的输出应该类似于下图(我用油漆画的),其中黑点代表我想要的点,第一个点的值 3.6 是 (6,2,4,2.5) 的平均值并代表我想要的值(value)标签。

enter image description here

我也看过:

Plot average line in a facet_wrap

ggplot2: line connecting the means of grouped data

How to label graph with the mean of the values using ggplot2

最佳答案

一个选项如下。我按照您的代码添加了几行。

# Your code
p <- ggplot(xym, aes(x = Var1, y = value, fill = Var2)) +
coord_flip() +
theme_bw() +
scale_fill_manual(values = c("yellow", "deepskyblue1", "yellowgreen","orchid4")) +
xlim(rev(levels(xym$Var1))) +
theme(axis.title = element_blank(),
axis.ticks.y = element_blank(),
legend.position = "bottom",
axis.text.x = element_text(angle = 0,vjust = 0.4)) +
geom_bar(stat = "identity", width = 0.7, position = position_dodge(width = 0.7)) +
geom_text(aes(x = Var1, y = max(value), label = round(value, 2), fill = Var2),
angle = 0, position = position_dodge(width = 0.7), size = 4.2)

p + labs(fill = "")

然后,我添加了以下代码。您可以在 stat_summary 中添加点更改 geom 到 point。对于标签,我选择从 ggplot_build() 获取数据并创建一个名为 foo 的数据框。 (我认为还有其他方法可以完成相同的工作。)使用 foo,我在最后添加了注释。

p2 <- p +
stat_summary(fun.y = mean, color = "red", geom = "line", aes(group = 1)) +
stat_summary(fun.y = mean, color = "black", geom ="point", aes(group = 1), size = 5,
show.legend = FALSE)

# This is the data for your dots in the graph
foo <- as.data.frame(ggplot_build(p2)$data[[4]])

p2 +
annotate("text", x = foo$x, y = foo$y + 0.5, color = "black", label = foo$y)

enter image description here

关于r - ggplot2:添加线和点显示手段(stat_summary),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37357188/

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