Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 能否手动控制ggplot2几何点避开/重叠顺序?_R_Ggplot2_Ggproto - Fatal编程技术网

R 能否手动控制ggplot2几何点避开/重叠顺序?

R 能否手动控制ggplot2几何点避开/重叠顺序?,r,ggplot2,ggproto,R,Ggplot2,Ggproto,最近对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

最近对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)) 
库(dplyr)
图书馆(GG2)
图书馆(tibble)

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

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 ---------------------------------------------------------------------
库(ggplot2)
#我在爱达荷州的私人住所---------------------------------------------------

谢谢你发了一个小例子。但是,请考虑进一步的最小化:是否真的有必要加载<代码> DPLYR ,<代码> TiBL> 和猫>代码>来说明您的问题?您可以使用
col
group
来向自己证明这一点。只有减淡顺序是重要的,这是由
控制的(如果只提供
,则自动设置)。感谢前面问题的链接-这向我展示了将宽度值设置为负值(例如
位置=位置_减淡(宽度=-0.1)
将生成所需的输出。但是,这现在给了我一个警告:
位置闪避需要不重叠的x间隔
。不过这不是问题,因为它绘制得很好。位置闪避2
不应该是
新行为
而不是
旧行为
?新==新ggplot2默认值。旧==如何gg图2曾经这样做过
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
)