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