简体   繁体   中英

Adjust the start of a polar barplot in ggplot2

I think this is a quite common problem but I can't figure out how to adjust my polar barplot so that the bars are centered at zero and the rest of my manually drawn axes are also drawn properly.

I made this function to plot the way I want.

polar_chart_theme <- function(data, start = 0){
  
  #max_rate <- data %>% pull(rate) %>% max
  ggplot(data=data)+
  coord_polar(start = start)+
  theme(panel.background = element_rect(fill="white"),
        axis.text.y = element_blank(),
         axis.ticks.y = element_blank(),
        panel.grid = element_blank())+
  annotate("rect", xmin=12, xmax=24, ymin=-Inf, ymax=Inf, fill="gray70")+
  # annotate+
  geom_hline(yintercept = seq(0, 30, by = 10), colour = "grey50", size = 0.2) +
  geom_vline(xintercept = seq(0, 23, by = 6), colour = "grey50", size = 0.2) +
  scale_x_continuous(breaks=seq(0, 23, 6), expand = c(0, 0))+
  # add the y labels
  geom_text(
    data = data.frame(x=2, y=seq(10, 30, 10)),
    aes(
    x = x, 
    y = y,
    label = as.character(y)))

}

It basically creates the scaffolding where things will go later.

polar_chart_theme(toy)

在此处输入图像描述

If I plot this with a line, I get the desired output

toy %>% 
  polar_chart_theme(.) +
  facet_wrap(~date)+
  stat_summary(geom = "polygon",
               fun = mean,
               aes(x=hour_of_day, y=rate,
                   group=Treatment, color=Treatment, fill=Treatment), alpha=0.5) +
  labs(title = "Chronogram",
       y="",
       x="") +
  scale_color_manual(values=c("orange", "black"))+
  scale_fill_manual(values=c("orange", "black"))

在此处输入图像描述

If I try to get a barplot, because the bars have some width, things get weird:

toy %>%
polar_chart_theme(.) +
  facet_wrap(~date)+
stat_summary(geom = "bar",
             position = "stack",
             fun = mean,
             aes(x=hour_of_day, y=rate,
                 group=Treatment, color=Treatment, fill=Treatment), alpha=0.5) +
  labs(title = "Chronogram",
       y="",
       x="") +
  scale_color_manual(values=c("orange", "black"))+
  scale_fill_manual(values=c("orange", "black"))

在此处输入图像描述

How can I guesstimate/adjust for the plot to be plotted properly? Maybe using the start argument on the polar_chart_theme ?. My final output will probably have two facets but using position="stack" was convenient to avoid scale problems due to position="dodge" .

To make it more clear:

  1. 0 and 24 are no longer at the same spot in the plot
  2. Neither 0 or 24 are aligned to 12
  3. 6 and 18 are no longer aligned

Looks like the bars are positioned on the center of the tick mark, but, because each bar has a width there is some adjustment/alignment issue.

在此处输入图像描述

Data sample

