极坐标图中的ggplot2 v2.21.9 sec.axis

时间:2017-03-11 06:33:49

标签: r ggplot2

我目前正在尝试使用sec.axis中最近引入的函数ggplot2添加辅助轴。此功能适用于散点图/条形图,但不适用于极坐标图:在下面的代码中,显示第二个y轴的名称,但不显示轴。

是否有任何解决方法或选项,我还没想到?

require(ggplot2)
set.seed(40);
Location <- data.frame(Winkel = round(runif(1000, 0, 24), 0))
Location$BAD <- Location$Winkel %in% c(seq(7, 18))
Abschnitte <- c(0:24)

polar <- data.frame(Winkel2 = c(1.5, 2.34, 1.2, 3.45, 1.67, 2.61, 1.11, 13.2), 
                    value = c(0.1, 0.03, 0.02, 0.015, 0.01, 0.04, 0.09, 0.06))

ggplot(Location, aes(x = Winkel, fill = BAD, y = (..count..)/sum(..count..))) + 
  geom_histogram(breaks = seq(0,24), colour = "black") + 
  coord_polar(start = 0) + theme_minimal() +
  scale_fill_brewer(type = "seq", palette = 3) +
  ylab("Percentual allocation time") + 
  ggtitle("") +
  scale_x_continuous("", limits = c(0, 24), breaks = Abschnitte, labels = Abschnitte) +
  scale_y_continuous(labels = scales::percent,
           sec.axis = sec_axis(~.*5, name = "mean direction")) +
  geom_segment(data = polar, aes(x = Winkel2, y = 0, xend = Winkel2, yend = value, fill = NA), 
               arrow = arrow(angle = 30, type = "closed", length = unit(0.3, "cm")))

enter image description here

1 个答案:

答案 0 :(得分:1)

正如@henrik在评论中提到的,这是一个错误。它是been patched,如果您使用GitHub的开发版本(即devtools::install_github("tidyverse/ggplot2")),则可以使用。

以下是补丁之后的示例:

require(ggplot2)
#> Loading required package: ggplot2
set.seed(40);
Location <- data.frame(Winkel = round(runif(1000, 0, 24), 0))
Location$BAD <- Location$Winkel %in% c(seq(7, 18))
Abschnitte <- c(0:24)

polar <- data.frame(Winkel2 = c(1.5, 2.34, 1.2, 3.45, 1.67, 2.61, 1.11, 13.2), 
                    value = c(0.1, 0.03, 0.02, 0.015, 0.01, 0.04, 0.09, 0.06))

ggplot(Location, aes(x = Winkel, fill = BAD, y = (..count..)/sum(..count..))) + 
  geom_histogram(breaks = seq(0,24), colour = "black") + 
  coord_polar(start = 0) + theme_minimal() +
  scale_fill_brewer(type = "seq", palette = 3) +
  ylab("Percentual allocation time") + 
  ggtitle("") +
  scale_x_continuous("", limits = c(0, 24), breaks = Abschnitte, labels = Abschnitte) +
  scale_y_continuous(labels = scales::percent,
           sec.axis = sec_axis(~.*5, name = "mean direction")) +
  geom_segment(data = polar, aes(x = Winkel2, y = 0, xend = Winkel2, yend = value, fill = NA), 
               arrow = arrow(angle = 30, type = "closed", length = unit(0.3, "cm")))
#> Warning: Ignoring unknown aesthetics: fill

相关问题