0

これはこちらのフォローアップ投稿です

以下のコードに従って、サブセットデータを使用してファセットプロットを作成しようとしています。要約すると、私の表から、製品 P3 の価格と製品 P1 の価格をプロットし、プロットをバージョンと色に対してファセットします。

ファセットは、以下のコードではどういうわけか機能しません。代わりに、すべてのデータポイントがすべてのファセットで複製されます。これを修正する方法について何か考えはありますか? そして、私が下に添付している「望ましいプロット」(手動でハッキングされたもの)を取得しますか?

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, labeller = label_both, , scales="free")
p1 
# Second, plot P3 prices vs P1 prices
p2 <- ggplot(df, aes(x=price[product=='p1'], y=price[product=='p3'], colour=factor(skew[product == 'p1']))) + geom_point(size=2, shape=19)
p2 <- p2 + facet_grid(version ~ color, labeller = label_both, , scales="free")
p2

望ましいプロットは次のとおりです。

希望のプロット

代わりに、これは私が得るものです これは私が得るものです

4

1 に答える 1

2

これは、前回の質問で述べたように、 内の変数のサブセット化に依存するべきではないために発生しますaes()。代わりにこれを行う必要があります:

library(reshape2)
df1 <- dcast(df,skew+version+color~product,
             fun.aggregate = sum,
             value.var = "price")
ggplot(df1,aes(x = p1,y = p3,colour = factor(skew))) +
  facet_grid(version~color) +
  geom_point(size = 2,shape = 19)
于 2013-11-19T04:07:19.613 に答える