用户为ggplot2 geom_boxplot定义的参数 [英] User defined parameters for ggplot2 geom_boxplot
问题描述
我有以下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(父母,父母,十字架,十字架,性别, (父亲,母亲,F1i,F1R,M,F),后验概率= 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 $ colors定义框的颜色。正如你所看到的,每一对盒子应该放在同一个X轴位置。使用alpha参数,我会使框变得透明,以便重叠显示。
到目前为止,我的代码是这样的:
p = ggplot(beta.df,aes(x = x,color = colours))
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)
我还想要指定图例标题和标签然后由默认生成。此外,df $ posterior.probability值对于每对具有相同x轴位置的盒子是相同的,我想要的是在图的顶部绘制这些值(每对一个),比如说最大的y轴值,或者在每对箱子的 您的箱型图重叠,正如我从您的代码中了解到的,您不需要这些。你不需要你的代码中的position元素,并且你将 使用此代码: 您得到以下结果: 如果您不希望文字标签颜色相同,请添加 I have the following data.frame: 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. What I have so far is this code:
Obviously the colors of the boxes are messed up - the 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 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 With this code: You get this result: When you don't want the text-labels in the same color, add 这篇关于用户为ggplot2 geom_boxplot定义的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
很显然,这些箱子的颜色已经搞乱了 - blue
和red
框应该在 df $ x
= 1,gray30
和gray70
框应该在 df $ x
= 2,pink
框应该在 df $ x
= 3。所以我正在修复这个问题。 p>
max(ymax)
上和相应的x轴位置上。换言之,0.92,0.97和0.99将分别绘制在x位置:1,2和3处,并且在绘图的最大y位置处或在这些位置y位置处绘制:0.75,0.65和0.95,分别。
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 =Plot title,x =X-lab label,y =Y-lab label)
color =black
到 geom_text
部分。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"))
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"
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.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. x
as a factor in order to get them plotted next to each other.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"
to the geom_text
part.