37

我想绘制一个图,其中 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")

这是我期待的结果:

这是我期待的结果

4

5 回答 5

24

最好不要子集内部的变量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
于 2013-11-18T20:59:04.987 回答
22

问题是它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)
于 2013-11-18T20:51:37.583 回答
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)

给出图像的结果。

于 2013-11-19T05:45:16.750 回答
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)

为了解决这个问题,我只是将 label 属性从 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)
于 2018-03-31T15:22:40.330 回答
0

我遇到了这个问题,因为数据集过滤错误,结果数据框为空。甚至以下导致错误显示:

ggplot(df, aes(x="", y = y, fill=grp))

因为df是空的。

于 2020-11-12T13:01:07.780 回答