gpt4 book ai didi

r - 在 ggplot2 中添加完全相同数量的水平传播/"jitter"/闪避

转载 作者:行者123 更新时间:2023-12-01 23:08:09 24 4
gpt4 key购买 nike

我要绘制三个组的测量值,每个组有五个观察值。我想绘制所有点,但是每个组中的数值都非常接近。改变 alpha 有帮助,但仍然很难观察到单独的点。

因此我想添加一些水平扩展(在 X 轴上)。点的部分重叠是可以的。 geom_jitter() 可以实现这一点,但增加了随机分布,而我想在 X 轴上均匀分布五个点,因为水平轴上的数据没有随机性。

Geom_dotplot() 可以创建均匀分布,但据我所知,它仅适用于离散/合并值。

这是我的 ggplot 输出,以及包含 tribble 格式数据和 ggplot 代码的可重现代码:

enter image description here





library(tidyverse)

df <- tribble(
~projection, ~E.DAP, ~Shielding,
"AP", 2.88755980861244e-06, "None",
"AP", 2.87626262626263e-06, "None",
"AP", 2.87e-06, "None",
"AP", 2.87702265372168e-06, "None",
"AP", 2.87775551102204e-06, "None",
"CRAN", 2.93893129770992e-06, "None",
"CRAN", 2.92857142857143e-06, "None",
"CRAN", 2.93517017828201e-06, "None",
"CRAN", 2.9375e-06, "None",
"CRAN", 2.93831168831169e-06, "None",
"LAO", 6.88053097345133e-06, "None",
"LAO", 6.90217391304348e-06, "None",
"LAO", 6.84489795918367e-06, "None",
"LAO", 6.84792626728111e-06, "None",
"LAO", 6.86425339366516e-06, "None",
"LAO-CRAN", 7.11216730038023e-06, "None",
"LAO-CRAN", 7.1039501039501e-06, "None",
"LAO-CRAN", 7.1042471042471e-06, "None",
"LAO-CRAN", 7.09368635437882e-06, "None",
"LAO-CRAN", 7.10183299389002e-06, "None",
"RAO", 2.48e-06, "None",
"RAO", 2.47540983606557e-06, "None",
"RAO", 2.46979865771812e-06, "None",
"RAO", 2.46206896551724e-06, "None",
"RAO", 2.453125e-06, "None",
"RAO-CRAN", 1.87972508591065e-06, "None",
"RAO-CRAN", 1.87068965517241e-06, "None",
"RAO-CRAN", 1.88461538461538e-06, "None",
"RAO-CRAN", 1.86816720257235e-06, "None",
"RAO-CRAN", 1.8735632183908e-06, "None",
"LAO-CAUD", 6.03324808184143e-06, "None",
"LAO-CAUD", 6.06502242152466e-06, "None",
"LAO-CAUD", 6.04785894206549e-06, "None",
"LAO-CAUD", 6.0402144772118e-06, "None",
"LAO-CAUD", 6.02949061662198e-06, "None",
"RAO-CAUD", 6.30573248407643e-07, "None",
"RAO-CAUD", 6.29834254143646e-07, "None",
"RAO-CAUD", 6.35467980295566e-07, "None",
"RAO-CAUD", 6.38297872340426e-07, "None",
"RAO-CAUD", 6.37755102040816e-07, "None",
"CAUD", 3.81597222222222e-06, "None",
"CAUD", 3.83083511777302e-06, "None",
"CAUD", 3.8330550918197e-06, "None",
"CAUD", 3.82936507936508e-06, "None",
"CAUD", 3.81614349775785e-06, "None",
"AP", 4.53580901856764e-07, "Standard",
"AP", 4.46745562130178e-07, "Standard",
"AP", 4.4973544973545e-07, "Standard",
"AP", 4.44976076555024e-07, "Standard",
"AP", 4.52380952380952e-07, "Standard",
"CRAN", 1.20574162679426e-06, "Standard",
"CRAN", 1.20113314447592e-06, "Standard",
"CRAN", 1.19130434782609e-06, "Standard",
"CRAN", 1.19349593495935e-06, "Standard",
"CRAN", 1.19757575757576e-06, "Standard",
"LAO", 1.50961538461538e-06, "Standard",
"LAO", 1.50761421319797e-06, "Standard",
"LAO", 1.51209677419355e-06, "Standard",
"LAO", 1.52216748768473e-06, "Standard",
"LAO", 1.51476793248945e-06, "Standard",
"LAO-CRAN", 2.96213425129088e-06, "Standard",
"LAO-CRAN", 2.95991561181435e-06, "Standard",
"LAO-CRAN", 2.95e-06, "Standard",
"LAO-CRAN", 2.95744680851064e-06, "Standard",
"LAO-CRAN", 2.95266272189349e-06, "Standard",
"RAO", 1.51515151515152e-07, "Standard",
"RAO", 1.52173913043478e-07, "Standard",
"RAO", 1.52866242038217e-07, "Standard",
"RAO", 1.57575757575758e-07, "Standard",
"RAO", 1.59420289855072e-07, "Standard",
"RAO-CRAN", 2.41379310344828e-07, "Standard",
"RAO-CRAN", 2.33050847457627e-07, "Standard",
"RAO-CRAN", 2.34741784037559e-07, "Standard",
"RAO-CRAN", 2.33812949640288e-07, "Standard",
"RAO-CRAN", 2.34817813765182e-07, "Standard",
"LAO-CAUD", 1.89125295508274e-07, "Standard",
"LAO-CAUD", 1.87110187110187e-07, "Standard",
"LAO-CAUD", 1.89309576837416e-07, "Standard",
"LAO-CAUD", 1.90821256038647e-07, "Standard",
"LAO-CAUD", 1.89189189189189e-07, "Standard",
"AP", 3.7542662116041e-08, "XRB",
"AP", 3.89972144846797e-08, "XRB",
"AP", 4.15335463258786e-08, "XRB",
"AP", 3.83275261324042e-08, "XRB",
"AP", 4.09556313993174e-08, "XRB",
"CRAN", 4e-08, "XRB",
"CRAN", 4.08163265306122e-08, "XRB",
"CRAN", 4.06905055487053e-08, "XRB",
"CRAN", 3.94574599260173e-08, "XRB",
"CRAN", 3.90835579514825e-08, "XRB",
"LAO", 6.47249190938511e-08, "XRB",
"LAO", 6.69144981412639e-08, "XRB",
"LAO", 6.42570281124498e-08, "XRB",
"LAO", 6.19834710743802e-08, "XRB",
"LAO", 6.25e-08, "XRB",
"LAO-CRAN", 5.31914893617021e-08, "XRB",
"LAO-CRAN", 5.11247443762781e-08, "XRB",
"LAO-CRAN", 5.30821917808219e-08, "XRB",
"LAO-CRAN", 5.17857142857143e-08, "XRB",
"LAO-CRAN", 5.31732418524871e-08, "XRB",
"RAO", 3.30578512396694e-08, "XRB",
"RAO", 3.38983050847458e-08, "XRB",
"RAO", 3.33333333333333e-08, "XRB",
"RAO", 3.07017543859649e-08, "XRB",
"RAO", 3.2171581769437e-08, "XRB",
"RAO-CRAN", 3.44827586206897e-08, "XRB",
"RAO-CRAN", 3.5264483627204e-08, "XRB",
"RAO-CRAN", 3.25581395348837e-08, "XRB",
"RAO-CRAN", 3.43007915567282e-08, "XRB",
"RAO-CRAN", 3.37837837837838e-08, "XRB",
"LAO-CAUD", 5.70776255707763e-08, "XRB",
"LAO-CAUD", 5.50847457627119e-08, "XRB",
"LAO-CAUD", 5.51876379690949e-08, "XRB",
"LAO-CAUD", 5.42797494780793e-08, "XRB",
"LAO-CAUD", 5.53191489361702e-08, "XRB",
"RAO-CAUD", 2.1505376344086e-08, "XRB",
"RAO-CAUD", 1.52990264255911e-08, "XRB",
"RAO-CAUD", 1.58033362598771e-08, "XRB",
"RAO-CAUD", 1.47679324894515e-08, "XRB",
"RAO-CAUD", 1.60213618157543e-08, "XRB",
"CAUD", 3.84024577572965e-08, "XRB",
"CAUD", 3.98671096345515e-08, "XRB",
"CAUD", 4e-08, "XRB",
"CAUD", 3.92156862745098e-08, "XRB",
"CAUD", 3.94574599260173e-08, "XRB",
"RAO-CAUD", 4.52961672473868e-08, "Standard",
"RAO-CAUD", 4.12844036697248e-08, "Standard",
"RAO-CAUD", 4.83870967741936e-08, "Standard",
"RAO-CAUD", 4.29184549356223e-08, "Standard",
"RAO-CAUD", 4.38356164383562e-08, "Standard",
"CAUD", 8.58974358974359e-08, "Standard",
"CAUD", 8.70646766169154e-08, "Standard",
"CAUD", 8.66372980910426e-08, "Standard",
"CAUD", 8.67579908675799e-08, "Standard",
"CAUD", 8.70113493064313e-08, "Standard",
"LAO90", 3.38266384778013e-08, "XRB",
"LAO90", 3.34346504559271e-08, "XRB",
"LAO90", 3.2258064516129e-08, "XRB",
"LAO90", 3.21543408360129e-08, "XRB",
"LAO90", 3.20987654320988e-08, "XRB",
"LAO90", 8.60215053763441e-08, "Standard",
"LAO90", 8.88888888888889e-08, "Standard",
"LAO90", 8.44686648501362e-08, "Standard",
"LAO90", 8.90804597701149e-08, "Standard",
"LAO90", 8.67052023121387e-08, "Standard",
"LAO90", 1.96982378854626e-05, "None",
"LAO90", 1.97443820224719e-05, "None",
"LAO90", 1.96883720930233e-05, "None",
"LAO90", 1.96856540084388e-05, "None",
"LAO90", 1.96415770609319e-05, "None"
)

