r - 为什么 xlim 会切断连续尺度的边界值?

标签 r plot 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))

我可以用 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)

看起来像这样:

enter image description here

对长尾不满意我限制了 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)) 

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

enter image description here

请注意,y 轴仍然像边界点一样缩放,属于情节美学。这是一个功能还是一个错误?

最佳答案

功能的副作用。由于您自己对数据进行了分箱,因此您可能需要离散比例和 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))

enter image description here

关于r - 为什么 xlim 会切断连续尺度的边界值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18211170/

相关文章:

r - 如何让 webshot 与 bookdown 一起使用?

从R向量中没有小数的数字中删除前导零

r - 使用 bam() 和 family = betar 时出现 GAM 错误

matlab - 如何使用 subplot 绘制导入的 .fig 文件中的所有内容(同时保留图例+轴+标签)?

python - 使用 matplotlib.colors.Colormap 将标量值映射到 RGB 时出现奇怪的颜色图

python - 将 pandas 条形图的图例与次要 y 轴放在条形图前面

r - 除了动画直方图之外,如何向 gganimate 添加移动 vline?

r - 如何在表格的每个单元格中进行字符串拆分后获取唯一值和计数

r - 如何在ggplot2的geom_bracket中添加数学/下标?

r - R 中出现错误 - [错误 : could not find function "qplot"]