GGPLOT:如何使用 geom_line、geom_hline、注释为平均值和标准差添加第二个 Y 轴标签
GGPLOT : How to add 2nd Y axis Labels for Mean and Standard Deviation using geom_line, geom_hline, annotate
尊敬的 Whosebug 领导,
我使用 geom_line 绘制了一个线图,并且我在 Y 轴的左侧设置了浓度。我还使用 geom_hline() 和 annotate() 函数将数据的均值和标准差添加为水平线和文本。
如何更改我的 R 代码,使平均值和标准偏差值的实际文本显示在 Y 轴的右侧?任何 advice/help 表示赞赏。
我的数据:
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,请指教如何change/improve代码创建My_Desired_Plot
你走在正确的轨道上。利用“辅助轴技巧”是实现所需结果的好选择。此外,您可以通过将 hline
的信息和数据框中的标签的信息大大简化您的代码:
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))
编辑 如果你想设置 hlines 的颜色,你可以通过将颜色添加到数据框 df_hline
来实现。要在 color
aes 上映射 Country
的同时进行这项工作,您可以通过 scale_color_manual
设置颜色,这需要为 Country
定义一个调色板。在下面的代码中,我简单地使用了通过 scales::hue_pal
提供的默认 ggplot2 调色板。在这个调色板中,我添加了 red
和 black
。此外,为了防止 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")
尊敬的 Whosebug 领导,
我使用 geom_line 绘制了一个线图,并且我在 Y 轴的左侧设置了浓度。我还使用 geom_hline() 和 annotate() 函数将数据的均值和标准差添加为水平线和文本。
如何更改我的 R 代码,使平均值和标准偏差值的实际文本显示在 Y 轴的右侧?任何 advice/help 表示赞赏。
我的数据:
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,请指教如何change/improve代码创建My_Desired_Plot
你走在正确的轨道上。利用“辅助轴技巧”是实现所需结果的好选择。此外,您可以通过将 hline
的信息和数据框中的标签的信息大大简化您的代码:
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))
编辑 如果你想设置 hlines 的颜色,你可以通过将颜色添加到数据框 df_hline
来实现。要在 color
aes 上映射 Country
的同时进行这项工作,您可以通过 scale_color_manual
设置颜色,这需要为 Country
定义一个调色板。在下面的代码中,我简单地使用了通过 scales::hue_pal
提供的默认 ggplot2 调色板。在这个调色板中,我添加了 red
和 black
。此外,为了防止 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")