【问题标题】:Combine polar histogram with polar scatterplot将极坐标直方图与极坐标散点图相结合
【发布时间】:2016-08-04 08:00:19
【问题描述】:

我想绘制一个将极坐标直方图(罗盘方位测量值)与极坐标散点图(指示倾角和方位值)相结合的图。例如,这是我想要制作的(source):

让我们忽略直方图的比例尺的绝对值是没有意义的;我们显示直方图以在图中进行比较,而不是读取精确值(这是地质学中的常规图)。直方图 y 轴文本通常不会显示在这些图中。

这些点显示了它们的方位(与垂直的角度)和倾角(与中心的距离)。倾角始终在 0 到 90 度之间,方位角始终在 0 到 360 度之间。

我可以得到一些方法,但我被直方图的比例(在下面的示例中,0-20)和散点图的比例(总是 0-90,因为它是一个下降)之间的不匹配所困扰测量)。

这是我的例子:

n <-  100
bearing <- runif(min = 0, max = 360, n = n) 
dip <- runif(min = 0, max = 90, n = n)

library(ggplot2)
ggplot() +
  geom_point(aes(bearing, 
                 dip),
             alpha = 0.4) +
  geom_histogram(aes(bearing),
                 colour = "black", 
                 fill = "grey80") +
  coord_polar() +
  theme(axis.text.x = element_text(size = 18)) +
  coord_polar(start = 90 * pi/180) +
  scale_x_continuous(limits = c(0, 360), 
                     breaks = (c(0, 90, 180, 270))) +
  theme_minimal(base_size = 14) +
  xlab("") +
  ylab("") +
  theme(axis.text.y=element_blank())

如果您仔细观察,您会在圆的中心看到一个微小的直方图。

如何使直方图看起来像顶部的图,以便自动缩放直方图,使最高条等于圆的半径(即 90)?

【问题讨论】:

  • 也许在 ggplot 之外进行条形高度计算,然后将其重新调用为 0-90 可以解决问题吗?听起来比重新调整倾角要好。
  • 您可以使用geom_histogram(aes(bearing, y = ..count.. * 10) 放大10倍。自动缩放有点困难,因为它需要访问直方图中的实际中断,但是像 max(dip) / max(hist(bearing, n = 30, plot = FALSE)$counts) 这样的东西应该可以。
  • 另外,您可能希望交换geom_pointgeom_histogram 的顺序,这样前者会出现在顶部。

标签: r plot ggplot2 histogram


【解决方案1】:

to_barplot 可能可以用更简单的方式制作,但这里是:

library(Hmisc)
library(dplyr)

set.seed(2016)
n <-  100
bearing <- runif(min = 0, max = 360, n = n) 
dip <- runif(min = 0, max = 90, n = n)

rescale_prop <- function(x, a, b, min_x = min(x), max_x = max(x)) {
  (b-a)*(x-min_x)/(max_x-min_x) + a
}

to_barplot <- bearing %>%
  cut2(cuts = seq(0, 360, 20)) %>%
  table(useNA = "no") %>%
  as.integer() %>%
  rescale_prop(0, 90, min_x = 0) %>%  # min_x = 0 to keep min value > 0 (if higher than 0 of course)
  data.frame(x = seq(10, 350, 20),
             y = .)

library(ggplot2)
ggplot() +
  geom_bar(data = to_barplot,
           aes(x = x, y = y),
           colour = "black", 
           fill = "grey80",
           stat = "identity") +
  geom_point(aes(bearing, 
                 dip),
             alpha = 0.4) +
  geom_hline(aes(yintercept = 90), colour = "red") + 
  coord_polar() +
  theme(axis.text.x = element_text(size = 18)) +
  coord_polar(start = 90 * pi/180) +
  scale_x_continuous(limits = c(0, 360), 
                     breaks = (c(0, 90, 180, 270))) +
  theme_minimal(base_size = 14) +
  xlab("") +
  ylab("") +
  theme(axis.text.y=element_blank())

结果:

【讨论】:

  • 谢谢,这很有帮助。奇怪的是,我们无法将条形图正确定位到情节的最后一个圆圈。我看到你把红线放在 90,所以我不确定下一条线是做什么用的。即使有expand = c(0,0),外线仍然存在。
  • panel.grid = element_blank() 添加到theme,但您需要使用geom_hlines 和geom_hlines 重新创建网格,请参阅stackoverflow.com/questions/20808009/…
  • 啊,对table(useNA = "no") 感到抱歉,我有一个包装器,所以它总是打印 NA 并忘记在发布前删除它。
【解决方案2】:

这不是最终解决方案,但我认为它朝着正确的方向发展。 这里的问题是直方图的比例与点的比例完全不同。按比例,我打算取最大值。

如果你重新缩放点,你可以得到这个:

scaling <- dip / 9
ggplot()  +
    geom_point(aes(bearing, 
                   scaling),
               alpha = 0.4) +
    geom_histogram(aes(bearing),
                   colour = "black", 
                   fill = "grey80") +
    coord_polar() +
    theme(axis.text.x = element_text(size = 18)) +
    coord_polar(start = 90 * pi/180) +
    scale_x_continuous(limits = c(0, 360), 
                       breaks = (c(0, 90, 180, 270))) +
    theme_minimal(base_size = 14) +
    xlab("") +
    ylab("") +
    theme(axis.text.y=element_blank())

在这里,我试探性地得出了缩放的数字。 下一步是找出定义它的算法方法。 类似的东西:取点的最大值并将其除以 直方图的最大值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多