为什么xlim会切断连续刻度的边界值?

top*_*hef 3 plot r ggplot2 axis-labels

给定数据框:

d = structure(list(bin_start = 1:12, 
                   bin_count = c(12892838L, 1921261L, 438219L, 126650L, 41285L, 
                                 15948L, 6754L, 3274L, 1750L, 992L, 703L, 503L)), 
              .Names = c("bin_start", "bin_count"), 
              class = "data.frame", 
              row.names = c(NA, 12L))
Run Code Online (Sandbox Code Playgroud)

我可以用stat="identity"以下方法构建直方图:

ggplot(d) +
  geom_histogram(aes(x=bin_start, y=bin_count), stat="identity", 
                 colour="black", fill="white") +
  scale_x_continuous(breaks=1:12)
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

在此输入图像描述

对长尾不满意我限制x比例(相当于xlim=c(1,6)):

ggplot(d) +
  geom_histogram(aes(x=bin_start, y=bin_count), stat="identity", colour="black", fill="white") +
  scale_x_continuous(breaks=1:12, limits=c(1,6)) 
Run Code Online (Sandbox Code Playgroud)

但我得到边界点x=1x=6消失:

在此输入图像描述

注意,y轴仍然像边界点一样缩放属于情节美学.它是一个功能还是一个bug?

Pey*_*ton 6

功能的副作用.由于您自己对数据进行了分类,因此您可能需要一个离散的比例,以及一个因素x:

ggplot(d) +
    geom_histogram(aes(x=factor(bin_start), y=bin_count), stat="identity", colour="black", fill="white") +
    scale_x_discrete(limit=as.character(1:6))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述