我收到一个 position_dodge()
需要非重叠 x 间隔`警告,其中包含以下数据和代码。如果我省略 width = 3 选项,那么我不会收到警告,但列几乎无法区分。 ggplot 代码的绘图在每组列之间都有很大的空间,因此我想知道是否有人对我需要更改哪些内容以摆脱警告提出建议。
library(ggplot2)
g_crop <- ggplot(cumsum_2022_melt, aes(fill = variable, y = quantity, x = as.Date(date))) +
geom_col(position="dodge", width = 3)
dput 输出
structure(list(date = structure(c(1657152000, 1657497600, 1657756800,
1657843200, 1658102400, 1658966400, 1659571200, 1660176000, 1660780800,
1661385600, 1661990400, 1662595200, 1663286400, 1663545600, 1664323200,
1657152000, 1657497600, 1657756800, 1657843200, 1658102400, 1658966400,
1659571200, 1660176000, 1660780800, 1661385600, 1661990400, 1662595200,
1663286400, 1663545600, 1664323200, 1657152000, 1657497600, 1657756800,
1657843200, 1658102400, 1658966400, 1659571200, 1660176000, 1660780800,
1661385600, 1661990400, 1662595200, 1663286400, 1663545600, 1664323200,
1657152000, 1657497600, 1657756800, 1657843200, 1658102400, 1658966400,
1659571200, 1660176000, 1660780800, 1661385600, 1661990400, 1662595200,
1663286400, 1663545600, 1664323200), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), variable = c("chard plot 1", "chard plot 1",
"chard plot 1", "chard plot 1", "chard plot 1", "chard plot 1",
"chard plot 1", "chard plot 1", "chard plot 1", "chard plot 1",
"chard plot 1", "chard plot 1", "chard plot 1", "chard plot 1",
"chard plot 1", "chard plot 2", "chard plot 2", "chard plot 2",
"chard plot 2", "chard plot 2", "chard plot 2", "chard plot 2",
"chard plot 2", "chard plot 2", "chard plot 2", "chard plot 2",
"chard plot 2", "chard plot 2", "chard plot 2", "chard plot 2",
"chard plot 3", "chard plot 3", "chard plot 3", "chard plot 3",
"chard plot 3", "chard plot 3", "chard plot 3", "chard plot 3",
"chard plot 3", "chard plot 3", "chard plot 3", "chard plot 3",
"chard plot 3", "chard plot 3", "chard plot 3", "chard plot 4",
"chard plot 4", "chard plot 4", "chard plot 4", "chard plot 4",
"chard plot 4", "chard plot 4", "chard plot 4", "chard plot 4",
"chard plot 4", "chard plot 4", "chard plot 4", "chard plot 4",
"chard plot 4", "chard plot 4"), quantity = c(0, 0, 0, 7.4, 7.4,
17.82, 27.37, 32.32, 35.37, 38.77, 41.62, 45.27, 45.27, 48.82,
54.17, 0, 0, 0, 0.35, 0.35, 0.35, 1.7, 3.3, 4.2, 6, 7.3, 7.3,
7.3, 10, 12.4, 0, 0, 0, 1, 1, 1, 1, 4.3, 9.55, 9.55, 10.05, 12.85,
12.85, 14.65, 16.65, 0, 0, 0, 2.3, 2.3, 9, 11.75, 13.85, 16.5,
19.55, 20.45, 21.95, 21.95, 23.2, 25.65)), row.names = c(NA,
-60L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x135841ce0>, index = structure(integer(0), "`__variable`" = integer(0)))
最佳答案
我不知道有什么区别,但我总是使用 dodge2
而不是 dodge
。它似乎也适用于您的情况:
https://ggplot2.tidyverse.org/reference/position_dodge.html
library(ggplot2)
#g_crop <-
ggplot(cumsum_2022_melt, aes(fill = variable, y = quantity, x = as.Date(date))) +
geom_col(position="dodge2", width=3)
左侧的条形发生了一些有趣的事情,它们比其余的条形要小。也许是因为记录的数据为零?这个链接谈到了它:Consistent width for geom_bar in the event of missing data
尝试这个解决方案:
ggplot(cumsum_2022_melt, aes(fill = variable, y = quantity, x = as.Date(date))) +
geom_bar(position=position_dodge2(preserve = "single"), stat="identity", width=2)
关于R ggplot2position_dodge()`需要非重叠的x间隔警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75253789/