gpt4 book ai didi

r - geom_dotplot 中未对齐的点

转载 作者:行者123 更新时间:2023-12-02 02:46:36 24 4
gpt4 key购买 nike

我有一个点图,但出于某种原因,并不是所有的点都沿着中心对齐。相反,这些点似乎“漂浮”在每个类别的中心网格线上方。我怎样才能调整他们的位置?

enter image description here

当前图代码:

ggplot(data = filter(lastDet,
!ID %in% c("NB2015.341", "NB2018.7",
"NB2018.43", "NB2018.45")),
aes(x = speciesEN, y = yday(depDate))) +
geom_dotplot(aes(fill = factor(year), colour = factor(year)),
binaxis = 'y', stackdir = 'center', dotsize = 0.7,
stackgroups = TRUE,
binpositions = "all",
method = "histodot") +
stat_summary(fun.y = mean, geom = "point",
colour = "black", shape = 4, size = 4) +
scale_y_continuous(limits = c(181, max(yday(lastDet$depDate))),
breaks = c(181, 196, 212, 227, 243),
labels = c("1 Jul", "15 Jul", "1 Aug",
"15 Aug", "1 Sep")) +
scale_colour_manual(values = c("white", "white", "white"),
guide = FALSE) +
scale_fill_manual(values = c("#CA3542", "#FEBC38", "#849FAD")) +
coord_flip() +
labs(y = "Departure date",
x = "",
fill = "Year") +
theme_bw() +
theme(panel.grid.minor = element_blank())

编辑:

重复问题中提供的“hacky”解决方案不适用于我较大的数据框。

编辑 2:

根据要求,这是我的数据框的一个子集。我试图让它尽可能具有代表性。

