X値を測定値のサブセットとして、Y値を測定データの別のサブセットとしてプロットしたいと思います。
以下の例では、4つの製品p1、p2、p3、p4があります。それぞれは、スキュー、色、バージョンに応じて価格設定されます。 P3製品(Y軸)とP1製品(X軸)を示すマルチファセットプロットを作成したいと思います。
以下の私の試みは、次のエラーで惨めに失敗しました:
エラー:美学は長さ1、またはdataProblems:subset(price、product == "p1")、subset(price、product == "p3")と同じ長さでなければなりません
library(ggplot2)
product=c("p1","p1","p1","p1","p1","p1","p1","p1","p2","p2","p2","p2","p2","p2","p2","p2","p3","p3","p3","p3","p3","p3","p3","p3","p4","p4","p4","p4","p4","p4","p4","p4")
skew=c("b","b","b","b","a","a","a","a","b","b","b","b","a","a","a","a","b","b","b","b","a","a","a","a","b","b","b","b","a","a","a","a")
version=c(0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2)
color=c("C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2")
price=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32)
df = data.frame(product, skew, version, color, price)
# First plot all the data
p1 <- ggplot(df, aes(x=price, y=price, colour=factor(skew))) + geom_point(size=2, shape=19)
p1 <- p1 + facet_grid(version ~ color)
p1 # This gavea very good plot. So far so good
# Now plot P3 vs P1
p1 <- ggplot(df, aes(x=subset(price, product=='p1'), y=subset(price, product=='p3'), colour=factor(skew))) + geom_point(size=2, shape=19)
p1
# failed with: Error: Aesthetics must either be length one, or the same length as the dataProblems:subset(price, product == "p1"), subset(price, product == "p3")
これは私が期待している結果です:
問題は、skew
がcolour=factor(skew)
にサブセット化されていないため、長さが間違っていることです。 subset(skew, product == 'p1')
はsubset(skew, product == 'p3')
と同じであるため、この場合、どのサブセットが使用されるかは関係ありません。そのため、次の方法で問題を解決できます。
p1 <- ggplot(df, aes(x=subset(price, product=='p1'),
y=subset(price, product=='p3'),
colour=factor(subset(skew, product == 'p1')))) +
geom_point(size=2, shape=19)
ほとんどのRユーザーは、これをより簡潔に書くことに注意してください。
p1 <- ggplot(df, aes(x=price[product=='p1'],
y=price[product=='p3'],
colour=factor(skew[product == 'p1']))) +
geom_point(size=2, shape=19)
aes()
内の変数をサブセット化せず、代わりにデータを変換することをお勧めします。
df1 <- unstack(df,form = price~product)
df1$skew <- rep(letters[2:1],each = 4)
p1 <- ggplot(df1, aes(x=p1, y=p3, colour=factor(skew))) +
geom_point(size=2, shape=19)
p1
@joranの答えに似ています。各製品の価格が異なる列になるようにdfを再構成します。
xx <- reshape(df, idvar=c("skew","version","color"),
v.names="price", timevar="product", direction="wide")
xxの列はprice.p1、... price.p4なので、次のようになります。
ggp <- ggplot(xx,aes(x=price.p1, y=price.p3, color=factor(skew))) +
geom_point(shape=19, size=5)
ggp + facet_grid(color~version)
画像から結果が得られます。
Geom(geom_text
)でラベル属性を指定していたが、トップレベルのaesで色を指定していたため、このエラーが発生しました。
df <- read.table('match-stats.tsv', sep='\t')
library(ggplot2)
# don't do this!
ggplot(df, aes(x=V6, y=V1, color=V1)) +
geom_text(angle=45, label=df$V1, size=2)
これを修正するために、ラベル属性をgeomからトップレベルaesに移動しました。
df <- read.table('match-stats.tsv', sep='\t')
library(ggplot2)
# do this!
ggplot(df, aes(x=V6, y=V1, color=V1, label=V1)) +
geom_text(angle=45, size=2)