使用“jitterdodge”时对齐ggplot中的点和错误条`

使用“jitterdodge”时对齐ggplot中的点和错误条`,r,ggplot2,ggproto,R,Ggplot2,Ggproto,以下可复制数据包含每个季节(夏季和冬季)每只动物(猫和狗)对两个协变量(cov1和cov2)及其各自误差估计(SE)的50次观察 您可以扩展位置减淡以生成数据的修正抖动: myjit <- ggproto("fixJitter", PositionDodge, width = 0.3, dodge.width = 0.1, jit = NULL, comp

以下可复制数据包含每个季节(夏季和冬季)每只动物(猫和狗)对两个协变量(cov1和cov2)及其各自误差估计(SE)的50次观察


您可以扩展
位置减淡
以生成数据的修正
抖动

myjit <- ggproto("fixJitter", PositionDodge,
                 width = 0.3,
                 dodge.width = 0.1,
                 jit = NULL,
                 compute_panel =  function (self, data, params, scales) 
                 {

                   #Generate Jitter if not yet
                   if(is.null(self$jit) ) {
                    self$jit <-jitter(rep(0, nrow(data)), amount=self$dodge.width)
                   }

                   data <- ggproto_parent(PositionDodge, self)$compute_panel(data, params, scales)

                   data$x <- data$x + self$jit
                   #For proper error extensions
                   if("xmin" %in% colnames(data)) data$xmin <- data$xmin + self$jit
                   if("xmax" %in% colnames(data)) data$xmax <- data$xmax + self$jit
                   data
                 } )




ggplot(datLong, aes(y = Estimate, x = Cov, color = Species, group=Species)) +
  geom_point(position = myjit, size = 1) +
  geom_errorbar(aes(ymin = Estimate-SE, ymax = Estimate+SE), width = 0.2, position = myjit)+
  theme_bw() +
  facet_wrap(~ Season, ncol = 1, scales = "free") +
  scale_color_manual(values = c("blue", "red"))

myjit您可以扩展
position\u dodge
为数据生成一个补丁
jitter

myjit <- ggproto("fixJitter", PositionDodge,
                 width = 0.3,
                 dodge.width = 0.1,
                 jit = NULL,
                 compute_panel =  function (self, data, params, scales) 
                 {

                   #Generate Jitter if not yet
                   if(is.null(self$jit) ) {
                    self$jit <-jitter(rep(0, nrow(data)), amount=self$dodge.width)
                   }

                   data <- ggproto_parent(PositionDodge, self)$compute_panel(data, params, scales)

                   data$x <- data$x + self$jit
                   #For proper error extensions
                   if("xmin" %in% colnames(data)) data$xmin <- data$xmin + self$jit
                   if("xmax" %in% colnames(data)) data$xmax <- data$xmax + self$jit
                   data
                 } )




ggplot(datLong, aes(y = Estimate, x = Cov, color = Species, group=Species)) +
  geom_point(position = myjit, size = 1) +
  geom_errorbar(aes(ymin = Estimate-SE, ymax = Estimate+SE), width = 0.2, position = myjit)+
  theme_bw() +
  facet_wrap(~ Season, ncol = 1, scales = "free") +
  scale_color_manual(values = c("blue", "red"))

myjit相关问题。这似乎归结为你自己的紧张。谢谢你的参考。关于如何在分组因子中手动抖动作为现有问题的答案,您有什么想法吗?显示了手动抖动。看起来
jitter
使用统一分布,所以您可以自己从统一分布中提取,以创建新的“jittered”变量。相关问题。这似乎归结为你自己的紧张。谢谢你的参考。关于如何在分组因子中手动抖动作为现有问题的答案,您有什么想法吗?显示了手动抖动。看起来,
jitter
使用统一的分布,所以您可以自己从统一的分布中绘制,以创建新的“jittered”变量。
Jit <- position_jitterdodge(dodge.width=0.4)

ggplot(datLong, aes(y = Estimate, x = Cov, color = Species)) +
  geom_point(position = Jit, size = 1) +
  geom_errorbar(aes(ymin = Estimate-SE, ymax = Estimate+SE), width = 0.2, position = Jit) +
  theme_bw() +
  facet_wrap(~ Season, ncol = 1, scales = "free") +
  scale_color_manual(values = c("blue", "red"))
myjit <- ggproto("fixJitter", PositionDodge,
                 width = 0.3,
                 dodge.width = 0.1,
                 jit = NULL,
                 compute_panel =  function (self, data, params, scales) 
                 {

                   #Generate Jitter if not yet
                   if(is.null(self$jit) ) {
                    self$jit <-jitter(rep(0, nrow(data)), amount=self$dodge.width)
                   }

                   data <- ggproto_parent(PositionDodge, self)$compute_panel(data, params, scales)

                   data$x <- data$x + self$jit
                   #For proper error extensions
                   if("xmin" %in% colnames(data)) data$xmin <- data$xmin + self$jit
                   if("xmax" %in% colnames(data)) data$xmax <- data$xmax + self$jit
                   data
                 } )




ggplot(datLong, aes(y = Estimate, x = Cov, color = Species, group=Species)) +
  geom_point(position = myjit, size = 1) +
  geom_errorbar(aes(ymin = Estimate-SE, ymax = Estimate+SE), width = 0.2, position = myjit)+
  theme_bw() +
  facet_wrap(~ Season, ncol = 1, scales = "free") +
  scale_color_manual(values = c("blue", "red"))