user3091668
user3091668

Reputation: 2310

Increase the levels at numeric legend in ggplot2

I would like to include more detailed legend in my ggplot. The current legend does not represent all my dot sizes. In the following example:

df <- "Freq Obs NumberOfWindows
        15 0.5  40
        12 0.4  80
        10 0.3  100
        8  0.2  800
       6   0.18 1300
       3   0.1  2000
       1   0.05 30000"

ResA <- read.table(text=df, header=T)


library(ggplot2)

 ggplot(ResA, aes(Freq, Obs, size=NumberOfWindows)) +
  geom_point() +
  xlab("Boundary frequency") +
  ylab("Average number of overlaps per window (10kb)") +
  ggtitle(as.character("The plot"))+ 
  theme_bw()+
  scale_size_continuous(name="area", range = c(1,20))

enter image description here

Please note that my numbers range from 40 to 30000. I have a large discrepancy there, however, I would like to have in the legend at least the largest and smallest dot. Otherwise the legend will not help much to small dots. Any idea here is highly appreciated.

Upvotes: 0

Views: 704

Answers (1)

Codutie
Codutie

Reputation: 1075

yes you have to add breaks inside your scale_size_continuous, something like this:

ggplot(ResA, aes(Freq, Obs, size=NumberOfWindows)) +
  geom_point() +
  xlab("Boundary frequency") +
  ylab("Average number of overlaps per window (10kb)") +
  ggtitle(as.character("The plot"))+ 
  theme_bw()+
  scale_size_continuous(name="area", 
                        range = c(1,20), 
                        breaks = ResA$NumberOfWindows)

result:

enter image description here

Upvotes: 5

Related Questions