[英]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.我认为这是一个很常见的问题,但我不知道如何调整我的极坐标条形图,使条形图以零为中心,并且我手动绘制的轴的 rest 也正确绘制。
I made this function to plot the way I want.我按照我想要的方式制作了这个 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)))
}
It basically creates the scaffolding where things will go later.它基本上创建了脚手架,稍后将在其中进行 go。
polar_chart_theme(toy)
If I plot this with a line, I get the desired output如果我用一条线 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"))
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?我怎样才能猜测/调整 plot 以正确绘制? Maybe using the start
argument on the polar_chart_theme
?.也许在polar_chart_theme
上使用start
参数? My final output will probably have two facets but using position="stack"
was convenient to avoid scale problems due to position="dodge"
.我的最终 output 可能会有两个方面,但使用position="stack"
可以方便地避免由于position="dodge"
引起的缩放问题。
To make it more clear:为了更清楚:
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.看起来这些条位于刻度线的中心,但是,因为每个条都有一个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"))
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:用coord_polar
覆盖coord_cartesian
,我们可以看到条形引入的偏移量,因为条形居中:
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)我们可以通过将数据偏移一半条宽来解决这个问题(参见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()
Now that the bars are aligned with 0
, we can go back to polar coordinates:现在条形图与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.