dput(lastDet.sub)
structure(list(speciesEN = c("Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Bank Swallow", "Bank Swallow", "Bank Swallow",
"Bank Swallow", "Barn Swallow", "Barn Swallow", "Barn Swallow",
"Barn Swallow", "Barn Swallow", "Barn Swallow", "Barn Swallow",
"Barn Swallow", "Barn Swallow", "Barn Swallow", "Barn Swallow",
"Barn Swallow", "Barn Swallow", "Barn Swallow", "Barn Swallow",
"Barn Swallow", "Barn Swallow", "Barn Swallow", "Barn Swallow",
"Barn Swallow", "Cliff Swallow", "Cliff Swallow", "Cliff Swallow",
"Cliff Swallow", "Cliff Swallow", "Cliff Swallow", "Cliff Swallow",
"Cliff Swallow", "Cliff Swallow", "Cliff Swallow", "Tree Swallow",
"Tree Swallow", "Tree Swallow", "Tree Swallow", "Tree Swallow",
"Tree Swallow", "Tree Swallow", "Tree Swallow", "Tree Swallow",
"Tree Swallow"), year = c(2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015, 2015, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018
), depDate = structure(c(1531737145.3837, 1534093849.7991, 1530997725.9412,
1531076069.5422, 1533041446.3001, 1531076076.231, 1533032604.2326,
1534086023.11, 1531737149.7107, 1532882823.5637, 1406899801.4133,
1406297348.1112, 1407276094.4158, 1405278154.7126, 1406330632.0613,
1407490305.4993, 1406370738.3239, 1406655731.0996, 1406552025.8514,
1406366734.892, 1405371083.9787, 1405935159.2899, 1406656817.3111,
1405964739.2527, 1405935269.2718, 1407405240.0903, 1406416984.3628,
1407078925.0925, 1406201767.0139, 1406285074.475, 1407362335.7969,
1405886217.5548, 1406586545.9097, 1406123078.5834, 1439577132.3188,
1439594526.7378, 1438097832.985, 1438097743.3089, 1438470481.0184,
1438246572.18, 1438252727.6824, 1436190169.4567, 1438595642.3985,
1438728341.4292, 1437298553.0341, 1437573409.4331, 1439402462.271,
1437577437.0105, 1438209133.1853, 1438006270.5975, 1438424815.9509,
1438260465.2748, 1439561952.8794, 1441068339.0791, 1437322842.1042,
1438794640.5246, 1439673494.5053, 1439825890.7982, 1438173337.2322,
1438805137.1318, 1438083434.1402, 1436372532.3594, 1435960025.2834,
1439508786.8144, 1439668992.4789, 1439545490.7975, 1438962415.0014,
1437768970.3123, 1438423416.3447, 1438251288.8178, 1439757591.1317,
1437844819.4679, 1438247033.9429, 1439141690.47, 1438207292.6573,
1438171703.2655, 1438277212.7584, 1439199695.1607, 1534507807.6186,
1532702235.5266, 1533820579.7317, 1532957266.8756, 1531824345.5634,
1532598380.2699, 1532098828.52, 1533568517.8915, 1533407138.0839,
1535885104.009, 1533305719.4551, 1535042919.6962, 1533555209.5058,
1535132109.2126, 1534526297.7543, 1535543329.7974, 1535797758.1752,
1535538134.3428, 1535279656.0938, 1532184581.5469, 1534778864.0948,
1534708212.9832, 1534862676.3394, 1535027042.7154, 1534936249.3542,
1535211828.7767, 1534936232.9197, 1535227479.6166, 1534506480.411,
1533161753.2014, 1533115007.6647, 1531570748.0769, 1533673583.6144,
1533201700.8021, 1531239978.3688, 1531843656.4454, 1532604093.761,
1530790685.643, 1531132089.8288, 1530533431.1395, 1530787169.1414,
1530471991.9423, 1530643624.7047, 1530474331.461, 1531679979.9343,
1531673724.9375, 1531579988.1816, 1531394352.5095, 1531831107.0235
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -127L))

最佳答案

我不经常使用geom_dotplot,所以不能说我是最熟悉它的。但是根据示例数据,似乎 x 分面并将每个 x 值分配给不同的分面面板可以解决对齐问题。看看这是否适用于您的大型数据框?

# original plot as per question, other than the data portion since lastDet has no ID to filter
p1 <- ggplot(data = lastDet,
aes(x = speciesEN, y = yday(depDate))) +
geom_dotplot(aes(fill = factor(year), colour = factor(year)),
binaxis = 'y', stackdir = 'center', dotsize = 0.7,
stackgroups = TRUE,
binpositions = "all",
method = "histodot") +
stat_summary(fun.y = mean, geom = "point",
colour = "black", shape = 4, size = 4) +
scale_y_continuous(limits = c(181, max(yday(lastDet$depDate))),
breaks = c(181, 196, 212, 227, 243),
labels = c("1 Jul", "15 Jul", "1 Aug",
"15 Aug", "1 Sep")) +
scale_colour_manual(values = c("white", "white", "white"),
guide = FALSE) +
scale_fill_manual(values = c("#CA3542", "#FEBC38", "#849FAD")) +
coord_flip() +
labs(y = "Departure date",
x = "",
fill = "Year") +
theme_bw() +
theme(panel.grid.minor = element_blank())

# alignment fixed by facet
p2 <- p1 +
facet_grid(forcats::fct_rev(speciesEN) ~ ., # use fct_rev to reverse factor levels & match
# x-axis order (can replace with simply
# `speciesEN ~ .`, if this isn't needed)
scales = "free", space = "free") +
theme(strip.background = element_blank(),
strip.text = element_blank(),
panel.spacing = unit(0, "pt"))

p2

默认情况下,每个构面面板都具有相同的大小。相同位置的点数不影响比例限制,因此在 facet_grid 中设置 space = "free" 不会改变分配给每个面的空间量.由于“Bank Swallow”分面面板具有比其他面板更多的具有相同出发日期的点,因此我在 p2 中使用了非常大的高宽比来为每个分面提供足够的空间。

但是,如果我们负担不起这么大的面板尺寸,我们也可以手动计算最大点数:

rel.width <- layer_data(p2) %>%
select(PANEL, y, fill, colour, count) %>%
unique() %>%
group_by(PANEL, y) %>%
summarise(count = sum(count)) %>%
summarise(count = max(count),
y = mean(y, na.rm = TRUE)) %>%
rename(speciesEN = PANEL)
levels(rel.width$speciesEN) <- rev(sort(unique(lastDet$speciesEN)))

> rel.width
# A tibble: 4 x 3
speciesEN count y
<fct> <dbl> <dbl>
1 Tree Swallow 2 192.
2 Cliff Swallow 3 200
3 Barn Swallow 5 229.
4 Bank Swallow 12 210.

并将其作为不可见层添加到图中,以强制每个面调整其空间:

p3 <- p2 +
geom_tile(data = rel.width,
aes(y = y, width = count, height = 1),
alpha = 0)

p3

关于r - geom_dotplot 中未对齐的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54406134/

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