R geom_hist限制会删除限制范围内的数据

时间:2019-03-12 04:58:16

标签: r ggplot2

我正在尝试绘制一些具有相同x轴限制的不同直方图。为此,我从具有最大x轴限制的直方图中提取了x限制,并强制每个图都具有这些限制。但是,当我针对从中提取限制的相同数据自定义我的限制到该轴范围时,它删除了4个数据点。它们显然在轴限制的范围内-实际上,其中一个几乎在中间。知道是什么导致了这种现象吗?下面的代码有望使我清楚自己正在尝试

original <- ggplot(myData, aes (x = Signal, fill = Positivity)) + 
  geom_histogram(alpha = 0.2, position = "identity", color = "black") +
  scale_x_log10() + theme_bw() + xlab("Original Limits")

original_info<-ggplot_build(original) #get plot info
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
xlimlow <- 10^original_info$layout$panel_scales_x[[1]]$range$range[1] # get lower x axis limit, convert because log10
xlimhigh <- 10^original_info$layout$panel_scales_x[[1]]$range$range[2] # get upper x axis limit, convert because log10

hist_custom_limits <- ggplot(myData, aes (x = Signal, fill = Positivity)) + 
  geom_histogram(alpha = 0.2, position = "identity", color = "black") +
  scale_x_log10(limits = c(xlimlow, xlimhigh)) + theme_bw() + xlab("Custom Limits") 

对应的图形如下

Original Limits

enter image description here

以下是reprex的输出,以提供应重现问题的代码

rm(list = ls())
library(ggplot2)

myData <- data.frame(
                 Signal = c(258L, 290L, 470L, 167L, 133L, 183L, 2441L, 225L, 64L, 140L,
                            204L, 398L, 113L, 269L, 838L, 183L, 182L, 440L,
                            107L, 161L, 215L, 408L, 225L, 1920L, 1579L, 150L, 161L,
                            247L, 129L, 537L, 333L, 193L, 161L, 151L, 97L, 730L,
                            258L, 2234L, 129L, 226L, 86L, 343L, 107L, 183L, 226L,
                            236L, 1029L, 7308L, 376L, 140L, 516L, 269L, 204L,
                            483L, 140L, 440L, 333L),
             Positivity = c(0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L,
                            1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L,
                            0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L,
                            1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L,
                            1L, 0L, 0L)
          )
myData$Positivity <- factor(myData$Positivity)
levels(myData$Positivity) = c("Negative", "Positive")


original <- ggplot(myData, aes (x = Signal, fill = Positivity)) + 
  geom_histogram(alpha = 0.2, position = "identity", color = "black") +
  scale_x_log10() + theme_bw() + xlab("Original Limits")

original_info<-ggplot_build(original) #get plot info
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
xlimlow <- 10^original_info$layout$panel_scales_x[[1]]$range$range[1] # get lower x axis limit, convert because log10
xlimhigh <- 10^original_info$layout$panel_scales_x[[1]]$range$range[2] # get upper x axis limit, convert because log10

hist_custom_limits <- ggplot(myData, aes (x = Signal, fill = Positivity)) + 
  geom_histogram(alpha = 0.2, position = "identity", color = "black") +
  scale_x_log10(limits = c(xlimlow, xlimhigh)) + theme_bw() + xlab("Custom Limits") 

reprex package(v0.2.1)于2019-03-12创建

0 个答案:

没有答案