我想制作一个图,其中X值作为测量的子集,Y值作为测量数据的另一个子集。
在下面的示例中,我有4个产品p1,p2,p3和p4。每个都根据他们的歪斜,颜色和版本定价。 我想创建一个多面图,描绘P3产品(Y轴)与P1产品(X轴)。
我的下面的尝试因以下错误而失败:
错误:美学必须是长度为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","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")
这是我期待的结果:
答案 0 :(得分:21)
最好不要在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
答案 1 :(得分:18)
问题是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)
答案 2 :(得分:6)
与@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)
显示图像的结果。
答案 3 :(得分:1)
我遇到了这个错误,因为我在我的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)
答案 4 :(得分:0)
我遇到了这个问题,因为数据集被错误地过滤并且结果数据框为空。甚至以下情况也会导致错误显示:
ggplot(df, aes(x="", y = y, fill=grp))
因为df
为空。