-1

I guess this questions has been asked already but I will try it anyways, as I couldn't find anything thich solved my problem.

I simply want to draw a series of rectangles, and then color them depending on a value, but not the x or y value.

So: I am reading from a file start and end positions for the rectangles I want to draw:

file <- read.table("positions.txt", header=FALSE)
names(file) <- c("start", "end")

char_start <- as.character(file$start)
first <- as.numeric(char_start)

char_end <- as.character(file$end)
last <- as.numeric(char_end)

mydata <- data.frame(first, last)

I make my plot then like this:

plot <- ggplot() + geom_rect(data = mydata, aes(xmin = mydata$first, xmax = mydata$last, ymin = 2, ymax = 5)) 

Now I was trying to fill the rectangles with two different colors, depending on some other value. Lets say for example that I have another file "positions_subset.txt" which contains a subset of the first file. If a rectangle is contained in both, I want it blue, otherwise red.

Idea:

So what I thought how I could do it, is to parse the to files, and every time I find the coordinate pair in both files, I add a "1" to a vector, otherwise "0". So at the end I get a vector like:

 in_both <- c(1,0,0,1,0...)

which has the same number of elements as rectangles I have. Then I simply did:

colors <- ifelse(in_both == 1, "blue", "red")

However if I then try to do it like

ggplot() + aes(xmin = mydata$first, xmax = mydata$last, ymin = 2, ymax = 5, fill=colors))

I get the error "Aesthetics must be either length 1 or the same as the data (30): xmin, xmax, ymin, ymax, fill"

I understand what the error is saying but I don't know how I could do it otherwise. Do I really need for each point on the x axis to have a color information?

malajedala
  • 867
  • 2
  • 8
  • 22
  • Hey! Please provide an example of your data and, if possible example of your plot. See here for more info: https://stackoverflow.com/a/5963610/4738478 – zlipp Oct 26 '17 at 16:48

1 Answers1

1

Add a column to you data containing the variable used to color

 data$color<-colors

Then use

 aes(xmin=first, xmax=last,...,fill=color)

you should pass the name of the variable to aes, not a vector

Cedric
  • 2,412
  • 17
  • 31
  • ok I undestand, thank you. But isnt color data$color also a vector? – malajedala Oct 27 '17 at 10:23
  • Yes, and it will not correspond to your colors, you need to use + scale_fill_manual(values=color) to get the initial colors see this post https://stackoverflow.com/questions/36048033/manually-colouring-plots-with-scale-fill-manual-in-ggplot2-not-working – Cedric Oct 27 '17 at 10:48