美学要么是长度为1,要么与dataProblems的长度相同 [英] Aesthetics must either be length one, or the same length as the dataProblems
问题描述
我想用X值作为测量的一个子集,Y值作为测量数据的另一个子集。
在示例中,as下面,我有4个产品p1,p2,p3和p4。每个都根据他们的歪斜,颜色和版本来定价。
我想创建一个多面图,描绘P3产品(Y轴)与P1产品(X轴)。我的尝试如下所示,失败后失败:
lockquote
错误:美学要么是长度为1,要么是与
相同的长度dataProblems:子集(价格,产品==p1),子集(价格,产品
==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, 一, 一个, 一, 一个, b, b, b, b, 一, 一,一个abbbbaaaa)
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, (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)
价格= 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)
#首先绘制所有数据
p1 < - ggplot(df,aes(x = price,y = price,color = factor (skew)))+ geom_point(size = 2,shape = 19)
p1 < - p1 + facet_grid(version_color)
p1#这给出了非常好的情节。到目前为止这么好
#现在绘制出P3对P1
p1 < - ggplot(df,aes(x = subset(price,product =='p1'),y = subset(price,product = ='p3'),color = factor(skew)))+ geom_point(size = 2,shape = 19)
p1
#失败,错误:美学必须是长度为1或相同长度为dataProblems:子集(price,product ==p1),子集(price,product ==p3)
这是我期待的结果:
问题是 skew
没有在 color = factor(skew)
中进行子集化,所以它的长度是错误的。因为 subset(skew,product =='p1')
与 subset(skew,product =='p3')相同
,在这种情况下,使用哪个子集并不重要。因此,您可以通过以下方式解决您的问题:
p1 < - ggplot(df,aes(x = subset(price,product = ='p1'),
y = subset(price,product =='p3'),
color = factor(subset(skew,product =='p1')))+
geom_point(size = 2,shape = 19)
请注意,大多数R用户会将其写为更多简洁:
p1 < - ggplot(df,aes(x = price [product =='p1'],
y = price [product =='p3'],
color = factor(skew [product =='p1'])))+
geom_point(size = 2,shape = 19)
I would like to make a plot with X values as a subset of the measurement and Y-values as another subset of the measured data.
In the example as below, I have 4 products p1, p2, p3 and p4. Each are priced according to their skew, color and version. I would like to create a multi-facet plot that depicts the P3 products (Y-axis) vs P1 products (X-axis).
My attempt as below has failed miserably with the following error:
Error: Aesthetics must either be length one, or the same length as the 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")
This is the result I am expecting:
The problem is that skew
isn't being subsetted in colour=factor(skew)
, so it's the wrong length. Since subset(skew, product == 'p1')
is the same as subset(skew, product == 'p3')
, in this case it doesn't matter which subset is used. So you can solve your problem with:
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)
Note that most R users would write this as the more concise:
p1 <- ggplot(df, aes(x=price[product=='p1'],
y=price[product=='p3'],
colour=factor(skew[product == 'p1']))) +
geom_point(size=2, shape=19)
这篇关于美学要么是长度为1,要么与dataProblems的长度相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!