我有以下boxplot:
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?谢谢。
答案 0 :(得分: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")