2

我有以下箱线图:

import os
iris = pandas.read_table(os.path.expanduser("~/iris.csv"),
                         sep=",")
iris["Species"] = iris["Name"]
r_melted = conversion_pydataframe(iris)
p = ggplot2.ggplot(r_melted) + \
    ggplot2.geom_boxplot(aes_string(**{"x": "PetalLength",
                                       "y": "PetalWidth",
                                       "fill": "Species"})) + \
    ggplot2.facet_grid(Formula("Species ~ .")) + \
    ggplot2.coord_flip()
p.plot()

我的问题是:如何更改箱线图中绘制的胡须/分位数?假设我有一个数据框,我可以在其中按行或列计算分位数,如下所示:

quantiles_df = iris.quantiles(q=0.85, axis=1)

那么我如何使用它quantiles_df作为输入,geom_boxplot以便它绘制例如 0.2 到 0.85 的百分位数而不是标准的 0.25 到 0.75?谢谢你。

4

1 回答 1

0

您可以在 R 中从此开始。首先计算变量(此处为 Petal.Width)的每个物种的百分位数,并将其用于绘图。通过指定ymin(=下须线边界)、lower(=框的下边界)、middle(=框中的线)、upper(=框的上边界)、ymax(=上须线边界)并添加stat = "identity",您可以自定义箱线图。

library(reshape2)
library(plyr)
library(ggplot2)

dataf <- ddply(iris, .(Species), summarize, quantilesy= quantile(Petal.Width, c(0,0.2, 0.5,0.85,1 )))
dataf$Labels <- rep(c("0%", "20%","50%","85%", "100%"),length(unique(dataf$Species)))

dataf2 <- reshape(dataf , idvar = c("Species"),timevar = "Labels", direction = "wide")
datafmeanx <- ddply(iris, .(Species), summarize, meanx= mean(Petal.Length))
dataf3 <- merge(dataf2,datafmeanx)

b <- ggplot(dataf3 , aes(x=meanx,ymin = `quantilesy.0%`, lower = `quantilesy.20%`, middle = `quantilesy.50%`, upper = `quantilesy.85%`, ymax = `quantilesy.100%`))
b + geom_boxplot(stat = "identity")+ facet_grid(Species~.) + xlab("Mean PetalLength") + ylab("PetalWidth")

编辑:如果你不想使用反引号(见评论):

dataf$Labels <- rep(c("0", "20","50","85", "100"),length(unique(dataf$Species)))

dataf2 <- reshape(dataf , idvar = c("Species"),timevar = "Labels", direction = "wide")
datafmeanx <- ddply(iris, .(Species), summarize, meanx= mean(Petal.Length))
dataf3 <- merge(dataf2,datafmeanx)

b <- ggplot(dataf3 , aes(x=meanx ,ymin = quantilesy.0, lower = quantilesy.20, middle = quantilesy.50, upper = quantilesy.85, ymax = quantilesy.100))
b + geom_boxplot(stat = "identity")+ facet_grid(Species~.) + xlab("Mean PetalLength") + ylab("PetalWidth")

在此处输入图像描述

于 2013-03-12T21:27:48.113 回答