[英]Multiplot does not work sometimes - receiving Error: Aesthetics must either be length one, or the same length as the dataProblems
[英]Aesthetics must either be length one, or the same length as the dataProblems
我想用 X 值作為測量值的子集和 Y 值作為測量數據的另一個子集繪制一個圖。
在下面的示例中,我有 4 個產品 p1、p2、p3 和 p4。 每個都根據它們的偏斜、顏色和版本定價。 我想創建一個多面圖,描繪 P3 產品(Y 軸)與 P1 產品(X 軸)。
我的以下嘗試失敗了,並出現以下錯誤:
錯誤:Aesthetics 必須是長度為 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")
這是我期待的結果:
最好不要對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
問題是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)
類似於@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)
給出圖像的結果。
我遇到了這個錯誤,因為我在我的 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)
我遇到這個問題是因為數據集過濾錯誤,結果數據框為空。 甚至以下導致錯誤顯示:
ggplot(df, aes(x="", y = y, fill=grp))
因為df
是空的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.