This is a test version of Biostars. For the public version, visit https://www.biostars.org.
strange legend in ggplot

Hi,

I am having difficulties plotting the correct values in my legend on a ggplot + geom_tile

The command I am using is:

ggplot(PlotData, aes(y = start,  x = start.1)) +  
   geom_tile(aes(fill = Substraction)) + 
   scale_size(range = c(1, 200)) + 
   scale_fill_gradientn(
      colours = rainbow(100), 
      values = seq(0,1 , .1), 
      space = "Lab", 
      na.value = "white", 
      guide = "legend") + 
   ggsave(file=paste(PlotName, "_gplots1_1S.pdf", sep=""))

and the data I'. using as an input is a data.frame of dim 17766225x4, which looks line that:

       start start.1 Subtraction bins2
1_1001     1    1001     1.109193    12
1_2001     1    2001           NA    NA
1_3001     1    3001           NA    NA
1_4001     1    4001           NA    NA
1_5001     1    5001           NA    NA
1_6001     1    6001           NA    NA

I would like to show the values of Subtraction in my plot. The range of the data is [0,1].

But when I plot it, I get in my legend values between 0-25. I don't understand where this values are coming from.

Am I plotting the right values? What is happening to my scales?

A snapshot of the plot created with this command can be seen here.

Thanks for any help,

Tomas

legend ggplot

Even your example has a value > 1, so your expected range seems to be off.

Thanks. That is true. I was using the is.na too loosely.

I did !is.na(PlotData$Subtraction) - which gives only a logical vector (TRUE - FALSE || 0 -1), instead of calling the values themselves.

This is the real range:

range(PlotData[!is.na(PlotData$Substraction),3])
[1] 6.532022e-06 2.512563e+01

I reformatted your code so that it is a bit easier to read. However this is really a question for stackexchange rather than biostar, because it is not related to bioinformatics. To force the scale limits just use limits=c(0,1) inside scale_fill. Check also if you have duplicated start / start.1 lines.

p.s. I think that the range in the legend may be due to duplicated start/start.1 lines. In this case geom_tile sums the values for all the duplicated values, in some cases summing up to 25. Try to run PlotData[duplicated(paste(PlotData$start, PlotData$start.1))] to find duplicated rows.

Hello tomasscheitel!

We believe that this post does not fit the main topic of this site.

This is an R usage, not a bioinformatics-specific question, better suited to other forums.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

0 answers

No answers yet.

Log in to answer this question.