R:ggplot 在日期时间未显示 geom_segment
R: ggplot not showing geom_segment over datetime
我想在日期时间上绘制一个 geom_segment,就像一个简单的甘特图。我从数据框 (df) 中绘制了一个 geom_segment,然后在 ggplot 中绘制了一个 geom_segment。问题是,当 x 尺度限制与 geom_segment 重叠时,它就会消失。但只有 geom_segment 从数据框创建。另一个工作正常。这是代码:
output$TA_2 <- renderPlot({
df <- data.frame("Start" = c("2019-08-13"),
"End" = c("2019-08-15"),
"y" = c(1))
df$Start <- as.POSIXct(2019-01-01, origin = df$Start)
df$End <- as.POSIXct(2019-01-01, origin = df$End)
ggplot(df, aes(x = Start, y = y)) +
geom_segment(aes(xend = End, yend = y), size=4) +
theme_bw() +
scale_y_continuous(limits=c(0,3)) +
scale_x_datetime(date_breaks = "24 hour", labels = date_format("%d.%m - %H:%M"),
minor_breaks = "24 hour", expand=c(0,0),
limits = c(
as.POSIXct(2019-01-01, origin = "2019-08-14"),
as.POSIXct(2019-01-01, origin = "2019-08-16")
)) +
geom_segment(x = as.POSIXct(2019-01-01, origin = "2019-08-13"),
xend = as.POSIXct(2019-01-01, origin = "2019-08-15"),
y = 2, yend = 2, size=1)
})
如果x-scale限制
limits = c(
as.POSIXct(2019-01-01, origin = "2019-08-14"),
...
调整为例如
limits = c(
as.POSIXct(2019-01-01, origin = "2019-08-10"),
...
两个片段都出现了。
我必须做什么,从数据框创建的段也可以重叠工作?
您可以使用 coord_cartesian
,而不是在 scale_x_datetime
中设置限制来切断您的 df
,它只会放大并保留片段。
ggplot(df, aes(x = Start, y = y)) +
geom_segment(aes(xend = End, yend = y), size=4) +
theme_bw() +
scale_y_continuous(limits=c(0,3)) +
scale_x_datetime(date_breaks = "24 hour", labels = date_format("%d.%m - %H:%M"),
minor_breaks = "24 hour", expand=c(0,0)) +
geom_segment(x = as.POSIXct(2019-01-01, origin = "2019-08-13"),
xend = as.POSIXct(2019-01-01, origin = "2019-08-15"),
y = 2, yend = 2, size=1) +
coord_cartesian(xlim = c(
as.POSIXct(2019-01-01, origin = "2019-08-14"),
as.POSIXct(2019-01-01, origin = "2019-08-16")
))
我想在日期时间上绘制一个 geom_segment,就像一个简单的甘特图。我从数据框 (df) 中绘制了一个 geom_segment,然后在 ggplot 中绘制了一个 geom_segment。问题是,当 x 尺度限制与 geom_segment 重叠时,它就会消失。但只有 geom_segment 从数据框创建。另一个工作正常。这是代码:
output$TA_2 <- renderPlot({
df <- data.frame("Start" = c("2019-08-13"),
"End" = c("2019-08-15"),
"y" = c(1))
df$Start <- as.POSIXct(2019-01-01, origin = df$Start)
df$End <- as.POSIXct(2019-01-01, origin = df$End)
ggplot(df, aes(x = Start, y = y)) +
geom_segment(aes(xend = End, yend = y), size=4) +
theme_bw() +
scale_y_continuous(limits=c(0,3)) +
scale_x_datetime(date_breaks = "24 hour", labels = date_format("%d.%m - %H:%M"),
minor_breaks = "24 hour", expand=c(0,0),
limits = c(
as.POSIXct(2019-01-01, origin = "2019-08-14"),
as.POSIXct(2019-01-01, origin = "2019-08-16")
)) +
geom_segment(x = as.POSIXct(2019-01-01, origin = "2019-08-13"),
xend = as.POSIXct(2019-01-01, origin = "2019-08-15"),
y = 2, yend = 2, size=1)
})
如果x-scale限制
limits = c(
as.POSIXct(2019-01-01, origin = "2019-08-14"),
...
调整为例如
limits = c(
as.POSIXct(2019-01-01, origin = "2019-08-10"),
...
两个片段都出现了。
我必须做什么,从数据框创建的段也可以重叠工作?
您可以使用 coord_cartesian
,而不是在 scale_x_datetime
中设置限制来切断您的 df
,它只会放大并保留片段。
ggplot(df, aes(x = Start, y = y)) +
geom_segment(aes(xend = End, yend = y), size=4) +
theme_bw() +
scale_y_continuous(limits=c(0,3)) +
scale_x_datetime(date_breaks = "24 hour", labels = date_format("%d.%m - %H:%M"),
minor_breaks = "24 hour", expand=c(0,0)) +
geom_segment(x = as.POSIXct(2019-01-01, origin = "2019-08-13"),
xend = as.POSIXct(2019-01-01, origin = "2019-08-15"),
y = 2, yend = 2, size=1) +
coord_cartesian(xlim = c(
as.POSIXct(2019-01-01, origin = "2019-08-14"),
as.POSIXct(2019-01-01, origin = "2019-08-16")
))