add custom numbers to ggplot legend

Viewed 4698

I'm drawing a geom_tile plot where i have each point with a specific p-value.

The plot is drawn in a nice way with the p-value limits on the legend.

What i want to add to this legend is the number of data points which have this p-value.

Example:

the legend is something like that (with a color for each value):

0.05
0.04
0.03
0.02
0.01

what i want to have is something like this:

0.05 58
0.04 34
0.03 85
0.02 54
0.01 10

how can i add this to the legend of ggplot2 plots?

I tried using geom_text and change the text position to the legend, but it did not worked.

UPDATE

pl <- ggplot(df.new) + geom_tile(aes(fill=z, x, y)) + 
      scale_fill_gradient(low="yellow", high="red", name="Legend1") + 
      stat_contour(breaks=c(0.001, 0.01, 0.05), aes(x=x, y=y, z=npvalue, colour=..level..)) +
      scale_colour_gradient2(low="black", mid="black", high="green", labels=paste(levels(c(0.001, 0.01, 0.05))))

I have this plot, I'm drawing the geom_tile correctly from the z variable then im drawing a stat_contour based on the p-value. And i want to edit the labels for the stat_contour colors which are based on the p-value as in my initial question, but i'm having this error :

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

Where is the problem in my plotting??

1 Answers
Related