2015-07-22 80 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() 
相关问题