【发布时间】:2021-07-16 18:35:45
【问题描述】:
尊敬的 StackOverFlow 领导者,
我使用 geom_line 绘制了一个线图,并且我在 Y 轴的左侧有浓度。我还使用 geom_hline() 和 annotate() 函数将数据的平均偏差和标准偏差添加为水平线和文本。
如何更改我的 R 代码以使平均值和标准偏差值的实际文本出现在 Y 轴的右侧?任何建议/帮助表示赞赏。
我的数据:
Index Country Month Concentration
1 USA April 17.1094
2 USA April 16.001
3 USA April 16.6342
4 USA April 17.0215
5 USA April 16.6438
6 USA May 18.5031
7 USA May 19.2239
8 USA May 18.3213
9 USA May 19.8904
10 USA May 19.4935
我的脚本:
myData <- data.frame(as.matrix(read.table( "Demo_Data.txt", sep="\t", header=TRUE )))
myData$Concentration <- as.numeric(as.character(myData$Concentration))
Mean <- round(mean(myData$Concentration, na.rm = TRUE),2)
SD <- round(sd(myData$Concentration, na.rm = TRUE),2)
ggplot(myData, aes(x = Index, y = Concentration, group=Country)) +
facet_grid(. ~Month, scales = "free", space = "free") +
geom_line(aes(color=Country)) +
geom_point(aes(color=Country)) +
### Mean
geom_hline(yintercept=Mean, color = "black", size=1) +
annotate(geom="text", x=5, y=Mean+0.2, label=paste("Mean : ", round(Mean,1), sep=""), color="black", fontface="bold") +
### Mean + (1 * SD)
geom_hline(yintercept=Mean+SD, color = "black", linetype="dashed", size=1) +
annotate(geom="text", x=5, y=Mean+SD+0.2, label=paste("+1*SD : ", round(Mean+SD,1), sep=""), color="black", fontface="bold") +
### Mean - (1 * SD)
geom_hline(yintercept=Mean-SD, color = "black", linetype="dashed", size=1) +
annotate(geom="text", x=5, y=Mean-SD-0.2, label=paste("-1*SD : ", round(Mean-SD,1), sep=""), color="black", fontface="bold") +
### Mean + (2 * SD)
geom_hline(yintercept=Mean+(2*SD), color = "black", linetype="dotted", size=1) +
annotate(geom="text", x=5, y=Mean+(2*SD)+0.2, label=paste("+2*SD : ", round(Mean+(2*SD),1), sep=""), color="black", fontface="bold") +
### Mean - (2 * SD)
geom_hline(yintercept=Mean-(2*SD), color = "black", linetype="dotted", size=1) +
annotate(geom="text", x=5, y=Mean-(2*SD)-0.2, label=paste("-2*SD : ", round(Mean-(2*SD),1), sep=""), color="black", fontface="bold") +
theme(axis.title.y = element_text(size=14, face="bold", colour = "black"),
axis.text.y = element_text(size=10, face="bold", colour = "black"),
strip.text = element_text(face = "bold", size=14)) +
ylim(14,21)
我已经探索了 sec.axis() 函数,但这可能不适合这种情况。
My_Current_Plot 和 My_Desired_Plot 都张贴在这里。以上 R 代码生成 My_Current_Plot,请告知如何更改/改进代码以创建 My_Desired_Plot
【问题讨论】:
-
我可以调整 sec.axis() 函数并针对我的用例进行定制吗?