GGPLOT:如何使用 geom_line、geom_hline、annotate 为均值和标准差添加第二个 Y 轴标签

问题描述

尊敬的 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,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,linetype="dashed",y=Mean+SD+0.2,label=paste("+1*SD : ",round(Mean+SD,fontface="bold") +

### Mean - (1 * SD)
geom_hline(yintercept=Mean-SD,y=Mean-SD-0.2,label=paste("-1*SD : ",round(Mean-SD,fontface="bold") +

### Mean + (2 * SD)
geom_hline(yintercept=Mean+(2*SD),linetype="dotted",y=Mean+(2*SD)+0.2,label=paste("+2*SD : ",round(Mean+(2*SD),fontface="bold") +

### Mean - (2 * SD)
geom_hline(yintercept=Mean-(2*SD),y=Mean-(2*SD)-0.2,label=paste("-2*SD : ",round(Mean-(2*SD),fontface="bold") + 

theme(axis.title.y = element_text(size=14,face="bold",colour = "black"),axis.text.y = element_text(size=10,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

My_Current_PLot

My_Desired_Plot

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)