3

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??

tonytonov
  • 25,060
  • 16
  • 82
  • 98
ifreak
  • 1,726
  • 4
  • 27
  • 45
  • 2
    People are generally much more happy to help if you provide a [**minimal, reproducible example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and show the code you have tried. Thanks. – Henrik Dec 27 '13 at 23:14

1 Answers1

10

You can specify the labels of the legend for the fill colours.

An example dataset:

set.seed(1)
dat <- data.frame(x = rep(1:5), y = rep(1:10), 
                  z = factor(sample(seq(0.01, 0.05, 0.01), 100, TRUE)))

The function table counts the different values of dat$z:

table(dat$z)

# 0.01 0.02 0.03 0.04 0.05 
#   13   25   19   26   17

The plot:

library(ggplot2)
ggplot(dat) +
  geom_tile(aes(x = x, y = y, fill = z)) + 
  scale_fill_discrete(labels = paste(levels(dat$z), table(dat$z)))

In the last line, the labels of the legend are created.

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • thank you for you comment, this works, but i have a short update to the question which made things a bit more complicated for me and i'm getting an error. please have a look at the updated question. – ifreak Dec 28 '13 at 16:06
  • @ifreak Is your z variable not discrete but continuous? – Sven Hohenstein Dec 28 '13 at 16:50
  • the z variable is continuous, its basically the p-value but it has a lot of values other than the breaks, but what i want is to only draw the count for the values that are within the break – ifreak Dec 28 '13 at 22:39
  • @ifreak You can create cuts using `tmp <- cut(dat$z, seq(0, .5, .1))`. Then you can use this for your plot: `scale_fill_gradient(breaks = seq(0, .5, .1), labels = paste(levels(tmp), table(tmp)))`. – Sven Hohenstein Dec 31 '13 at 12:45