在python中使用rpy2将分位数输入ggplot geom_箱线图
我有以下箱线图:在python中使用rpy2将分位数输入ggplot geom_箱线图,python,r,ggplot2,pandas,rpy2,Python,R,Ggplot2,Pandas,Rpy2,我有以下箱线图: 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_strin
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)
那么,我如何使用
分位数_df
作为geom_箱线图
的输入,以便它绘制例如0.2到0.85百分位数,而不是标准的0.25到0.75?谢谢。您可以从R中的这一点开始。首先计算一个变量(此处为花瓣宽度)的每个物种的百分位数,并将其用于绘图。通过指定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")
library(重塑2)
图书馆(plyr)
图书馆(GG2)
你能发布你的完整代码吗?您正在导入熊猫。还有什么吗?你能解释一下dataf3的最终格式吗?那些反报价在做什么?将那些ddply
调用转换为pandas是很困难的。我想应该使用groupby
,但我发现这种格式非常隐晦,因此很难翻译成python,因为“%”是ggplot中的非法字符,我不得不将名称括在反勾中。如果删除“%”符号,则可以绘制无刻度的绘图。我加上去了。尝试在R中运行它以查看数据F3的最终格式。行是物种,计算出的分位数和meanx是列,因此一个包含3行7列的数据帧。
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")