簡體   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