1

I have a set of measurements of the number of spores counted for every two hour period over the course of 24 hours, one in dark conditions as well as one in light conditions. Total of 12 sampling periods, and 24 measurements. The closest I've gotten to a "normal" 24 hour "clock" barplot is with this code, with Mean.Treatment being the number of spores counted in each time interval:

dc = read_excel('Spore Data for R.xlsm', "Test")
summary(dc)

ggplot(dc, aes(x = Time, y = Mean.Treatment, fill = Treatment)) +
  geom_col(position = "dodge") +
  coord_polar(start = 0)

This displays a pretty nice image, but not a clean and clear "day measurement". Ideally I can find out how to move the "0/24" to the top and have the bars to the right of the timeline rather than split in between to more easily show the collection period was between these two time

I have tried:

scale_x_continuous(limits = c(0,24),
breaks = 0:24)
dc$Time=as.numeric(levels(dc$Time))[dc$Time]

but errors are still present. Any idea how to make this more legible as a graph?

tdy
  • 36,675
  • 19
  • 86
  • 83
  • Please say more about what "a normal 24 hour clock barplot" means to you here. How would you describe it to someone like me who doesn't know what that means? – Jon Spring Aug 05 '22 at 18:59
  • 1
    It will be easier to help if you can share some example data in the form of code we can run to load. For instance, if you run `dput(dc[1:24,])` that will create a code recipe that will create a perfect copy (with data formats, etc) of the first 24 rows (2 days I'm guessing?) of the `dc` data frame. – Jon Spring Aug 05 '22 at 19:03

1 Answers1

0

It's always easier to help with some reproducible data, but it sounds as though Time is a numeric / integer column with even numbers from 0 to 22, Treatment is a factor or character variable with two levels (light and dark), and Mean.Treatment is a numeric measurement variable. We can replicate such a data set with the same names like this:

set.seed(1)

dc <- data.frame(Time = rep(2 * 0:11, each = 2),
                 Treatment = rep(c("Dark", "Light"), 12),
                 Mean.Treatment = runif(24, rep(c(0.5, 0.75), 12),
                                        rep(c(1.25, 1.5), 12)))

I admit I'm biased, but I prefer the way coord_curvedpolar from the geomtextpath package renders labels in a circular plot. Whether you use this or coord_polar, the trick with a dodged bar plot is to set the limits to c(-1, 23) and start at -pi/12. Just set the breaks and give them an appropriate format to improve the look:

library(geomtextpath)

ggplot(dc, aes(x = Time, y = Mean.Treatment, fill = Treatment)) +
  geom_col(position = "dodge", col = "gray") +
  coord_curvedpolar(start = -pi/12) +
  scale_x_continuous(limits = c(-1, 23),
                     breaks = 0:11 * 2,
                     labels = ~ sprintf("%02d:00", .x)) +
  theme_minimal(base_size = 16) +
  theme(axis.text.x = element_text(vjust = -0.2, size = 18)) +
  scale_fill_manual(values = c("#4354A0", "#FFF0A0"))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87