【问题标题】:Can you manually control ggplot2 geom_point dodging/overlapping order? [duplicate]您可以手动控制 ggplot2 geom_point 躲避/重叠顺序吗? [复制]
【发布时间】:2017-01-03 13:54:56
【问题描述】:

最近对 ggplot2 (2.2.0) 的更新破坏了我们的一些绘图,因为绘制点的顺序发生了变化。比如下面的代码:

library(dplyr)
library(ggplot2)
library(tibble)


df <- tribble(~a, ~x, ~y,
              "I", "A",  2,
              "I", "B",  3,
              "II","A",  2,
              "II","B",  3)

ggplot(df, aes(x = x, y = y, color = a)) +
  geom_point(size = 5, position = position_dodge(width = 0.1))+
  coord_cartesian(ylim = c(0,15)) 

在 ggplot2 的两个最新版本之间产生不同的版本,

注意点重叠顺序的不同(即新版本与顶部最左边的点重叠)。我可以通过反转因子顺序来反转类别重叠的顺序:

library(dplyr)
library(ggplot2)
library(tibble)
library(forcats)


df <- tribble(~a, ~x, ~y,
              "I", "A",  2,
              "I", "B",  3,
              "II","A",  2,
              "II","B",  3)

ggplot(df, aes(x = x, y = y, color = fct_rev(a))) +
  geom_point(size = 5, position = position_dodge(width = 0.1))+
  coord_cartesian(ylim = c(0,15)) 

但这无济于事,因为它现在还颠倒了躲避顺序。

有谁知道重现以前行为的任何方法?是否可以在不改变闪避顺序的情况下手动反转点的绘制顺序?

【问题讨论】:

  • 感谢发布一个小例子。但是,请考虑进一步简化:是否真的需要加载 dplyr、tibble 和 forcats 来说明您的问题?
  • 左边的点将始终绘制在顶部。您可以同时使用col 和group 来向自己证明这一点。只有闪避顺序很重要,这由group 控制(如果只提供col,则会自动设置)。
  • 感谢上一个问题的链接 - 这向我展示了将宽度值设置为负数(例如 position = position_dodge(width = -0.1) 将产生所需的输出。但是,现在这给了我一个警告:@ 987654334@. 不过这不是问题,因为它绘制得非常好

标签: r ggplot2 ggproto


【解决方案1】:

建立自己的位置闪避也不是太难:

library(ggplot2)

# My private Idaho^H^H^H^H^Hdodge ---------------------------------------------------

collide2 <- function(data, width = NULL, name, strategy, check.width = TRUE) {
  if (!is.null(width)) {
    if (!(all(c("xmin", "xmax") %in% names(data)))) {
      data$xmin <- data$x - width / 2
      data$xmax <- data$x + width / 2
    }
  } else {
    if (!(all(c("xmin", "xmax") %in% names(data)))) {
      data$xmin <- data$x
      data$xmax <- data$x
    }
    widths <- unique(data$xmax - data$xmin)
    widths <- widths[!is.na(widths)]
    width <- widths[1]
  }

  ####### THIS is the line that was added that is causing you angst
  # data <- data[order(data$xmin, -data$group), ]

  intervals <- as.numeric(t(unique(data[c("xmin", "xmax")])))
  intervals <- intervals[!is.na(intervals)]

  if (length(unique(intervals)) > 1 & any(diff(scale(intervals)) < -1e-6)) {
    warning(name, " requires non-overlapping x intervals", call. = FALSE)
  }

  if (!is.null(data$ymax)) {
    plyr::ddply(data, "xmin", strategy, width = width)
  } else if (!is.null(data$y)) {
    data$ymax <- data$y
    data <- plyr::ddply(data, "xmin", strategy, width = width)
    data$y <- data$ymax
    data
  } else {
    stop("Neither y nor ymax defined")
  }
}

position_dodge2 <- function(width = NULL) {
  ggproto(NULL, PositionDodge2, width = width)
}

PositionDodge2 <- ggproto(
  "PositionDodge", 
  Position,
  required_aes = "x",
  width = NULL,
  setup_params = function(self, data) {
    if (is.null(data$xmin) && is.null(data$xmax) && is.null(self$width)) {
      warning("Width not defined. Set with `position_dodge(width = ?)`",
              call. = FALSE)
    }
    list(width = self$width)
  },

  compute_panel = function(data, params, scales) {
    collide2(data, params$width, "position_dodge2", ggplot2:::pos_dodge, check.width = FALSE)
  }
)

# End new Dodge ---------------------------------------------------------------------

测试…

library(dplyr)
library(tibble)
library(gridExtra)

df <- tribble(~a, ~x, ~y,
              "I", "A",  2,
              "I", "B",  3,
              "II","A",  2,
              "II","B",  3)

grid.arrange(
  ggplot(df, aes(x = x, y = y, color = a)) +
    geom_point(size = 5, position = position_dodge2(width = 0.1)) +
    coord_cartesian(ylim = c(0,15)) +
    labs(title="Old behaviour")
  ,
  ggplot(df, aes(x = x, y = y, color = a)) +
    geom_point(size = 5, position = position_dodge(width = 0.1)) +
    coord_cartesian(ylim = c(0,15)) +
    labs(title="New behaviour")
  ,
  ncol=2
)

【讨论】:

  • 不应该position_dodge2 是New behavior 而不是Old behavior?
  • 新 == 新的 ggplot2 默认值。旧 == ggplot2 过去是如何做到的
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 2013-03-20
相关资源
最近更新 更多