我有以下data.frame:
df = data.frame(ymin = c(0.35,0.4,0.25,0.3,0.55,0.6), lower = c(0.45,0.5,0.35,0.4,0.65,0.7), middle = c(0.5,0.55,0.4,0.45,0.7,0.75), upper = c(0.55,0.6,0.45,0.5,0.75,0.8), ymax = c(0.65,0.7,0.55,0.6,0.85,0.9), factor = c("parental","parental","cross","cross","sex","sex"), factor.label = c("paternal","maternal","F1i","F1R","M","F"), posterior.probability = c(0.92,0.92,0.97,0.97,0.99,0.99), x = c(1,1,2,2,3,3), colors = c("blue","red","gray30","gray70","lightskyblue","pink"))
我想生成一个ggplot2框图,其中df $ x定义框的x轴位置,df $ lower,df $ upper,df $ middle,df $ ymin,df $ ymax定义框,df $颜色定义框的颜色。如您所见,每对盒子应放在相同的x轴位置。使用alpha参数,我将使框变为透明,因此重叠将是可见的。
到目前为止我的代码是:
p = ggplot(beta.df, aes(x = x, color = colors))
p = p + geom_boxplot(aes(lower = lower,upper = upper, middle = middle, ymin = ymin, ymax = ymax, fill = colors), position = position_dodge(width = 0), width = 0.5, alpha = 0.5, stat = "identity")
显然,框的颜色混乱了 - "blue"
和"red"
框应该在df$x
= 1,"gray30"
和"gray70"
框应该在df$x
= 2,并且"lightskyblue"
和"pink"
框应位于df$x
= 3.所以我希望解决此问题。
我还希望指定图例标题和标签,而不是默认生成。另外,对于每个具有相同x轴位置的盒子,df $ posterior.probability值是相同的,我想要的是在图的顶部绘制这些值(每对一个),比如说最大y轴值,或在每对盒子的max(ymax)
顶部和相应的x轴位置。换句话说,0.92,0.97和0.99将分别在x位置绘制:1,2和3,并且在绘图的最大y位置或在这些位置y位置:0.75,0.65和0.95,分别。
答案 0 :(得分:2)
你的箱图是重叠的,正如我从你的代码中理解的那样,你不希望这样。您不需要代码中的位置元素,并且您必须将x
视为一个因素,以便将它们彼此相邻绘制。
使用此代码:
ggplot(df, aes(x = as.factor(x), color = colors)) +
geom_boxplot(aes(lower = lower,upper = upper, middle = middle, ymin = ymin, ymax = ymax, fill = colors), width = 0.5, alpha = 0.5, stat = "identity") +
geom_text(data = df, aes(x = as.factor(x), y = ymax, label = posterior.probability), size = 4, vjust = -0.5) +
scale_fill_identity("Color legend", guide = "legend", labels = c("paternal","F1i","F1R","M","F","maternal")) +
scale_color_identity("Color legend", guide = "legend", labels = c("paternal","F1i","F1R","M","F","maternal")) +
labs(title = "Plot title", x = "X-lab label", y = "Y-lab label")
你得到这个结果:
如果您不希望文字标签采用相同的颜色,请将color = "black"
添加到geom_text
部分。
答案 1 :(得分:2)
对于标签,每个x
位置只包含一个标签的新数据框和y值计算为最大ymax
值。
library(plyr)
df.text<-ddply(df,.(x),summarise,y=max(ymax),label=max(posterior.probability))
当您提供颜色名称时,您应该使用scale_fill_identity()
和scale_color_identity()
告诉ggplot将其解释为实际颜色。如果您需要显示图例,请将参数guide="legend"
添加到scale_fill_identity()
和scale_color_identity()
,然后提供要在图例中显示的labels=
。然后使用geom_text()
和新数据框在箱图上方添加标签。
ggplot(df, aes(x = x)) +
geom_boxplot(aes(lower = lower,upper = upper, middle = middle, ymin = ymin,
ymax = ymax,color = colors, fill = colors),
position = position_dodge(width = 0), width = 0.5,
alpha = 0.5, stat = "identity")+
scale_fill_identity("Legend name",guide="legend",
labels=c("paternal","F1i","F1R","M","F","maternal"))+
scale_color_identity("Legend name",guide="legend",
labels=c("paternal","F1i","F1R","M","F","maternal"))+
geom_text(data=df.text,aes(x=x,y=y,label=label))