我想geom_point在facet_grid. 但我希望我的情節僅在 x 軸上被躲避!看起來像position_dodge()躲避了沿 y 軸和 x 軸的點!
我如何控制我可以躲避哪個軸:
我的資料類似于以下內容:
carat cut color clarity depth table price x y z grade
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr>
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 low-quality
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 High-quality
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 low-quality
4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63 High-quality
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 low-quality
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 High-quality
...
我的代碼如下:
library(ggplot2)
library(dplyr)
library(tidyverse)
dmnd_data <- diamonds %>% head(100) %>% mutate(grade = ifelse(cut == "Premium", "High-quality", ifelse(cut == "Very Good", "High-quality", "low-quality"))
dmnd_data %>% ggplot(aes(color, cut, size = price))
geom_point(alpha = 0.7, position = position_dodge2(width = 0.5))
facet_grid(grade~., space = "free", scales = "free")
正如您在下圖中看到的,每個 x 坐標的點都沒有居中,它們在每個網格內沿兩個軸都被躲避!有沒有辦法控制每個點僅沿 x 軸閃避facet_grid?
facet_grid 圖
uj5u.com熱心網友回復:
在我看來,躲避效果很好,因為每種顏色的點都會被躲避。但是,如果我理解正確,您希望為color和 的每個組合分別躲避點數cut。不確定這是否可以通過position_dodge或 ...來實作,但是在一些資料整理和切換到連續規模的幫助下,您可以執行以下操作:
library(ggplot2)
library(dplyr)
dmnd_data <- diamonds %>%
mutate(grade = ifelse(cut == "Premium", "High-quality", ifelse(cut == "Very Good", "High-quality", "low-quality"))) %>%
head(100)
width <- .5
dmnd_data <- dmnd_data %>%
select(cut, color, grade, price) %>%
group_by(grade, cut, color) %>%
mutate(nudge = if (n() > 1) seq(-width/2, width/2, length.out = n()) else 0) %>%
ungroup()
color_lvls <- levels(dmnd_data$color)
ggplot(dmnd_data, aes(color, cut, size = price))
geom_point(aes(as.numeric(color) nudge), alpha = 0.7)
scale_x_continuous(breaks = seq_along(color_lvls), labels = color_lvls)
facet_grid(grade ~ ., space = "free", scales = "free")

uj5u.com熱心網友回復:
不position_nudge()完成你想要的是什么?
library(tidyverse)
dmnd_data <- diamonds %>%
head(100) %>%
mutate(
grade = ifelse(
cut == "Premium",
"High-quality",
ifelse(cut == "Very Good", "High-quality", "low-quality")
))
dmnd_data %>% ggplot(aes(color, cut, size = price))
geom_point(alpha = 0.7, position = position_nudge(x = 0.5))
facet_grid(grade ~ ., space = "free", scales = "free")

由reprex 包(v0.3.0)于 2021 年 12 月 6 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376440.html
