3

I have a problem with R ggplot2.

Im getting this error:

Error in scale_labels.continuous(scale, breaks) : 
  Breaks and labels are different lengths

even though im sure that breaks and labels are the same length.

is there anyway to debug the plots (i.e check the plot variable for breaks and labels to check where is the error)

the code im using is:

dataf$pval.d <- cut(dataf$pvalue, breaks=c(min(dataf$X),0.05,Inf), 
                    labels=c("min","0.05"), right = FALSE)

pl <-ggplot(dataf) + 
         geom_tile(aes(fill=C,A,B)) + 
         stat_contour(breaks = c(min(dataf$X),0.05),
                      aes(x=A,y=B,z=pvalue,colour = ..level..)) + 
         scale_colour_gradient2(low="black",mid="black",high="green",
            labels=paste(levels(dataf$pval.d),table(dataf$pval.d)),
            name="\n P-value \n")

a sample of the data frame dataf is something like that:

    A  B         C      pval
1   8  4 0.6714801 0.3138729
2   9  9 0.6373763 0.1402027
3   5  6 0.6695279 0.3426810
4  10  7 0.6338043 0.1722161
5   2  3 0.6692842 0.3301052
6   1  1 0.6728664 0.5777641
7   6  2 0.6608868 0.2637952
8   3 10 0.6721009 0.6584530
9   7  8 0.6338043 0.7839811
10  4  5 0.6728818 0.5045481

the output of:

> paste(levels(dataf$pval.d),table(dataf$pval.d))
[1] "min 0"   "0.05 10"

as you can see, the labels and breaks are of the same length, but maybe im making something wrong which im not aware about?

tonytonov
  • 25,060
  • 16
  • 82
  • 98
ifreak
  • 1,726
  • 4
  • 27
  • 45
  • provide data please. If you aren't aware, how could we be? – rawr Feb 05 '14 at 16:09
  • @rawr the problem is the data is too huge to provide here, but the idea is that why it says that breaks and labels have different lengths when they have the same length – ifreak Feb 05 '14 at 16:10
  • post the `str` of dataf, post a sample of the data, post the levels of `pval.d`, etc etc – rawr Feb 05 '14 at 16:12
  • @rawr i made an edit of the question, hope this is useful – ifreak Feb 05 '14 at 16:17
  • So how many breaks are in `scale_colour_gradient2`? I guess there are more than two. – Roland Feb 05 '14 at 16:19
  • @Roland i assume the breaks of `scale_colour_gradient2` takes the breaks from `stat_contour` – ifreak Feb 05 '14 at 16:23
  • I don't see a `breaks` argument in the documentation of `stat_contour`. So, why do you assume this? – Roland Feb 05 '14 at 16:27
  • @Roland because if u removed this part `labels=paste(levels(dataf$pval.d),table(dataf$pval.d))` from `scale_colour_gradient2` everything works fine but without the labels – ifreak Feb 05 '14 at 16:34
  • 1
    Sure it work fine then. Again, `stat_contour` doesn't have a `breaks` argument. `scale_colour_gradient2` does, but you don't pass it there and only provide the `labels` parameter that is not of the same length as the number of breaks. – Roland Feb 05 '14 at 16:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46864/discussion-between-ifreak-and-roland) – ifreak Feb 05 '14 at 16:49
  • @Roland : so there is no solution for this ? – ifreak Feb 06 '14 at 10:25
  • You haven't gotten an answer because you didn't provide a [reproducible example](http://stackoverflow.com/a/5963610/1412059) and didn't adequately define what you want to achieve. The error message you are getting is easy to understand and obvious. – Roland Feb 06 '14 at 10:32
  • @Roland : it is really confusing, since if i checked the `summary` of the plot, i look at the `stat_contour breaks`, and they are of length 2: `mapping: x = A y = B z = pvalue colour = ..level.. geom_path: na.rm = FALSE stat_contour: breaks = c(0.0231001371742112, 0.05) position_identity: (width = NULL, height = NULL)` and the labels printed in the question also have the same length – ifreak Feb 06 '14 at 10:40
  • `scale_colour_gradient2` is throwing you the error and **not** `stat_contour`. Is that so difficult to understand? – Roland Feb 06 '14 at 10:43
  • @Roland : how can i look at the breaks of `scale_colour_gradient2` ? they don't appear in the summary – ifreak Feb 06 '14 at 10:45
  • Don't use the `labels` argument and *look* at the graph legend. Or use `ggplotGrob` first to create the grob and than look at that. – Roland Feb 06 '14 at 10:59
  • @Roland : removing the labels only showed `one` value in the `scale_colour_gradient2` legend which is the `0.05`. but does this make sense? in the actual data there are values smaller and larger than 0.05. why only 0.05 is appearing ? – ifreak Feb 06 '14 at 12:41

0 Answers0