添加没有相应 x 数据的第二个 y 轴
Add second y-axis without corresponding x-data
我正在对课程评估进行图形分析。
我得到以下数据:
> str(dataJ2)
'data.frame': 16 obs. of 22 variables:
...
$ lk_nummer : Factor w/ 111 levels "051-0311-00S",..: 19 30 38 47 49 50 51 55 56 59 ...
$ le_titel : Factor w/ 111 levels "","Advanced Methods and Strategies in Synthesis",..: 6 99 75 82 84 8 40 39 38 68 ...
$ anzahl_stud : int 7 79 1 34 10 20 83 10 4 11 ...
$ durchschnitt : num 4.61 5.35 3.5 4.4 4.4 4.33 4.49 4.53 5.38 4.48 ...
$ standardabweich : num 0.4 0.54 0 1.02 1.21 0.62 1.17 0.9 0.28 0.68 ...
...
$ prozent_best : num 85.7 97.5 0 70.6 90 80 73.5 90 100 81.8 ...
...
使用 ggplot2 我可以绘制如下图:
plotJ2 <- ggplot(dataJ2, aes(y=durchschnitt,x=le_titel))
plotJ2 + geom_bar(position=position_dodge(), stat="identity", fill = I("chartreuse4")) +
scale_y_continuous(limits=c(0,6.6),breaks=seq(from=1, to=6, by=1)) +
geom_errorbar(aes(ymin=durchschnitt-standardabweich, ymax=durchschnitt+standardabweich), width=.1) +
ggtitle("2. Jahr Bsc Biologie") +
ylab("Durchschnitt") + xlab("Fächer") +
geom_text(aes(label = durchschnitt, y = 1.8), size = 4, colour="gray85") +
geom_text(aes(label = anzahl_stud, y = 0.2), size = 4, colour="grey85") +
geom_text(aes(label = prozent_best, y = 6.55), size = 4, colour="chartreuse4", adj=1) +
geom_text(aes(label = "%", y = 6.6), size = 4, colour="chartreuse4", adj=0) +
coord_flip()
但是,图形部分的"prozent_best"看起来不太好。
我尝试用 mtext、text 和 facet_wrap 添加来自 [=31 的数据=] 作为灰色图形部分右侧的第二个 y 轴标签,但无法使其工作。
有什么建议吗?
Useful translations/descriptions of the data annotation:
lk_nummer -> number of the lectures
le_titel -> name of the lectures
anzahl_stud -> number of students
durchschnitt -> average
prozent_best -> number of students which passed the exam in percent
Fächer -> classes
尝试:
geom_text(aes(label = paste0(prozent_best,'%'), y = 6.55),
size = 4, colour="chartreuse4", hjust='right')
这会将“%”符号与值组合成一个字符串。一般来说,我会建议在 ggplot 调用之外生成标签向量,但为此它不会增加太多混乱。
此外,您可能想考虑添加 scale_x_continuous(expand=0,limits=c(0,7))
。这将摆脱左侧丑陋的灰色条。
也可以尝试添加 theme_bw()
因为你的情节已经很忙了,ggplots 标准主题背景中的灰色块只会让它看起来糊涂。
我正在对课程评估进行图形分析。 我得到以下数据:
> str(dataJ2)
'data.frame': 16 obs. of 22 variables:
...
$ lk_nummer : Factor w/ 111 levels "051-0311-00S",..: 19 30 38 47 49 50 51 55 56 59 ...
$ le_titel : Factor w/ 111 levels "","Advanced Methods and Strategies in Synthesis",..: 6 99 75 82 84 8 40 39 38 68 ...
$ anzahl_stud : int 7 79 1 34 10 20 83 10 4 11 ...
$ durchschnitt : num 4.61 5.35 3.5 4.4 4.4 4.33 4.49 4.53 5.38 4.48 ...
$ standardabweich : num 0.4 0.54 0 1.02 1.21 0.62 1.17 0.9 0.28 0.68 ...
...
$ prozent_best : num 85.7 97.5 0 70.6 90 80 73.5 90 100 81.8 ...
...
使用 ggplot2 我可以绘制如下图:
plotJ2 <- ggplot(dataJ2, aes(y=durchschnitt,x=le_titel))
plotJ2 + geom_bar(position=position_dodge(), stat="identity", fill = I("chartreuse4")) +
scale_y_continuous(limits=c(0,6.6),breaks=seq(from=1, to=6, by=1)) +
geom_errorbar(aes(ymin=durchschnitt-standardabweich, ymax=durchschnitt+standardabweich), width=.1) +
ggtitle("2. Jahr Bsc Biologie") +
ylab("Durchschnitt") + xlab("Fächer") +
geom_text(aes(label = durchschnitt, y = 1.8), size = 4, colour="gray85") +
geom_text(aes(label = anzahl_stud, y = 0.2), size = 4, colour="grey85") +
geom_text(aes(label = prozent_best, y = 6.55), size = 4, colour="chartreuse4", adj=1) +
geom_text(aes(label = "%", y = 6.6), size = 4, colour="chartreuse4", adj=0) +
coord_flip()
但是,图形部分的"prozent_best"看起来不太好。 我尝试用 mtext、text 和 facet_wrap 添加来自 [=31 的数据=] 作为灰色图形部分右侧的第二个 y 轴标签,但无法使其工作。
有什么建议吗?
Useful translations/descriptions of the data annotation:
lk_nummer -> number of the lectures
le_titel -> name of the lectures
anzahl_stud -> number of students
durchschnitt -> average
prozent_best -> number of students which passed the exam in percent
Fächer -> classes
尝试:
geom_text(aes(label = paste0(prozent_best,'%'), y = 6.55),
size = 4, colour="chartreuse4", hjust='right')
这会将“%”符号与值组合成一个字符串。一般来说,我会建议在 ggplot 调用之外生成标签向量,但为此它不会增加太多混乱。
此外,您可能想考虑添加 scale_x_continuous(expand=0,limits=c(0,7))
。这将摆脱左侧丑陋的灰色条。
也可以尝试添加 theme_bw()
因为你的情节已经很忙了,ggplots 标准主题背景中的灰色块只会让它看起来糊涂。