-1

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)
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
alki
  • 3,334
  • 5
  • 22
  • 45

1 Answers1

1

(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()
Scransom
  • 3,175
  • 3
  • 31
  • 51