极坐标图的中心 bin 位于中点,而不会丢失 ggplot2 中的 bin

问题描述 投票:0回答:1

我正在尝试根据每个基本方向的频率创建一个图。我遵循了这个优秀资源的说明,但是当我尝试将垃圾箱置于中间时,我遇到了一个主要问题(例如,在“北”垃圾箱内从 NNW 到 N 到 NNE 的观察)。在最初的示例中,没有发现极端值,因此它没有显示为问题。

enter image description here

enter image description here

library(gcookbook)  # Load gcookbook for the wind data set
wind
w287<-c(0, 3.5, 9, 10, 90, 5, 358)
w288<-c(0, 3.5, 9, 10, 90, 5, 357)
w289<-c(0, 3.5, 9, 10, 90, 5, 1)
w290<-c(0, 3.5, 9, 10, 90, 5, 2)

wind2<-rbind(wind, w287, w288, w289, w290,w287, w288, w289, w290,w287, w288, w289, w290,w287, w288, w289, w290,w287, w288, w289, w290,w287, w288, w289, w290)
wind2[is.na(wind2$SpeedCat),]$SpeedCat<-">20"

#initial plot without rotation 
a<-ggplot(wind2, aes(x = DirCat)) +
  geom_histogram(binwidth = 90, boundary = 0) +
  coord_polar() +
  labs(title="Bins visible : no rotation")+
  scale_x_continuous(limits = c(0,360))

#plot with rotation in which the values from bin#1 are lost
b<-ggplot(wind2, aes(x = DirCat)) +
  geom_histogram(binwidth = 90, boundary = -45) +
  coord_polar() +
  labs(title="Bin not visible: rotation")+
  scale_x_continuous(limits = c(0,360))

c<-ggplot(wind2, aes(x = DirCat)) +
  geom_histogram(binwidth = 90, boundary = -45) +
  coord_polar() +
  labs(title="Bin visible: rotation + scale increased")+
  scale_x_continuous(limits = c(-45,405))

grid.arrange(a, b, c, ncol=3)
r ggplot2 polar-coordinates
1个回答
0
投票

center
geom_histogram
的论证可能会解决问题:

## example data:
d <- data.frame(DirCat = sample(0:360, 3600, replace = TRUE))

th <-  22.5 ## wedge angle

    d |> 
      ggplot() +
      geom_histogram(aes(x = DirCat),
                     colour = 'white',
                     binwidth = th,
                     center = th/2
      ) +
      scale_x_continuous(breaks = 1:16 * th) +
      coord_polar()

arbitrarily center histogram bar

© www.soinside.com 2019 - 2024. All rights reserved.