在R中使用ggplot2创建条形图上的趋势线。

huangapple go评论64阅读模式
英文:

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() 部分不起作用。有什么想法吗?查看输出的图形以获取效果:
在R中使用ggplot2创建条形图上的趋势线。

英文:

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
在R中使用ggplot2创建条形图上的趋势线。

答案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)

在R中使用ggplot2创建条形图上的趋势线。

英文:

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 &lt;- 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=&quot;identity&quot;, color=&quot;black&quot;, position=position_dodge()) + 
  geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
  theme_classic() + 
  xlab(&quot;Round&quot;) + 
  ylab(&quot;Bead Mass (g)&quot;) + 
  ggtitle(&quot;Verbal&quot;) + 
  theme(legend.position=&quot;none&quot;) + 
  geom_smooth(aes(fill=NULL),  method=lm, se=FALSE, col=&quot;black&quot;, linewidth=2)

在R中使用ggplot2创建条形图上的趋势线。

答案2

得分: 0

您的绘图中的离散填充比例尺显示,您用于 xfillround 变量不是 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_baraes 中指定 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)

在R中使用ggplot2创建条形图上的趋势线。

英文:

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 &lt;- iris %&gt;%
  mutate(round = as.character(as.numeric(as.factor(Species)))) %&gt;%
  group_by(round) %&gt;% 
  summarise(means = mean(Sepal.Length),
            se = sd(Sepal.Length)/sqrt(n())) %&gt;%
  ungroup()

mydf
# A tibble: 3 x 3
  round means     se
  &lt;chr&gt; &lt;dbl&gt;  &lt;dbl&gt;
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=&quot;black&quot;, linewidth=2) + 
  geom_bar(stat=&quot;identity&quot;, color=&quot;black&quot;, position=position_dodge()) + 
  geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
  theme_classic() + 
  xlab(&quot;Round&quot;) + 
  ylab(&quot;Bead Mass (g)&quot;) + 
  ggtitle(&quot;Verbal&quot;) + 
  theme(legend.position=&quot;none&quot;) + 
  geom_smooth(method=lm, se=FALSE, col=&quot;black&quot;, linewidth=2)

在R中使用ggplot2创建条形图上的趋势线。

Solution

  • Convert the x variable to numeric in the global aes and remove the fill aesthetics
  • Specify the fill aestethics only in the geom_bar aes
ggplot(mydf, aes(x=as.numeric(round), y=means)) +
  geom_smooth(method=lm, se=FALSE, col=&quot;black&quot;, linewidth=2) + 
  geom_bar(aes(fill = round), stat=&quot;identity&quot;, color=&quot;black&quot;, position=position_dodge()) + 
  geom_errorbar(aes(ymin=means-se, ymax=means+se), width=.2, position=position_dodge(.9),) +
  theme_classic() + 
  xlab(&quot;Round&quot;) + 
  ylab(&quot;Bead Mass (g)&quot;) + 
  ggtitle(&quot;Verbal&quot;) + 
  theme(legend.position=&quot;none&quot;) + 
  geom_smooth(method=lm, se=FALSE, col=&quot;black&quot;, linewidth=2)

在R中使用ggplot2创建条形图上的趋势线。

huangapple
  • 本文由 发表于 2023年3月7日 16:51:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定