将箱线图的 geom_segment 和中位数添加到图例
Adding geom_segment and Median of the Boxplot to legend
我正在寻找一种方法,在图例中有两个或多个线段以及箱线图的中位数。
我提出了以下示例:
y = data.frame(runif(500))
library(ggplot2)
ggplot(data = y )+
aes(0, y)+
geom_boxplot(outlier.shape = 1)+
scale_y_continuous(name = "",limits = c(0,1))+
scale_x_discrete(name = "")+
geom_segment( aes(x=-0.362, y=0.6, xend=0.363,yend=0.6, linetype = "R
fans"), linetype = "dashed", colour = "black")+
geom_segment( aes(x=-0.35, y=0.8, xend=0.35,yend=0.8, linetype =
"frustated R users"), col = "red" )+
theme_bw()+
theme(legend.title = element_blank())+
theme(legend.background = element_rect(fill="white",
size=0.1, linetype="solid",
colour ="black"))
y=0.6 的 geom_segment 应在图例中用黑色虚线表示。目前我选择了两次线型,这没有意义,但如果我擦除第二个线型,图例中的颜色会变成红色,或者线型会变成另一个不需要的线型。对于情节和图例,它应该是黑色和虚线。对于 y = 0.8,它运行良好,因为默认情况下线型是正确的。
另外,我想在图例中加入第三行。第三条线为中线,黑色实线。
提前感谢您的帮助。
通过将线条作为单独的 data.frame
传递解决方案,通过 color = Group
设置单独的线条颜色并使用 scale_color_manual
.
指定这些颜色
library(ggplot2)
# Generate data
dBox <- data.frame(y = rnorm(10))
dLines <- data.frame(X =c(-0.362, -0.35),
Y = c(0.6, 0.8),
Xend = c(0.363, 0.35),
Yend=c(0.6, 0.8),
Group = c("typeA", "typeB"),
color = c("black", "red"))
ggplot(dBox, aes(0, y)) +
geom_boxplot(outlier.shape = 1)+
scale_y_continuous(name = "",limits = c(0,1))+
scale_x_discrete(name = "") +
geom_segment(data = dLines,
aes(x = X, xend = Xend,
y = Y, yend = Yend,
color = Group)) +
scale_color_manual(values = dLines$color) +
theme_bw() +
theme(legend.title = element_blank()) +
theme(legend.background = element_rect(fill = "white",
size = 0.1,
linetype = "solid",
colour = "black"))
我还增加了 PoGibas 对两种不同线型的非常有用的答案:
y = data.frame(runif(500))
dLines <- data.frame(X =c(-0.362, -0.35),
Y = c(0.6, 0.8),
Xend = c(0.363, 0.35),
Yend=c(0.6, 0.8),
Group = c("TypeA", "TypeB"),
color = c("black", "red"),
linetype = c( "solid", "dashed"))
ggplot(data = y )+
aes(0, y)+
geom_boxplot(outlier.shape = 1)+
scale_y_continuous(name = "",limits = c(0,1))+
scale_x_discrete(name = "")+
geom_segment(data = dLines,
aes(x = X, xend = Xend,
y = Y, yend = Yend,
color = Group,
linetype = Group))+
scale_color_manual(values = dLines$color) +
scale_linetype_manual(values = dLines$linetype) +
theme_bw()+
theme(legend.title = element_blank())+
theme(legend.background = element_rect(fill="white",
size=0.1, linetype="solid",
colour ="black"))
我正在寻找一种方法,在图例中有两个或多个线段以及箱线图的中位数。 我提出了以下示例:
y = data.frame(runif(500))
library(ggplot2)
ggplot(data = y )+
aes(0, y)+
geom_boxplot(outlier.shape = 1)+
scale_y_continuous(name = "",limits = c(0,1))+
scale_x_discrete(name = "")+
geom_segment( aes(x=-0.362, y=0.6, xend=0.363,yend=0.6, linetype = "R
fans"), linetype = "dashed", colour = "black")+
geom_segment( aes(x=-0.35, y=0.8, xend=0.35,yend=0.8, linetype =
"frustated R users"), col = "red" )+
theme_bw()+
theme(legend.title = element_blank())+
theme(legend.background = element_rect(fill="white",
size=0.1, linetype="solid",
colour ="black"))
y=0.6 的 geom_segment 应在图例中用黑色虚线表示。目前我选择了两次线型,这没有意义,但如果我擦除第二个线型,图例中的颜色会变成红色,或者线型会变成另一个不需要的线型。对于情节和图例,它应该是黑色和虚线。对于 y = 0.8,它运行良好,因为默认情况下线型是正确的。
另外,我想在图例中加入第三行。第三条线为中线,黑色实线。
提前感谢您的帮助。
通过将线条作为单独的 data.frame
传递解决方案,通过 color = Group
设置单独的线条颜色并使用 scale_color_manual
.
library(ggplot2)
# Generate data
dBox <- data.frame(y = rnorm(10))
dLines <- data.frame(X =c(-0.362, -0.35),
Y = c(0.6, 0.8),
Xend = c(0.363, 0.35),
Yend=c(0.6, 0.8),
Group = c("typeA", "typeB"),
color = c("black", "red"))
ggplot(dBox, aes(0, y)) +
geom_boxplot(outlier.shape = 1)+
scale_y_continuous(name = "",limits = c(0,1))+
scale_x_discrete(name = "") +
geom_segment(data = dLines,
aes(x = X, xend = Xend,
y = Y, yend = Yend,
color = Group)) +
scale_color_manual(values = dLines$color) +
theme_bw() +
theme(legend.title = element_blank()) +
theme(legend.background = element_rect(fill = "white",
size = 0.1,
linetype = "solid",
colour = "black"))
我还增加了 PoGibas 对两种不同线型的非常有用的答案:
y = data.frame(runif(500))
dLines <- data.frame(X =c(-0.362, -0.35),
Y = c(0.6, 0.8),
Xend = c(0.363, 0.35),
Yend=c(0.6, 0.8),
Group = c("TypeA", "TypeB"),
color = c("black", "red"),
linetype = c( "solid", "dashed"))
ggplot(data = y )+
aes(0, y)+
geom_boxplot(outlier.shape = 1)+
scale_y_continuous(name = "",limits = c(0,1))+
scale_x_discrete(name = "")+
geom_segment(data = dLines,
aes(x = X, xend = Xend,
y = Y, yend = Yend,
color = Group,
linetype = Group))+
scale_color_manual(values = dLines$color) +
scale_linetype_manual(values = dLines$linetype) +
theme_bw()+
theme(legend.title = element_blank())+
theme(legend.background = element_rect(fill="white",
size=0.1, linetype="solid",
colour ="black"))