I have the following 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"))
I want to produce a ggplot2 box plot where df$x defines the x axis locations of the boxes and df$lower, df$upper, df$middle, df$ymin, df$ymax define the boxes, and df$colors define the colors of the boxes. As you can see each pair of boxes should be put on the same x axis location. With the alpha parameter I'll make the boxes transparent so overlaps will be visible.
我想制作一个ggplot2 box plot,其中df$x定义了盒子的x轴位置,df$lower, df$upper, df$middle, df$ymin, df$ymax定义了盒子的颜色,df$彩色定义了盒子的颜色。正如您所看到的,每一对盒子都应该放在相同的x轴位置上。有了alpha参数,我将使盒子透明,这样重叠就可以看到了。
What I have so far is this code:
到目前为止,我的代码是:
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")
Obviously the colors of the boxes are messed up - the "blue"
and "red"
boxes should be at df$x
= 1, the "gray30"
and "gray70"
boxes should be at df$x
= 2, and , the "lightskyblue"
and "pink"
boxes should be at df$x
= 3. So I'm looking to fix that.
很明显,盒子的颜色弄乱了——“蓝色”和“红色”的盒子应该是df$x = 1,“gray30”和“gray70”的盒子应该是df$x = 2,“lightskyblue”和“pink”的盒子应该是df$x = 3。所以我想解决这个问题。
I additionally want to have the legend title and labels to be specified rather then generated by defaults. In addition, the df$posterior.probability values are identical for each pair of boxes with the same x axis location, and what I would like is to draw these values (one for each pair) either at the top of the plot, say at the maximum y axis value, or on top the max(ymax)
of each pair of boxes and at the corresponding x axis locations. In other words, 0.92, 0.97, and 0.99 will be drawn at x locations: 1, 2, and 3, respectively, and at either the maximum y location of the plot or at these locations y locations: 0.75, 0.65, and 0.95, respectively.
另外,我还希望将标题和标签指定为默认值,而不是由默认值生成。此外,df后美元。概率值是相同的每一对盒相同的x轴的位置,我想画这些值(每组一个)顶部的阴谋,在y轴的最大价值,或者在上面的马克斯(ymax)每一对对应的盒子和x轴的位置。换句话说,0.92、0.97和0.99将分别绘制在x个地点:1、2和3,以及在地块的最大y位置,或分别绘制在这些地点:0.75、0.65和0.95。
2 个解决方案
#1
2
Your boxplots are overlapping and as I understand from your code, you don't want that. You don't need the position element in your code and you have tot treat you x
as a factor in order to get them plotted next to each other.
你的boxplot是重叠的,我从你的代码中了解到,你不希望这样。你不需要在你的代码中使用位置元素,你必须把你的x作为一个因数来把它们画在一起。
With this code:
这段代码:
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")
You get this result:
你得到这个结果:
When you don't want the text-labels in the same color, add color = "black"
to the geom_text
part.
如果不希望文本标签使用相同的颜色,请在geom_text部分中添加color = "black"。
#2
2
For labels made new data frame that contains only one label for each x
position and y value is calculated as maximal ymax
value.
对于标签,新数据帧只包含每个x位置的一个标签,y值被计算为最大ymax值。
library(plyr)
df.text<-ddply(df,.(x),summarise,y=max(ymax),label=max(posterior.probability))
As you supply color names then you should use scale_fill_identity()
and scale_color_identity()
to tell ggplot to interpret those as actual color. If you need to show legend, then add argument guide="legend"
to scale_fill_identity()
and scale_color_identity()
and then provide labels=
you want to show in legend. Then use geom_text()
and new data frame to add labels above boxplots.
当您提供颜色名称时,您应该使用scale_fill_identity()和scale_color_identity()来告诉ggplot将它们解释为实际的颜色。如果需要显示legend,那么向scale_fill_identity()和scale_color_identity()添加参数指南=“legend”,然后提供要在legend中显示的标签=。然后使用geom_text()和新的数据帧在boxplot上添加标签。
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))
#1
2
Your boxplots are overlapping and as I understand from your code, you don't want that. You don't need the position element in your code and you have tot treat you x
as a factor in order to get them plotted next to each other.
你的boxplot是重叠的,我从你的代码中了解到,你不希望这样。你不需要在你的代码中使用位置元素,你必须把你的x作为一个因数来把它们画在一起。
With this code:
这段代码:
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")
You get this result:
你得到这个结果:
When you don't want the text-labels in the same color, add color = "black"
to the geom_text
part.
如果不希望文本标签使用相同的颜色,请在geom_text部分中添加color = "black"。
#2
2
For labels made new data frame that contains only one label for each x
position and y value is calculated as maximal ymax
value.
对于标签,新数据帧只包含每个x位置的一个标签,y值被计算为最大ymax值。
library(plyr)
df.text<-ddply(df,.(x),summarise,y=max(ymax),label=max(posterior.probability))
As you supply color names then you should use scale_fill_identity()
and scale_color_identity()
to tell ggplot to interpret those as actual color. If you need to show legend, then add argument guide="legend"
to scale_fill_identity()
and scale_color_identity()
and then provide labels=
you want to show in legend. Then use geom_text()
and new data frame to add labels above boxplots.
当您提供颜色名称时,您应该使用scale_fill_identity()和scale_color_identity()来告诉ggplot将它们解释为实际的颜色。如果需要显示legend,那么向scale_fill_identity()和scale_color_identity()添加参数指南=“legend”,然后提供要在legend中显示的标签=。然后使用geom_text()和新的数据帧在boxplot上添加标签。
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))