[英]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.