0

我有一个示例脚本,它生成带有两个非零箱的直方图:

library(ggplot2)

# Make a dummy data set, containing 11 values on the interval (1,2), and a
# single value on the interval (3,4)
dftest <- data.frame(dummy_data=c(seq(1.1,1.9,0.08), 3.3))

# Create a histogram with 5 bins, on the interval (0,5)
hst <- ggplot(dftest, aes(x=dummy_data)) +
       geom_histogram(breaks=seq(0,5,1)) +
       theme_gray(base_size=18)

# Plot histogram with linear y-axis scaling; note there is a count present
# in the bin with edges (3,4)
print(hst)

# Plot histogram with logarithmic y-axis scaling; note the bin on the
# interval (3,4) cannot be displayed because the top of it is level
# with the plot baseline at 1e0.
print(hst + scale_y_log10())

该脚本生成两个图,我在下面附加了它们: 具有线性 y 轴缩放的 ggplot2 直方图 具有对数 y 轴缩放的 ggplot2 直方图,以及包含未正确绘制的单个计数的 bin

在对数缩放版本中,我怎样才能geom_histogram()将渲染的直方图基线向下移动到 1.0 以下(例如,在 0.1 处重新绘制基线),以便可以看到包含单个计数的 bin?

4

1 回答 1

3

包中的pseudo_log_trans转换scales非常有用地提供了线性和对数刻度之间的平滑过渡。

ggplot(dftest, aes(x=dummy_data)) +
  geom_histogram(breaks=seq(0,5,1)) +
  theme_gray(base_size=18) +
  scale_y_continuous(trans = scales::pseudo_log_trans(),
                     breaks = 0:10)

在此处输入图像描述

或者,从这个答案中借用技术,您可以使用 geom_rect 并假设“零”应该出现在您的对数刻度上。 https://stackoverflow.com/a/46664684/6851825

library(dplyr)
dftest %>%
  count(bin = floor(dummy_data)) %>%
  ggplot(aes(xmin = bin, xmax = bin+1,
             ymin = 0.1, ymax = n)) +
  geom_rect() +
  scale_y_log10()

在此处输入图像描述

于 2019-04-10T19:38:14.393 回答