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

帅比萌擦擦* 提交于 2021-02-08 09:00:55

问题


I am still new to ggplot2. I want to plot my data from my insect poo experiment: x-value is time of day (24h) and y value is amount of poo (frass) produced as a percentage and represented as a circular plot. I added geom_ribbon to represent standard deviation which I calculated with circular. Firstly, I had issues with the 'clock' starting as 1/24 instead of 0/24:

ggplot2 clock starting from 1/24 instead of 0/24:

So I added the code expand_limits(x = 0, y = 0) which helped with fixing 1/24 to 0/24 but now there is a gap between 0/24 and 1:

ggplot2 clock starting from 0/24 but with blank space:

Can someone help me connect the data/remove the blank space between those hours.

Here is my code that I used:

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)

data

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")

Thank you in advance!


回答1:


I guess this kind of makes sense because your data "stops" at measurement 24, and there is nothing to tell R that we are dealing with a recurrent, periodical variable. So why would the lines be connected? Anyways, I guess the easiest "trick" would be to simply create an additional data point, at 0. I guess you can use the data from measurement 24 or 1.

Note I've slightly "optimised" your code - in particular:

  • reduced the axis titles to a single call to labs (use NULL, not "")
  • removed the expand argument from the scale_x (+ tiny change in the seq call)
  • merged the geom_vline calls to a singe call

I think that was it. It was a very nice first question.

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")

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

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") 

Created on 2021-01-07 by the reprex package (v0.3.0)



来源:https://stackoverflow.com/questions/65589126/gap-in-polar-time-plot-how-to-connect-start-and-end-points-in-geom-line-or-rem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!