在 raincloud plot ggplot2 上创建自定义图例
Create custom legend on raincloud plot ggplot2
我有一个带有传说的雨云情节。
###DATA##
rain_plot_data = d13C,lengthcat,study
-18.24,1,s
-26.48,2,s
-25,3,s
-26.98,2,s
-23.47,3,s
-22.15,3,s
-23.19,2,s
-19.97,1,s
-24.89,1,s
-21.21,1,s
-22.44,2,s
-15.59,3,s
-22.22,3,s
-16.65,1,s
-19.51,2,s
-20.46,3,s
-21.31,3,s
-27.08,1,s
-22.28,2,s
-16.13,3,s
-18.45,3,s
-24.09,3,s
-23.95,2,s
-21,1,s
-21.43,3,s
-15.44,3,s
-17.97,1,s
-19.51,2,s
-26.4,1,s
-22.3,1,s
-20.15,2,s
-21.32,3,s
-23.47,1,s
-14.47,3,s
-13.56,2,s
-26.19,2,s
-19.27,2,s
-16.65,2,s
-18.98,2,s
-15.65,3,s
-25.16,2,s
-25.84,2,s
-14.82,1,s
-15.03,2,s
-19.54,3,s
-26.4,2,g
-26.4,2,g
-19.8,3,g
-18.5,1,g
-23.4,2,g
-21.9,1,g
-24.7,1,g
-22.9,1,g
-21.4,3,g
-23.5,2,g
-25.2,1,g
-18.5,1,g
-18.2,1,g
-22.4,1,g
-17.7,1,g
-18.7,2,g
-18.4,1,g
-17.7,1,g
-21.3,1,g
-21.3,1,g
-21.9,1,g
-18.9,3,g
-19.6,2,g
-17.8,2,g
-19.7,3,g
-20.6,3,g
-24.2,3,g
-18.1,2,g
-19.4,2,g
-20.1,2,g
-20.4,2,g
-23.2,2,g
-20.9,2,g
-21.8,2,g
-22.1,2,g
-23.5,2,g
-20.1,2,g
-21.3,2,g
-23.9,2,g
-23.5,2,g
-22.9,2,g
-25.5,2,g
-24.8,2,g
-18.3,2,g
-20.7,2,g
-20.5,2,g
-19.9,2,g
-22.4,2,g
-23.1,3,g
-21.4,3,g
-22.2,3,g
-21.3,3,g
-19.6,3,g
-22.2,3,g
-22.3,3,g
-22.9,3,g
-21,3,g
-19.2,3,g
-20.6,3,g
-23.2,3,g
-20.8,3,g
-22.1,3,g
-23.7,3,g
-19.8,3,g
-21.6,3,g
-23.9,3,g
-20.1,3,g
-20.5,3,g
-23.7,3,g
-19.5,1,g
-21.1,1,g
-18.6,1,g
-19.3,3,g
-17.8,1,g
-21.1,2,g
-17.9,1,g
-23.7,1,g
-25.8,2,g
-25.9,1,g
-27.3,2,g
-21.5,1,g
-25.1,1,g
-23.1,1,g
-18,2,g
-25.5,1,g
-25.5,2,g
-16.7,1,g
-16.6,1,g
-17.3,2,g
-23.6,2,g
-25.5,3,g
-15,2,g
-23.2,3,g
-21.1,2,g
-21.5,3,g
-23.3,2,g
-21.9,2,g
-22.7,3,g
-22.6,3,g
-16.8,2,g
-15.4,1,g
-15.4,1,g
-16.8,1,g
-16.1,1,g
-24.7,3,g
-22.6,3,g
-26.8,2,g
-21.6,2,g
-22.7,1,g
-25,1,g
-18.8,3,g
-19.5,1,g
-19.1,1,g
-23.2,3,g
-25.7,2,g
-18.6,3,g
-18.6,3,g
-17.8,1,g
-15.5,3,g
-17.1,2,g
-21.3,3,g
-18.8,2,g
-26.4,1,g
-15.4,1,g
-21.7,2,g
-14.6,1,g
-15.1,3,g
-20.2,2,g
-15.5,1,g
-20.9,3,g
-14.2,1,g
-16.7,2,g
-14.3,1,g
-19.3,1,g
-13.9,1,g
-17.9,1,g
-24.5,3,g
雨云图的来源。
source("https://raw.githubusercontent.com/RainCloudPlots/RainCloudPlots/master/tutorial_R/R_rainclouds.R")
雨云图。
my_clrs_yct <- c("grey15", "grey35", "grey55", "#407a8c", "#404f86", "#3e1451")
yct_C_rain <- ggplot(rain_plot_data, aes(y=d13C, x=lengthcat,
fill = interaction(lengthcat, study),color=interaction(lengthcat, study)),
position = position_nudge(x = 1, y =0)) +
geom_flat_violin(position = position_nudge(x = .2, y =0), alpha = .5)+
geom_point(aes(y = C, color = interaction(lengthcat, study)),
position = position_jitter(width = .05), size = 2, alpha = .5) +
geom_boxplot(width = .3, guides = FALSE, outlier.shape = NA, alpha = 0, notch = FALSE) +
stat_summary(fun= mean, geom = "point", shape = c(21,23,21,23,21,23), size = 3, fill = "black") +
scale_y_continuous (limits = c(-29,-13),expand = c(0,0),breaks=seq(-29,-13,2)) +
scale_x_discrete(labels= c("100 - 300", "301 - 475", "476 - 575"),expand = c(0.1,0.5)) +
ylab("δ13C") + xlab("Total length group") +
ggtitle("") +
scale_fill_manual(values=my_clrs_yct, label=) +
theme(panel.background=element_blank(),panel.grid = element_blank(),legend.position = "none") +
theme (axis.title.y = element_text(size = 14, margin = margin(t = 0, r = 10, b = 0, l = 0), colour = "black"),
axis.title.x = element_text(size = 14, margin = margin(t = 10, r = 0, b = 0, l = 0), colour = "black"),
text = element_text(family = "Times New Roman"),
plot.title = element_text(face = "bold", family = "Arial"),
plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"),
legend.position = "top",
legend.box = "horizontal",
legend.title = element_blank(),
axis.ticks.y = element_line(size = 0.5),
axis.ticks.x = element_line(size = 0.5),
axis.ticks.length = unit(0.2,"cm"),
axis.text.y = element_text(colour = "black", size = 14, angle = 0, vjust = 0.5, hjust = 1,
margin = margin(t = 0, r = 5, b = 0, l = 0)),
axis.text.x = element_text(colour = "black", size = 14, angle = 0, vjust = 0.7, hjust = 0.5,
margin = margin(t = 5, r = 0, b = 0, l = 0)),
axis.line = element_line(colour = "black", size = 0.5, lineend = "square"))
yct_C_rain
我得到的这个图有一个非常需要的默认图例。
但我希望图例看起来像这样:
我已尝试对 ggplot()
图例进行多种不同的自定义,但我认为雨云图的使用使我无法理解如何执行此操作。
也许您正在寻找这个
my_clrs_yct <- c("grey15", "grey35", "grey55", "#407a8c", "#404f86", "#3e1451")
my_labels <- c("100 - 300", "301 - 475", "476 - 575")
dfs <- toupper(unique(rain_plot_data$study))
mylabels <- c(paste(dfs[2],my_labels),paste(dfs[1],my_labels))
yct_C_rain <- ggplot(rain_plot_data, aes(y=d13C, x=factor(lengthcat),
fill = interaction(lengthcat, study),color=interaction(lengthcat, study)),
show.legend = FALSE,
position = position_nudge(x = 1, y =0)) +
geom_flat_violin(position = position_nudge(x = .2, y =0), alpha = .5)+
geom_point(aes(y = d13C, color = interaction(lengthcat, study)),
position = position_jitter(width = .05), size = 2, alpha = .5) +
geom_boxplot(width = .3, outlier.shape = NA, alpha = 0, notch = FALSE, show.legend = FALSE) +
stat_summary(fun= mean, geom = "point", shape = c(21,23,21,23,21,23), size = 3, fill = "black") +
scale_y_continuous(limits = c(-29,-13),expand = c(0,0),breaks=seq(-29,-13,2)) +
scale_x_discrete(labels = my_labels, expand = c(0.1,0.5) ) + #
ylab("δ13C") + xlab("Total length group") +
ggtitle("") +
scale_fill_manual(values=my_clrs_yct) +
scale_color_manual(values=my_clrs_yct, label= mylabels) +
theme(panel.background=element_blank(), panel.grid = element_blank()) +
theme(axis.title.y = element_text(size = 14, margin = margin(t = 0, r = 10, b = 0, l = 0), colour = "black"),
axis.title.x = element_text(size = 14, margin = margin(t = 10, r = 0, b = 0, l = 0), colour = "black"),
text = element_text(family = "Times New Roman"),
plot.title = element_text(face = "bold", family = "Arial"),
plot.margin = unit(c(0.9, 0.5, 0.99, 0.5), "cm"),
legend.position = "top",
legend.box = "horizontal",
legend.key = element_blank(),
legend.title = element_blank(),
axis.ticks.y = element_line(size = 0.5),
axis.ticks.x = element_line(size = 0.5),
axis.ticks.length = unit(0.2,"cm"),
axis.text.y = element_text(colour = "black", size = 12, angle = 0, vjust = 0.5, hjust = 1,
margin = margin(t = 0, r = 5, b = 0, l = 0)),
axis.text.x = element_text(colour = "black", size = 12, angle = 0, vjust = 0.7, hjust = 0.5,
margin = margin(t = 5, r = 0, b = 0, l = 0)),
axis.line = element_line(colour = "black", size = 0.5, lineend = "square")) +
guides(color = guide_legend(override.aes=list(color=my_clrs_yct, lty=0),
nrow = 2, byrow = TRUE),
fill='none')
yct_C_rain
我有一个带有传说的雨云情节。
###DATA##
rain_plot_data = d13C,lengthcat,study
-18.24,1,s
-26.48,2,s
-25,3,s
-26.98,2,s
-23.47,3,s
-22.15,3,s
-23.19,2,s
-19.97,1,s
-24.89,1,s
-21.21,1,s
-22.44,2,s
-15.59,3,s
-22.22,3,s
-16.65,1,s
-19.51,2,s
-20.46,3,s
-21.31,3,s
-27.08,1,s
-22.28,2,s
-16.13,3,s
-18.45,3,s
-24.09,3,s
-23.95,2,s
-21,1,s
-21.43,3,s
-15.44,3,s
-17.97,1,s
-19.51,2,s
-26.4,1,s
-22.3,1,s
-20.15,2,s
-21.32,3,s
-23.47,1,s
-14.47,3,s
-13.56,2,s
-26.19,2,s
-19.27,2,s
-16.65,2,s
-18.98,2,s
-15.65,3,s
-25.16,2,s
-25.84,2,s
-14.82,1,s
-15.03,2,s
-19.54,3,s
-26.4,2,g
-26.4,2,g
-19.8,3,g
-18.5,1,g
-23.4,2,g
-21.9,1,g
-24.7,1,g
-22.9,1,g
-21.4,3,g
-23.5,2,g
-25.2,1,g
-18.5,1,g
-18.2,1,g
-22.4,1,g
-17.7,1,g
-18.7,2,g
-18.4,1,g
-17.7,1,g
-21.3,1,g
-21.3,1,g
-21.9,1,g
-18.9,3,g
-19.6,2,g
-17.8,2,g
-19.7,3,g
-20.6,3,g
-24.2,3,g
-18.1,2,g
-19.4,2,g
-20.1,2,g
-20.4,2,g
-23.2,2,g
-20.9,2,g
-21.8,2,g
-22.1,2,g
-23.5,2,g
-20.1,2,g
-21.3,2,g
-23.9,2,g
-23.5,2,g
-22.9,2,g
-25.5,2,g
-24.8,2,g
-18.3,2,g
-20.7,2,g
-20.5,2,g
-19.9,2,g
-22.4,2,g
-23.1,3,g
-21.4,3,g
-22.2,3,g
-21.3,3,g
-19.6,3,g
-22.2,3,g
-22.3,3,g
-22.9,3,g
-21,3,g
-19.2,3,g
-20.6,3,g
-23.2,3,g
-20.8,3,g
-22.1,3,g
-23.7,3,g
-19.8,3,g
-21.6,3,g
-23.9,3,g
-20.1,3,g
-20.5,3,g
-23.7,3,g
-19.5,1,g
-21.1,1,g
-18.6,1,g
-19.3,3,g
-17.8,1,g
-21.1,2,g
-17.9,1,g
-23.7,1,g
-25.8,2,g
-25.9,1,g
-27.3,2,g
-21.5,1,g
-25.1,1,g
-23.1,1,g
-18,2,g
-25.5,1,g
-25.5,2,g
-16.7,1,g
-16.6,1,g
-17.3,2,g
-23.6,2,g
-25.5,3,g
-15,2,g
-23.2,3,g
-21.1,2,g
-21.5,3,g
-23.3,2,g
-21.9,2,g
-22.7,3,g
-22.6,3,g
-16.8,2,g
-15.4,1,g
-15.4,1,g
-16.8,1,g
-16.1,1,g
-24.7,3,g
-22.6,3,g
-26.8,2,g
-21.6,2,g
-22.7,1,g
-25,1,g
-18.8,3,g
-19.5,1,g
-19.1,1,g
-23.2,3,g
-25.7,2,g
-18.6,3,g
-18.6,3,g
-17.8,1,g
-15.5,3,g
-17.1,2,g
-21.3,3,g
-18.8,2,g
-26.4,1,g
-15.4,1,g
-21.7,2,g
-14.6,1,g
-15.1,3,g
-20.2,2,g
-15.5,1,g
-20.9,3,g
-14.2,1,g
-16.7,2,g
-14.3,1,g
-19.3,1,g
-13.9,1,g
-17.9,1,g
-24.5,3,g
雨云图的来源。
source("https://raw.githubusercontent.com/RainCloudPlots/RainCloudPlots/master/tutorial_R/R_rainclouds.R")
雨云图。
my_clrs_yct <- c("grey15", "grey35", "grey55", "#407a8c", "#404f86", "#3e1451")
yct_C_rain <- ggplot(rain_plot_data, aes(y=d13C, x=lengthcat,
fill = interaction(lengthcat, study),color=interaction(lengthcat, study)),
position = position_nudge(x = 1, y =0)) +
geom_flat_violin(position = position_nudge(x = .2, y =0), alpha = .5)+
geom_point(aes(y = C, color = interaction(lengthcat, study)),
position = position_jitter(width = .05), size = 2, alpha = .5) +
geom_boxplot(width = .3, guides = FALSE, outlier.shape = NA, alpha = 0, notch = FALSE) +
stat_summary(fun= mean, geom = "point", shape = c(21,23,21,23,21,23), size = 3, fill = "black") +
scale_y_continuous (limits = c(-29,-13),expand = c(0,0),breaks=seq(-29,-13,2)) +
scale_x_discrete(labels= c("100 - 300", "301 - 475", "476 - 575"),expand = c(0.1,0.5)) +
ylab("δ13C") + xlab("Total length group") +
ggtitle("") +
scale_fill_manual(values=my_clrs_yct, label=) +
theme(panel.background=element_blank(),panel.grid = element_blank(),legend.position = "none") +
theme (axis.title.y = element_text(size = 14, margin = margin(t = 0, r = 10, b = 0, l = 0), colour = "black"),
axis.title.x = element_text(size = 14, margin = margin(t = 10, r = 0, b = 0, l = 0), colour = "black"),
text = element_text(family = "Times New Roman"),
plot.title = element_text(face = "bold", family = "Arial"),
plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"),
legend.position = "top",
legend.box = "horizontal",
legend.title = element_blank(),
axis.ticks.y = element_line(size = 0.5),
axis.ticks.x = element_line(size = 0.5),
axis.ticks.length = unit(0.2,"cm"),
axis.text.y = element_text(colour = "black", size = 14, angle = 0, vjust = 0.5, hjust = 1,
margin = margin(t = 0, r = 5, b = 0, l = 0)),
axis.text.x = element_text(colour = "black", size = 14, angle = 0, vjust = 0.7, hjust = 0.5,
margin = margin(t = 5, r = 0, b = 0, l = 0)),
axis.line = element_line(colour = "black", size = 0.5, lineend = "square"))
yct_C_rain
我得到的这个图有一个非常需要的默认图例。
但我希望图例看起来像这样:
我已尝试对 ggplot()
图例进行多种不同的自定义,但我认为雨云图的使用使我无法理解如何执行此操作。
也许您正在寻找这个
my_clrs_yct <- c("grey15", "grey35", "grey55", "#407a8c", "#404f86", "#3e1451")
my_labels <- c("100 - 300", "301 - 475", "476 - 575")
dfs <- toupper(unique(rain_plot_data$study))
mylabels <- c(paste(dfs[2],my_labels),paste(dfs[1],my_labels))
yct_C_rain <- ggplot(rain_plot_data, aes(y=d13C, x=factor(lengthcat),
fill = interaction(lengthcat, study),color=interaction(lengthcat, study)),
show.legend = FALSE,
position = position_nudge(x = 1, y =0)) +
geom_flat_violin(position = position_nudge(x = .2, y =0), alpha = .5)+
geom_point(aes(y = d13C, color = interaction(lengthcat, study)),
position = position_jitter(width = .05), size = 2, alpha = .5) +
geom_boxplot(width = .3, outlier.shape = NA, alpha = 0, notch = FALSE, show.legend = FALSE) +
stat_summary(fun= mean, geom = "point", shape = c(21,23,21,23,21,23), size = 3, fill = "black") +
scale_y_continuous(limits = c(-29,-13),expand = c(0,0),breaks=seq(-29,-13,2)) +
scale_x_discrete(labels = my_labels, expand = c(0.1,0.5) ) + #
ylab("δ13C") + xlab("Total length group") +
ggtitle("") +
scale_fill_manual(values=my_clrs_yct) +
scale_color_manual(values=my_clrs_yct, label= mylabels) +
theme(panel.background=element_blank(), panel.grid = element_blank()) +
theme(axis.title.y = element_text(size = 14, margin = margin(t = 0, r = 10, b = 0, l = 0), colour = "black"),
axis.title.x = element_text(size = 14, margin = margin(t = 10, r = 0, b = 0, l = 0), colour = "black"),
text = element_text(family = "Times New Roman"),
plot.title = element_text(face = "bold", family = "Arial"),
plot.margin = unit(c(0.9, 0.5, 0.99, 0.5), "cm"),
legend.position = "top",
legend.box = "horizontal",
legend.key = element_blank(),
legend.title = element_blank(),
axis.ticks.y = element_line(size = 0.5),
axis.ticks.x = element_line(size = 0.5),
axis.ticks.length = unit(0.2,"cm"),
axis.text.y = element_text(colour = "black", size = 12, angle = 0, vjust = 0.5, hjust = 1,
margin = margin(t = 0, r = 5, b = 0, l = 0)),
axis.text.x = element_text(colour = "black", size = 12, angle = 0, vjust = 0.7, hjust = 0.5,
margin = margin(t = 5, r = 0, b = 0, l = 0)),
axis.line = element_line(colour = "black", size = 0.5, lineend = "square")) +
guides(color = guide_legend(override.aes=list(color=my_clrs_yct, lty=0),
nrow = 2, byrow = TRUE),
fill='none')
yct_C_rain