【问题标题】:GGPLOT : How to add 2nd Y axis Labels for Mean and Standard Deviation using geom_line, geom_hline, annotateGGPLOT:如何使用 geom_line、geom_hline、annotate 为平均值和标准偏差添加第二个 Y 轴标签
【发布时间】: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() 函数并针对我的用例进行定制吗?

标签: r ggplot2 annotate


【解决方案1】:

你在正确的轨道上。利用“辅助轴技巧”是实现所需结果的好选择。此外,您可以通过将hlines 的信息和标签放在数据框中来大大简化代码:

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)

df_hline <- data.frame(
  y = c(Mean, Mean + c(1, -1, 2, -2) * SD),
  label = c("Mean : ", "+1*SD : ", "-1*SD : ", "+2*SD : ", "-2*SD : "),
  linetype = c("solid", "dashed", "dashed", "dotted", "dotted")
)
df_hline$label <- paste0(df_hline$label, round(df_hline$y, 1))

library(ggplot2)

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)) + 
  geom_hline(data = df_hline, aes(yintercept = y, linetype = linetype), color = "black", size=1) +
  scale_y_continuous(sec.axis = dup_axis(breaks = df_hline$y, labels = df_hline$label), limits = c(14, 21)) +
  scale_linetype_identity() +
  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))

编辑如果您想设置线的颜色,您可以通过将颜色添加到数据框df_hline 来实现。要使这项工作同时在color aes 上映射Country,您可以通过scale_color_manual 设置颜色,这需要为Country 定义调色板。在下面的代码中,我只是使用通过scales::hue_pal 提供的默认ggplot2 调色板。我在这个调色板中添加了redblack。此外,为了防止 hline 出现在图例中,我使用了 breaks 参数。

df_hline <- data.frame(
  y = c(Mean, Mean + c(1, -1, 2, -2) * SD),
  label = c("Mean : ", "+1*SD : ", "-1*SD : ", "+2*SD : ", "-2*SD : "),
  linetype = c("solid", "dashed", "dashed", "dotted", "dotted"),
  color = c("black", "red", "red", "red", "red")
)
df_hline$label <- paste0(df_hline$label, round(df_hline$y, 1))

pal_country <- scales::hue_pal()(length(unique(myData$Country)))
pal_country <- setNames(pal_country, unique(myData$Country))

ggplot(myData, aes(x = Index, y = Concentration, group = Country)) +
  facet_grid(. ~ Month, scales = "free", space = "free") +
  geom_hline(data = df_hline, aes(yintercept = y, linetype = linetype, color = color), size = 1) +
  geom_line(aes(color = Country)) +
  geom_point(aes(color = Country)) +
  scale_y_continuous(sec.axis = dup_axis(breaks = df_hline$y, labels = df_hline$label), limits = c(14, 21)) +
  scale_linetype_identity() +
  scale_color_manual(values = c(pal_country, red = "red", black = "black"), breaks = names(pal_country)) +
  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)
  )

数据

structure(list(Index = 1:10, Country = c("USA", "USA", "USA", 
"USA", "USA", "USA", "USA", "USA", "USA", "USA"), Month = c("April", 
"April", "April", "April", "April", "May", "May", "May", "May", 
"May"), Concentration = c(17.1094, 16.001, 16.6342, 17.0215, 
16.6438, 18.5031, 19.2239, 18.3213, 19.8904, 19.4935)), row.names = c(NA, 
-10L), class = "data.frame")

【讨论】:

  • 非常感谢。它有效,但是,您是否注意到该图应该在平均值处有 1 条实线,在 +1*SD 和 -1*SD 处有 2 条虚线。取而代之的是,该图有 1 条虚线和 2 条实线
  • 噢。是的。我忘了添加 scale_linetype_identity()。
  • 太棒了,谢谢。我在脚本中添加了 scale_linetype_identity() +,它现在可以工作了
  • 是的。当然,我们可以有不同的颜色。我刚刚进行了编辑,向您展示了一种添加颜色的方法。
  • 我发现我可以只使用外行方法,即直接复制并过去希腊字母标签 μ 和 σ 代替我的 r 代码中的平均值和 SD,它可以工作标签 = = c( “μ:”,“+1σ:”,“-1σ:”,“+2σ:”,“-2σ:”)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
  • 2023-02-11
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多