これはこちらのフォローアップ投稿です。
以下のコードに従って、サブセットデータを使用してファセットプロットを作成しようとしています。要約すると、私の表から、製品 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
望ましいプロットは次のとおりです。
代わりに、これは私が得るものです