ggplot(df , aes(x=projection, y=E.DAP, color=Shielding)) +
geom_jitter(size = 2.5, width=0.2, height=0, alpha =0.8) +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1, size=8)) +
labs(title= "mSv/DAP according to projection",
y="mSv/mGy*cm2 ", x="")+
theme(legend.position = c(0.85, 0.8), legend.key = element_rect(colour = "transparent", fill = "transparent")) +
scale_color_manual(values=c(c("#69ac5c", "#5b86c3","#c7533b"))) +
theme(legend.title = element_blank())

最佳答案

ggbeeswarm 可以使用...可以使用不同的形状或 geom_beeswarm 的参数来增强外观:alphadodge.widthgroupOnX .

设置dodge.width 强制点按Shielding 变量分组。更新后的答案名义上包括 0.1 的值,将 dodge.width 设置为更接近于零使得看起来好像没有躲闪并将来自相同屏蔽值的点保持在一起。较大的 dodge.width 值将有助于区分 x 变量的点,例如“CAUD”。

为确保 Shielding 的重叠绘制是一致的,请创建绘图顺序变量。通过 Shielding 排列数据任意设置,以便最后一个变量绘制在最后,这可以更改以适应。

library(ggplot2)
library(dplyr)
library(ggbeeswarm)


df1 <-
df %>%
arrange(Shielding) %>%
mutate(plot_order = row_number())

ggplot(df1, aes(x = projection, y = E.DAP, color = Shielding, order = plot_order)) +
geom_beeswarm(size = 2.5, alpha = 0.9, dodge.width = 0.1) +
scale_color_manual(values=c(c("#69ac5c", "#5b86c3","#c7533b"))) +
labs(title= "mSv/DAP according to projection",
y="mSv/mGy*cm2 ", x="")+
theme(legend.title = element_blank(),
legend.position = c(0.85, 0.8),
legend.key = element_rect(colour = "transparent", fill = "transparent"),
axis.text.x = element_text(angle = 45, vjust = 1, hjust=1, size=8))

reprex package 创建于 2021-12-21 (v2.0.1)

关于r - 在 ggplot2 中添加完全相同数量的水平传播/"jitter"/闪避,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70434268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com