2015-07-22 110 views
-1

我得到這個錯誤:GGPLOT2錯誤:美學必須是一個長度或相同長度的dataProblems:顏色,字母

Error: Aesthetics must either be length one, or the same length as the dataProblems:colors, letters

,當我使用ggplot與數據幀Z如下所示:

Z <- data.frame("Name"=c("A","G","C","T","T","T","AG","AG","GC","GC","CT","CT","AT","AT","CT","CT"), 
    "Track"=c(0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1), 
    "Position"=c(1,1,1,1,1,1,1,2,2,3,3,4,9,10,12,13)) 

Z[1:16]    # Small dummy sample 
# Name Track Position 
# 1 A  0  1 
# 2 G  0  1 
# 3 C  0  1 
# 4 T  0  1 
# 5 T  0  1 
# 6 T  0  1 
# 7 AG  1  1 
# 8 AG  1  2 
# 9 GC  1  2 
# 10 GC  1  3 
# 11 CT  1  3 
# 12 CT  1  4 
# 13 AT  1  9 
# 14 AT  1  10 
# 15 CT  1  12 
# 16 CT  1  13 

在這裏,我創建一個調色板後適用於geom_raster

# Create color palette 
x <- length(levels(Z$Name))    
x.colors <- hcl(h=seq(15,375,length=(x+1)),l=65,c=100)[1:x] 
x.colors[1:4] <- c("blue","red","green","yellow")  
colors <- factor(x.colors) 
letters <- factor(levels(Z$Name)) 
my_fill <- x.colors 

此代碼嘗試繪製所有內容:

# Plot 
ggplot(NULL) + 
    aes(x = Z$Track, 
     y = Z$Position, 
     fill = colors, 
     label = letters) + 
    geom_raster() + 
    geom_text() + 
    scale_fill_manual(values=my_fill) 
+1

'aes'應該是裏面'ggplot()'。 – 2015-07-22 07:07:00

+2

在'aes'裏面用'fill = Z $ Name'和'label = Z $ Name'試試。看起來很奇怪,但在Track/Position 0/1,1/2和1/3處有重複。 –

+0

當我這樣做時,我遇到了另一個錯誤'錯誤:ggplot2不知道如何處理類的數據uneval' @帕斯卡爾 – ALKI

回答

1

(爲清晰起見添加完整答案;鞏固從帕斯卡,johnson_shuffle,&夏侯)

繪製代碼註釋應該是這樣的:

ggplot(Z, aes(x=Track, y=Position, fill=Name, label=Name)) + 
    geom_raster() + 
    geom_text() 
相關問題