繁体   English   中英

对角线上的偏移点 - 相似和 geom_jitter

[英]Off-set points in a diagonal line - similar and geom_jitter

如何使用分类 x 变量沿对角线偏移点?

有没有类似于position_jitter东西来实现这一点?

ggplot(mpg, aes(cyl, hwy)) + geom_point(position = position_jitter(width = 0.2))

在此示例中,每个cyl的最高hwy值应位于该类别的左上角,而最低的hwy值应位于右下角。

这是一个温和的解决方案:

library(tidyverse)

p1 <- ggplot(mpg, aes(cyl, hwy)) + geom_point()

diagonal_plot <- function(.plot) {
  p <- ggplot_build(.plot)
    p$data[[1]] <- 
       p$data[[1]] %>% 
       group_by(x) %>% 
       mutate(order_y = as.integer(factor(y))) %>% 
# making helper column for ranks depending on height of y
       ungroup %>% 
       mutate(x = x - order_y/100) %>% 
#this one was just an idea to create the offset to x depending on the rank of y
       select(-order_y)

plot(ggplot_gtable(p))

}

diagonal_plot(p1)

reprex 包(v0.2.0) 于2018年 12 月 27 日创建。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM