【问题标题】:Line Plot with standard deviation带标准差的线图
【发布时间】:2020-06-06 18:07:21
【问题描述】:

我想创建一个折线图,其中有两条线显示每条线的标准偏差。目前,我有一个折线图,它显示了两条线。 我的代码是这样的,类别是 x 轴的名称,结果 1/2 是结果,SD 1/2 是标准差。

categories <-c("Traditionsbewusst / Heimatverbunden","Bekanntheit
+ ","Jugendlich / Modern
+ ","Professionell
+ ","Sozial engagiert
+ ","Aufstrebend / Motiviert
+ ","Umwelt / Nachhaltigkeit
+ ","Sympathisch
+ ","Familienfreundlich
+ ","Mitreißend
+ ","Posetives Image
+ ","Teamgeist
+ ","Inovativ
+ ")


Result1<-c(2.34,1.76,2.66,2.85,2.45,2.66,2.64,2.89,2.61,2.80,2.94,2.72,2.82)
Result2<-c(2.08,1.29,2.41,2.39,2.11,2.08,2.34,2.25,2.19,2.24,2.58,2.19,2.42)

SD1<-c(0.89,0.93,0.85,0.92,0.78,0.86,0.86,1.01,0.83,0.86,0.92,0.90,0.97)
SD2<-c(0.96,0.71,0.80,0.85,0.89,1.00,0.76,0.94,0.87,0.93,0.94,0.95,0.85)

par(mar = c(15, 3, 3, 3))
plot(Result1,type = "b",main = "Profil Image",xlab = "",ylab = "Bewertung",axes =FALSE,ylim = c(1,4))
axis(1,at=1:13,labels = categories,las=2,cex.axis=0.8)
lines(Result2,type = "b")
axis(2)

【问题讨论】:

  • 题外话:我认为有一个拼写错误:“posetives Image”应该是“positives Image”,而“Inovativ”应该是“Innovativ”。
  • 请再次解释您想要的输出。我不明白你在做什么。
  • 没问题,我想添加标准误差线
  • 您想要两个线图都在一个图表中,并且两个线图都带有误差线?

标签: r line linechart standard-deviation


【解决方案1】:

StupidWolf 已经回答了您的问题,但我想向您展示另一种使用ggplot2tidyrdplyr 绘制数据的方法。它们都包含在包tidyverse中。

首先我们需要创建一个data.frame:

df <- data.frame(categories, Result1, Result2, SD1, SD2)
                            categories Result1 Result2  SD1  SD2
1  Traditionsbewusst / Heimatverbunden    2.34    2.08 0.89 0.96
2                          Bekanntheit    1.76    1.29 0.93 0.71
3                  Jugendlich / Modern    2.66    2.41 0.85 0.80
4                        Professionell    2.85    2.39 0.92 0.85
5                     Sozial engagiert    2.45    2.11 0.78 0.89

现在我们需要稍微整理一下数据,把它变成“长”格式:

df %<>% 
  pivot_longer(cols=starts_with("Result"), names_to="Group", names_prefix="Result", values_to="Result") %>%
  pivot_longer(cols=starts_with("SD"), names_to="SD_Group", names_prefix="SD", values_to="SD") %>%
  filter(Group == SD_Group) %>%
  select(-SD_Group)

# A tibble: 26 x 4
   categories                          Group Result    SD
   <chr>                               <chr>  <dbl> <dbl>
 1 Traditionsbewusst / Heimatverbunden 1       2.34  0.89
 2 Traditionsbewusst / Heimatverbunden 2       2.08  0.96
 3 Bekanntheit                         1       1.76  0.93
 4 Bekanntheit                         2       1.29  0.71
 5 Jugendlich / Modern                 1       2.66  0.85

现在有一个变量Group,用于分隔 Result1、SD1 和 Result2、SD2。 Result 和 SD 的值显示在 ResultSD 列中。这种演示文稿中的数据通常更容易处理。

现在我们使用ggplot 来创建绘图。 ggplot 以复杂的语法为代价提供了许多绘制数据的可能性。

ggplot(df, aes(x=categories, y=Result, group=Group, color=Group)) +
  geom_line() + 
  geom_errorbar(aes(ymin=Result-SD, ymax=Result+SD)) +
  geom_point() + 
  theme(axis.text.x = element_text(angle = 65, vjust = 1, hjust = 1), 
        plot.title = element_text(hjust = 0.5),
        strip.text.x = element_blank()) + 
  ylim(c(0,4.5)) +
  labs(title="Profil Image", 
       x=NULL, 
       y="Bewertung",
       color="Gruppe") +
  facet_wrap(~Group, labeller=labeller(Group=paste0("Gruppe ", 1:2)))

给了

【讨论】:

  • 非常感谢。这一定是命运,因为我正在尝试更多地使用 ggplot。
【解决方案2】:

您可以使用箭头,see this post too。如果你把它们放在同一张图上,它看起来真的很糟糕:

par(mar = c(15, 3, 3, 3))
plot(Result1,type = "b",main = "Profil Image",xlab = "",ylab = "Bewertung",axes =FALSE,ylim = c(0,4))
arrows(x0=1:length(SD1), y0=Result1-SD1, x1=1:length(SD1),code=3,y1=Result1+SD1,angle=90, length=0.05)
arrows(x0=1:length(SD2), y0=Result2-SD2, x1=1:length(SD2),code=3,y1=Result2+SD2,angle=90, length=0.05)
axis(1,at=1:13,labels = categories,las=2,cex.axis=0.8)
lines(Result2,type = "b")
axis(2)

也许把它们并排放置:

par(mfrow=c(1,2))
par(mar = c(15, 3, 3, 3))
plot(Result1,type = "b",main = "Profil Image",xlab = "",ylab = "Bewertung",axes =FALSE,ylim = c(0,4))
arrows(x0=1:length(SD1), y0=Result1-SD1, x1=1:length(SD1),code=3,y1=Result1+SD1,angle=90, length=0.05)
axis(1,at=1:13,labels = categories,las=2,cex.axis=0.8)
axis(2)

plot(Result2,type = "b",main = "Profil Image",xlab = "",ylab = "Bewertung",axes =FALSE,ylim = c(0,4))
arrows(x0=1:length(SD2), y0=Result2-SD2, x1=1:length(SD2),code=3,y1=Result2+SD2,angle=90, length=0.05)
axis(1,at=1:13,labels = categories,las=2,cex.axis=0.8)
axis(2)

【讨论】:

  • 感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2015-03-30
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
相关资源
最近更新 更多