极地时间图中的间隙 - 如何连接 geom_line 中的起点和终点或删除空白 space

Gap in polar time plot - How to connect start and end points in geom_line or remove blank space

我还是 ggplot2 的新手。我想绘制我的昆虫便便实验数据:x 值是一天中的时间 (24h),y 值是以百分比表示的便便(粪便)量,并表示为圆形图。我添加了 geom_ribbon 来表示我用圆形计算的标准偏差。首先,我对以 1/24 而不是 0/24 开头的 'clock' 有疑问:

ggplot2 时钟从 1/24 而不是 0/24 开始:

所以我添加了代码 expand_limits(x = 0, y = 0) 这有助于将 1/24 固定为 0/24 但现在 0/24 和 1 之间存在差距:

ggplot2 时钟从 0/24 开始但空白 space:

谁能帮我连接那些时间之间的 data/remove 空白 space。

这是我使用的代码:

b <- ggplot(dat, aes(x = TIME, y = Percentage)) +
    geom_ribbon(aes(ymin = Percentage - 1.19651, ymax = Percentage + 1.19651), fill = "grey70", alpha = 0.2) + 
    geom_line() + 
    theme_minimal() + 
    coord_polar(start = 0) + 
    scale_y_continuous(breaks = seq(0, 10, by = 2)) + 
    scale_x_continuous(breaks = seq(0, 24, by = 1), expand = c(0, 0)) + 
    ylab("Frass production %") + 
    xlab("") + 
    geom_vline(xintercept = 6.30, color = "red", linetype = "dashed") +
    geom_vline(xintercept = 20.3, color = "red", linetype = "dashed")
 
b + expand_limits(x = 0, y = 0)

数据

structure(list(Number = 1:24, TIME = 1:24, Average = c(0.08, 0.08, 0.05, 0.03, 0.02, 0.03, 0.03, 0.02, 0.02, 0.02, 0.01, 0.01, 0, 0.01, 0.01, 0.01, 0.02, 0.05, 0.06, 0.06, 0.09, 0.1, 0.09, 0.08), Percentage = c(8, 8, 5, 3, 2, 3, 3, 2, 2, 2, 1, 1, 0, 1, 1, 1, 2, 5, 6, 6, 9, 10, 9, 8), Light = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("DARK", "LIGHT"), class = "factor")), row.names = c(NA, -24L), class = "data.frame")

提前致谢!

我猜这是有道理的,因为您的数据“停止”在测量值 24 处,并且没有任何信息可以告诉 R 我们正在处理一个周期性的周期性变量。那为什么会连线呢?

无论如何,我认为最简单的“技巧”是在 0 处简单地创建一个额外的数据点。为了顺利连接,您应该使用测量 24 的数据。

library(ggplot2)

dat <- structure(list(Number = 1:24, TIME = 1:24, Average = c(0.08, 0.08, 0.05, 0.03, 0.02, 0.03, 0.03, 0.02, 0.02, 0.02, 0.01, 0.01, 0, 0.01, 0.01, 0.01, 0.02, 0.05, 0.06, 0.06, 0.09, 0.1, 0.09, 0.08), Percentage = c(8, 8, 5, 3, 2, 3, 3, 2, 2, 2, 1, 1, 0, 1, 1, 1, 2, 5, 6, 6, 9, 10, 9, 8), Light = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("DARK", "LIGHT"), class = "factor")), row.names = c(NA, -24L), class = "data.frame")

fakedat<- dat[dat$TIME == 24, ]
fakedat$TIME <- 0
plotdat <- rbind(dat, fakedat)

ggplot(plotdat, aes(x = TIME, y = Percentage)) +
  geom_ribbon(aes(ymin = Percentage - 1.19651, ymax = Percentage + 1.19651), fill = "grey70", alpha = 0.2) + 
  geom_line() + 
  theme_minimal() + 
  coord_polar(start = 0) + 
  scale_x_continuous(breaks = seq(0, 24)) + 
  scale_y_continuous(breaks = seq(0, 10, by = 2)) +
  labs(x = NULL, y = "Frass production %") + 
  geom_vline(xintercept = c(6.30, 20.3), color = "red", linetype = "dashed")

reprex package (v2.0.0)

于 2021-04-21 创建

P.S.

一些改进代码的建议

  • 将轴标题减少到对 labs 的一次调用(使用 NULL,而不是“”)
  • scale_x 中删除扩展参数(+ seq 调用中的微小变化)
  • geom_vline 个调用合并为单个调用

我想就是这样。这是一个非常好的第一个问题。