ggplot2错误:美学必须是长度为1或与dataProblems相同的长度:颜色,字母 [英] ggplot2 Error: Aesthetics must either be length one, or the same length as the dataProblems:colors, letters
问题描述
出现此错误:
错误:美学必须是长度为1或与dataProblems的长度相同:colors ,字母
当我使用ggplot和dataframe Z
时,如下所示:< (Name= c(A,G,C, - ), T, T, T, 公司, 公司, 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),
位置= c(1,1,1,1,1,1,1,2,2,3,3,4,9,10,12,13))
Z [1: 16]#小虚拟样本
#名称曲目位置
#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
以后的调色板
#创建调色板$ (h = seq(15,375,长度=(x + 1)),l = 65,c = 100)b x.colors< [1:x]
x.colors [1:4] < - c(blue,red,green,yellow)
colors < - factor(x。颜色)
字母< - 因子(等级(Z $名称))
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)
绘图代码应该如下所示:
ggplot(Z ,aes(x = Track,y = Position,fill = Name,label = Name))+
geom_raster()+
geom_text()
I'm getting this error:
Error: Aesthetics must either be length one, or the same length as the dataProblems:colors, letters
when I use ggplot with dataframe Z
as shown here:
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
Here I create a color palette to apply to geom_raster
later
# 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
And this code tries to plot everything:
# Plot
ggplot(NULL) +
aes(x = Z$Track,
y = Z$Position,
fill = colors,
label = letters) +
geom_raster() +
geom_text() +
scale_fill_manual(values=my_fill)
(Adding a full answer for clarity's sake; consolidating on comments from Pascal, johnson_shuffle, & Jaap)
Plotting code should look like:
ggplot(Z, aes(x=Track, y=Position, fill=Name, label=Name)) +
geom_raster() +
geom_text()
这篇关于ggplot2错误:美学必须是长度为1或与dataProblems相同的长度:颜色,字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!