增加 ggplot2 中数字图例的级别

Increase the levels at numeric legend in ggplot2

我想在我的 ggplot 中包含更详细的图例。当前的图例并不代表我所有的点大小。在以下示例中:

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

请注意我的数字在 40 到 30000 之间。我在那里有很大的差异,但是,我想在图例中至少有最大和最小的点。否则图例对小点没有多大帮助。非常感谢这里的任何想法。

是的,您必须在 scale_size_continuous 中添加中断,如下所示:

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)

结果: