繁体   English   中英

在 ggplot2 中调整极坐标图的开始

[英]Adjust the start of a polar barplot in ggplot2

我认为这是一个很常见的问题,但我不知道如何调整我的极坐标条形图,使条形图以零为中心,并且我手动绘制的轴的 rest 也正确绘制。

我按照我想要的方式制作了这个 function 到 plot 。

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

}

它基本上创建了脚手架,稍后将在其中进行 go。

polar_chart_theme(toy)

在此处输入图像描述

如果我用一条线 plot 这个,我得到想要的 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"))

在此处输入图像描述

如果我尝试获取条形图,因为条形有一定宽度,事情会变得很奇怪:

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

在此处输入图像描述

我怎样才能猜测/调整 plot 以正确绘制? 也许在polar_chart_theme上使用start参数? 我的最终 output 可能会有两个方面,但使用position="stack"可以方便地避免由于position="dodge"引起的缩放问题。

为了更清楚:

  1. plot 中的 0 和 24 不再位于同一位置
  2. 0 或 24 都不与 12 对齐
  3. 6 和 18 不再对齐

看起来这些条位于刻度线的中心,但是,因为每个条都有一个width ,所以存在一些调整/对齐问题。

在此处输入图像描述

数据样本

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

我经常发现在非极坐标中调试极坐标问题更容易。 coord_polar覆盖coord_cartesian ,我们可以看到条形引入的偏移量,因为条形居中:

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

在此处输入图像描述

我们可以通过将数据偏移一半条宽来解决这个问题(参见x坐标上的+ 0.5

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

在此处输入图像描述

现在条形图与0对齐,我们可以 go 回到极坐标:

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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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