英文:
Creating a trendline on a bar plot with ggplot2 in R
问题
我试图创建一个带有实心直线趋势线的条形图,但无法让趋势线显示出来。
我正在使用ggplot2包,以下是我的条形图的当前代码:
library(ggplot2)
ggplot(mydf, aes(x=round, y=means, fill=round)) +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2) +
geom_bar(stat="identity", color="black", position=position_dodge()) +
geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
theme_classic() +
xlab("Round") +
ylab("Bead Mass (g)") +
ggtitle("Verbal") +
theme(legend.position="none") +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2)
我不确定为什么geom_smooth()
部分不起作用。有什么想法吗?查看输出的图形以获取效果:
英文:
I am trying to create a bar plot with a solid straight trendline but can't get the trendline to appear.
I am using the ggplot2 package and here is my current code for the bar plot:
library(ggplot2)
ggplot(mydf, aes(x=round, y=means, fill=round)) +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2) +
geom_bar(stat="identity", color="black", position=position_dodge()) +
geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
theme_classic() +
xlab("Round") +
ylab("Bead Mass (g)") +
ggtitle("Verbal") +
theme(legend.position="none") +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2)
I'm not sure why the geom_smooth() section is not working. Any ideas?
See graph for what the output looks like
答案1
得分: 0
以下是翻译好的内容:
两个因素阻止平滑图的绘制,第一个是x轴似乎是一个因子,因此需要将其转换为数值变量,第二个是ggplot会自动将来自因子的不同填充视为不同的组,因此需要覆盖平滑层,如下所示:
library(ggplot2)
# 与示例图类似的示例数据
mydf <- data.frame(round=factor(1:5), means=c(102, 75, 73, 95, 120), se=15)
ggplot(mydf, aes(x=as.numeric(round), y=means, fill=round)) +
geom_bar(stat="identity", color="black", position=position_dodge()) +
geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9)) +
theme_classic() +
xlab("Round") +
ylab("Bead Mass (g)") +
ggtitle("Verbal") +
theme(legend.position="none") +
geom_smooth(aes(fill=NULL), method=lm, se=FALSE, col="black", linewidth=2)
英文:
Two things stop the smooth being plotted, the first is that the x axis appears to be a factor, so it needs to be converted to a numeric variabke, the second is that ggplot will automatically treat different fill (from a factor) as separate groups, so this needs to be overwritten for the smooth layer, giving:
library(ggplot2)
#Example data similar to the plot shown
mydf <- data.frame(round=factor(1:5), means=c(102, 75, 73, 95, 120), se=15)
ggplot(mydf, aes(x=as.numeric(round), y=means, fill=round)) +
geom_bar(stat="identity", color="black", position=position_dodge()) +
geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
theme_classic() +
xlab("Round") +
ylab("Bead Mass (g)") +
ggtitle("Verbal") +
theme(legend.position="none") +
geom_smooth(aes(fill=NULL), method=lm, se=FALSE, col="black", linewidth=2)
答案2
得分: 0
您的绘图中的离散填充比例尺显示,您用于 x
和 fill
的 round
变量不是 numeric
,这会导致 geom_smooth
无法正常工作。
问题的重现
library(dplyr)
library(ggplot2)
mydf <- iris %>%
mutate(round = as.character(as.numeric(as.factor(Species))) %>%
group_by(round) %>%
summarise(means = mean(Sepal.Length),
se = sd(Sepal.Length)/sqrt(n())) %>%
ungroup()
mydf
# A tibble: 3 x 3
round means se
<chr> <dbl> <dbl>
1 1 5.01 0.0498
2 2 5.94 0.0730
3 3 6.59 0.0899
ggplot(mydf, aes(x=round, y=means, fill=round)) +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2) +
geom_bar(stat="identity", color="black", position=position_dodge()) +
geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
theme_classic() +
xlab("Round") +
ylab("Bead Mass (g)") +
ggtitle("Verbal") +
theme(legend.position="none") +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2)
解决方案
- 在全局
aes
中将x
变量转换为numeric
,并移除fill
美学 - 仅在
geom_bar
的aes
中指定fill
美学
ggplot(mydf, aes(x=as.numeric(round), y=means)) +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2) +
geom_bar(aes(fill = round), stat="identity", color="black", position=position_dodge()) +
geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
theme_classic() +
xlab("Round") +
ylab("Bead Mass (g)") +
ggtitle("Verbal") +
theme(legend.position="none") +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2)
英文:
The discrete fill scale of your plot shows that the round
variable you use for x
and fill
is not numeric
, preventing geom_smooth
from working properly.
Reproduction of the issue
library(dplyr)
library(ggplot2)
mydf <- iris %>%
mutate(round = as.character(as.numeric(as.factor(Species)))) %>%
group_by(round) %>%
summarise(means = mean(Sepal.Length),
se = sd(Sepal.Length)/sqrt(n())) %>%
ungroup()
mydf
# A tibble: 3 x 3
round means se
<chr> <dbl> <dbl>
1 1 5.01 0.0498
2 2 5.94 0.0730
3 3 6.59 0.0899
ggplot(mydf, aes(x=round, y=means, fill=round)) +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2) +
geom_bar(stat="identity", color="black", position=position_dodge()) +
geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
theme_classic() +
xlab("Round") +
ylab("Bead Mass (g)") +
ggtitle("Verbal") +
theme(legend.position="none") +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2)
Solution
- Convert the
x
variable tonumeric
in the globalaes
and remove the fill aesthetics - Specify the
fill
aestethics only in thegeom_bar
aes
ggplot(mydf, aes(x=as.numeric(round), y=means)) +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2) +
geom_bar(aes(fill = round), stat="identity", color="black", position=position_dodge()) +
geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
theme_classic() +
xlab("Round") +
ylab("Bead Mass (g)") +
ggtitle("Verbal") +
theme(legend.position="none") +
geom_smooth(method=lm, se=FALSE, col="black", linewidth=2)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论