使用geom_dotplot时绘图区域被截断

时间:2017-06-30 20:31:56

标签: r ggplot2

考虑以下示例数据:

ex = structure(list(group = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 
  2L, 3L, 4L, 6L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 
  2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 
  1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
  5L, 6L, 1L, 2L, 1L, 2L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 1L, 
  4L), .Label = c("A", "B", "C", "D", "E", "F"), class = "factor"), 
  ID = structure(c(35L, 35L, 35L, 35L, 35L, 35L, 1L, 1L, 1L, 
    1L, 1L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 9L, 9L, 
    9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
    11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 
    13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 21L, 21L, 22L, 22L, 
    22L, 22L, 2L, 3L, 4L, 5L, 8L, 15L, 16L, 17L, 18L, 19L, 19L, 
    20L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 30L, 31L, 32L, 
    33L, 34L), .Label = c("10", "107", "108", "109", "124", "17", 
      "18", "187", "19", "21", "24", "26", "27", "28", "335", "336", 
      "339", "340", "341", "342", "38", "39", "576", "577", "578", 
      "579", "580", "581", "582", "583", "584", "585", "586", "592", 
      "6"), class = "factor"), value = c(1L, 7L, 4L, 4L, 3L, 9L, 
        1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 5L, 1L, 2L, 1L, 2L, 2L, 
        2L, 1L, 33L, 27L, 28L, 21L, 28L, 1L, 3L, 1L, 1L, 1L, 1L, 
        2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 3L, 3L, 2L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L)), class = "data.frame", 
  row.names = c(NA, -88L), .Names = c("group", "ID", "value")
)

请注意,在A组中,value = 1代表ID。我使用ggplot2根据使用value的{​​{1}}变量的计数和geom_dotplot的分面创建点图:

group

即使使用ggplot(ex) + aes(x = value) + geom_dotplot(binwidth = 1, method = "histodot") + facet_wrap(~ group) 导出,第一个构面中的点堆也会被切断。更改y轴限制没有任何效果,但是改变宽高比使得H> = W似乎解决了问题(通常通过向顶部添加比必要更多的空间)。 这是一个错误,还是我错误地指定了我的点图?

修改

一种解决方法是通过ggsave变量翻转我的dotplot和bin:

y

2 个答案:

答案 0 :(得分:2)

可以帮助您的其他两个参数是stackratiodotsize。例如:

ggplot(ex) + aes(x = value) + 
  geom_dotplot(binwidth = 1, method = "histodot", stackratio = 0.9, dotsize = .75) +
  facet_wrap(~ group)  +
  scale_y_continuous(NULL, breaks = NULL)

您需要调整数字,直到获得所需的布局。

答案 1 :(得分:0)

我找到了一个有趣的解决方法,使用geom_bar实现了与点图相同的结构,但是使用了矩形:

ggplot(ex) + aes(x = value, group = ID) + 
  geom_bar(color = "black", fill = "white", width = 1) +
  facet_wrap(~ group) 

虽然它会产生矩形(而不是点),但您无法控制堆栈间距。根据绘图窗口调整​​矩形的大小,这相当于调整geom_dotplot中的点大小。此外,它引出了一个问题“为什么不只是使用常规条形图?”