簡體   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