使用 scale_x_datetime() 设置限制会在 ggplot 中默默地删除栏
setting limits with scale_x_datetime() silently drops bar in ggplot
主要目标:我正在尝试使用一组 x 和 y 限制和比例从不同的 tibbles 构建一系列条形图,以便于跨图进行比较。 x-axis 是日期时间,y-axis 是数字。我使用小标题集中最具包容性的 x 和 y 范围来定义所有绘图的限制和比例。
挑战:当我向 scale_x_datetime() 添加限制以设置 x-axis 范围时,ggplot 会静静地删除我试图绘制的数据,即使我数据的 x 值在限制。下面的代码显示了来自单个图的示例,以及我尝试(未成功)在使用 scale_x_datetime().
的 limits 参数时使条形显示的一些事情
library(dplyr)
library(tibble)
library(ggplot2)
library(lubridate)
example_dat <- tibble(
author_name = 'fname lname',
time_window = as_datetime('2019-12-07 00:00:00'),
time_read = 32.4
)
lims <- c(as_datetime('2019-12-01 00:00:00'),
as_datetime('2019-12-09 00:00:00'))
# when I plot without limits, the bar appears as expected
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity') +
scale_x_datetime() +
ylim(0, 35)
# when I plot with limits, the bar is silently dropped
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity') +
scale_x_datetime(limits = lims) +
ylim(0, 35)
# specifying breaks alongside the limits does not help, the bar is still silently dropped
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity') +
scale_x_datetime(limits = lims, breaks = as_datetime(c('2019-12-01 00:00:00',
'2019-12-02 00:00:00',
'2019-12-03 00:00:00',
'2019-12-04 00:00:00',
'2019-12-05 00:00:00',
'2019-12-06 00:00:00',
'2019-12-07 00:00:00',
'2019-12-08 00:00:00',
'2019-12-09 00:00:00'))) +
theme(axis.text.x = element_text(angle = 90)) +
ylim(0, 35)
# using as.POSIXct() instead of as_datetime() to define the limits does not help, the bar is still silently dropped
p_lims <- as.POSIXct(c('2019-12-01 00:00:00', '2019-12-09 00:00:00'),
tz = "GMT")
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity') +
scale_x_datetime(limits = p_lims) +
ylim(0, 35)
没有错误消息,我怀疑这是我对 ggplot 中比例和日期交互方式的理解存在差距。如有任何见解,我们将不胜感激。
Session 上下文:
R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.5.2
attached base packages:
[1] stats graphics grDevices utils datasets
[6] methods base
other attached packages:
[1] lubridate_1.7.10 ggplot2_3.3.5 scales_1.1.1
[4] tibble_3.1.5 dplyr_1.0.7
谢谢
就在那里,只是非常狭窄!
我认为在这种情况下,ggplot 将您的示例解释为发生在 1 秒内,因为这是您数据的分辨率,但您的时间范围是 8 天 x 24 小时 x 60 米 x 60 秒 = 691,200 秒长。
要使栏“一天”变宽:
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity', width = 60*60*24) +
scale_x_datetime(limits = lims) +
ylim(0, 35)
主要目标:我正在尝试使用一组 x 和 y 限制和比例从不同的 tibbles 构建一系列条形图,以便于跨图进行比较。 x-axis 是日期时间,y-axis 是数字。我使用小标题集中最具包容性的 x 和 y 范围来定义所有绘图的限制和比例。
挑战:当我向 scale_x_datetime() 添加限制以设置 x-axis 范围时,ggplot 会静静地删除我试图绘制的数据,即使我数据的 x 值在限制。下面的代码显示了来自单个图的示例,以及我尝试(未成功)在使用 scale_x_datetime().
的 limits 参数时使条形显示的一些事情library(dplyr)
library(tibble)
library(ggplot2)
library(lubridate)
example_dat <- tibble(
author_name = 'fname lname',
time_window = as_datetime('2019-12-07 00:00:00'),
time_read = 32.4
)
lims <- c(as_datetime('2019-12-01 00:00:00'),
as_datetime('2019-12-09 00:00:00'))
# when I plot without limits, the bar appears as expected
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity') +
scale_x_datetime() +
ylim(0, 35)
# when I plot with limits, the bar is silently dropped
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity') +
scale_x_datetime(limits = lims) +
ylim(0, 35)
# specifying breaks alongside the limits does not help, the bar is still silently dropped
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity') +
scale_x_datetime(limits = lims, breaks = as_datetime(c('2019-12-01 00:00:00',
'2019-12-02 00:00:00',
'2019-12-03 00:00:00',
'2019-12-04 00:00:00',
'2019-12-05 00:00:00',
'2019-12-06 00:00:00',
'2019-12-07 00:00:00',
'2019-12-08 00:00:00',
'2019-12-09 00:00:00'))) +
theme(axis.text.x = element_text(angle = 90)) +
ylim(0, 35)
# using as.POSIXct() instead of as_datetime() to define the limits does not help, the bar is still silently dropped
p_lims <- as.POSIXct(c('2019-12-01 00:00:00', '2019-12-09 00:00:00'),
tz = "GMT")
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity') +
scale_x_datetime(limits = p_lims) +
ylim(0, 35)
没有错误消息,我怀疑这是我对 ggplot 中比例和日期交互方式的理解存在差距。如有任何见解,我们将不胜感激。
Session 上下文:
R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.5.2
attached base packages:
[1] stats graphics grDevices utils datasets
[6] methods base
other attached packages:
[1] lubridate_1.7.10 ggplot2_3.3.5 scales_1.1.1
[4] tibble_3.1.5 dplyr_1.0.7
谢谢
就在那里,只是非常狭窄!
我认为在这种情况下,ggplot 将您的示例解释为发生在 1 秒内,因为这是您数据的分辨率,但您的时间范围是 8 天 x 24 小时 x 60 米 x 60 秒 = 691,200 秒长。
要使栏“一天”变宽:
example_dat %>%
ggplot(aes(x = time_window, y = time_read)) +
geom_bar(stat = 'identity', width = 60*60*24) +
scale_x_datetime(limits = lims) +
ylim(0, 35)