带标准差的线图
Line Plot with standard deviation
我想创建一个折线图,用两条线显示每条线的标准差。目前,我有一个折线图,显示两条线。
我的代码是这样的,类别是 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)
您可以使用箭头,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)
StupidWolf 已经回答了您的问题,但我想向您展示一种使用 ggplot2
、tidyr
和 dplyr
绘制数据的替代方法。它们都包含在包中 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
现在我们需要稍微整理一下数据,将其转换为 "long" 格式:
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 分开。结果和 SD 的值显示在 Result
和 SD
列中。这种表示形式的数据通常更容易处理。
现在我们正在使用 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)))
给予
我想创建一个折线图,用两条线显示每条线的标准差。目前,我有一个折线图,显示两条线。 我的代码是这样的,类别是 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)
您可以使用箭头,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)
StupidWolf 已经回答了您的问题,但我想向您展示一种使用 ggplot2
、tidyr
和 dplyr
绘制数据的替代方法。它们都包含在包中 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
现在我们需要稍微整理一下数据,将其转换为 "long" 格式:
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 分开。结果和 SD 的值显示在 Result
和 SD
列中。这种表示形式的数据通常更容易处理。
现在我们正在使用 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)))
给予