structure(list(Treatment = c("B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A"), ID = c(1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), rate = c(9, 13, 8, 6, 6, 
11, 3, 12, 0, 9, 2, 0, 0, 4, 0, 0, 5, 1, 0, 0, 7, 8, 12, 17, 
14, 22, 27, 12, 19, 13, 7, 0, 8, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 
0, 0, 24, 19, 35, 3, 4, 0, 0, 0, 18, 2, 14, 2, 12, 3, 0, 0, 0, 
2, 2, 0, 0, 0, 0, 6, 2, 11, 14, 23, 12, 13, 11, 16, 9, 11, 5, 
12, 5, 3, 0, 0, 0, 0, 3, 4, 0, 4, 7, 0, 7, 15, 17, 3, 20, 14, 
14, 17, 16, 17, 7, 6, 15, 8, 7, 4, 9, 6, 6, 0, 8, 0, 0, 5, 2, 
11, 14, 23, 16, 5, 13, 15, 8, 13, 12, 1, 3, 7, 0, 0, 2, 0, 3, 
6, 0, 5, 4, 6, 8, 17, 21), date = structure(c(18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 18606, 
18606, 18606, 18606, 18606, 18606, 18606), class = "Date"), zt = structure(c(1607529600, 
1607533200, 1607536800, 1607540400, 1607544000, 1607547600, 1607551200, 
1607554800, 1607558400, 1607562000, 1607565600, 1607569200, 1607572800, 
1607576400, 1607580000, 1607583600, 1607587200, 1607590800, 1607594400, 
1607598000, 1607601600, 1607605200, 1607608800, 1607612400, 1607529600, 
1607533200, 1607536800, 1607540400, 1607544000, 1607547600, 1607551200, 
1607554800, 1607558400, 1607562000, 1607565600, 1607569200, 1607572800, 
1607576400, 1607580000, 1607583600, 1607587200, 1607590800, 1607594400, 
1607598000, 1607601600, 1607605200, 1607608800, 1607612400, 1607529600, 
1607533200, 1607536800, 1607540400, 1607544000, 1607547600, 1607551200, 
1607554800, 1607558400, 1607562000, 1607565600, 1607569200, 1607572800, 
1607576400, 1607580000, 1607583600, 1607587200, 1607590800, 1607594400, 
1607598000, 1607601600, 1607605200, 1607608800, 1607612400, 1607529600, 
1607533200, 1607536800, 1607540400, 1607544000, 1607547600, 1607551200, 
1607554800, 1607558400, 1607562000, 1607565600, 1607569200, 1607572800, 
1607576400, 1607580000, 1607583600, 1607587200, 1607590800, 1607594400, 
1607598000, 1607601600, 1607605200, 1607608800, 1607612400, 1607529600, 
1607533200, 1607536800, 1607540400, 1607544000, 1607547600, 1607551200, 
1607554800, 1607558400, 1607562000, 1607565600, 1607569200, 1607572800, 
1607576400, 1607580000, 1607583600, 1607587200, 1607590800, 1607594400, 
1607598000, 1607601600, 1607605200, 1607608800, 1607612400, 1607529600, 
1607533200, 1607536800, 1607540400, 1607544000, 1607547600, 1607551200, 
1607554800, 1607558400, 1607562000, 1607565600, 1607569200, 1607572800, 
1607576400, 1607580000, 1607583600, 1607587200, 1607590800, 1607594400, 
1607598000, 1607601600, 1607605200, 1607608800, 1607612400), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), hour_of_day = c(16, 17, 18, 19, 20, 21, 22, 23, 0, 
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)), row.names = c(NA, 
-144L), groups = structure(list(Treatment = c("A", "B"), ID = c(1, 
1), .rows = structure(list(97:144, 1:96), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

I often find it easier to debug polar problems in non-polar coords. Overriding the coord_polar with a coord_cartesian , we can see an offset introduced by the bars because the bars are centered:

toy %>% polar_chart_theme(.) +
  ## ...
  coord_cartesian()

在此处输入图像描述

We can address this by offsetting the data by half of a bar width (see the + 0.5 on the x coordinate)

toy %>%
  polar_chart_theme(.) +
  facet_wrap( ~ date) +
  stat_summary(
    geom = "bar",
    position = "stack",
    fun = mean,
    aes(
      x = hour_of_day + 0.5,
      y = rate,
      group = Treatment,
      color = Treatment,
      fill = Treatment
    ),
    alpha = 0.5
  ) +
  labs(title = "Chronogram",
       y = "",
       x = "") +
  scale_color_manual(values = c("orange", "black")) +
  scale_fill_manual(values = c("orange", "black")) + 
  coord_cartesian()

在此处输入图像描述

Now that the bars are aligned with 0 , we can go back to polar coordinates:

toy %>%
  polar_chart_theme(.) +
  facet_wrap( ~ date) +
  stat_summary(
    geom = "bar",
    position = "stack",
    fun = mean,
    aes(
      x = hour_of_day + 0.5,
      y = rate,
      group = Treatment,
      color = Treatment,
      fill = Treatment
    ),
    alpha = 0.5
  ) +
  labs(title = "Chronogram",
       y = "",
       x = "") +
  scale_color_manual(values = c("orange", "black")) +
  scale_fill_manual(values = c("orange", "black"))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM