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)

次に、標準の 0.25 から 0.75 ではなく、たとえば 0.2 から 0.85 パーセンタイルをプロットquantiles_dfするように入力として使用するにはどうすればよいですか? geom_boxplotありがとうございました。

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 に答える