2016-08-04 164 views
2

我想绘制一个将极坐标直方图(罗盘方位测量值)与极坐标散点图(表示倾角和方位值)组合的绘图。例如,这是我想生产什么(source):将极坐标直方图与极坐标散点图结合起来

enter image description here

让我们忽视了柱状图无意义规模的绝对值;我们在图中显示直方图进行比较,而不是读取确切值(这是地质学中的传统图)。直方图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()) 

enter image description here

如果你仔细观察,你可以在圆圈的中心看到一个很小的直方图。

我怎样才能得到直方图看起来像顶部的情节,以便直方图自动缩放,以便最高的酒吧等于圆的半径(即90)?

+0

也许做ggplot外杆高度的计算,然后将其rescalling 0 -90会做诡计吗?听起来比重新调整倾角更好。 –

+2

您可以使用'geom_histogram(aes(bearing,y = ..count .. * 10)'来放大10倍。自动缩放有点困难,因为它需要访问直方图中的实际中断,但是像'max (dip)/ max(hist(bearing,n = 30,plot = FALSE)$ counts)'should do fine。 – Axeman

+2

此外,您可能想要调换'geom_point'和'geom_histogram'的顺序,以便前者出现顶部。 – Axeman

回答

2

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()) 

结果:

result

+0

Thannks,这非常有帮助。奇怪的是,我们无法将条形图直接移至剧情上的最后一个圆圈。我看到你把红线放在90度,所以我不确定接下来的线是什么。即使使用'expand = c(0,0)',外线仍然存在。 – Ben

+0

将'panel.grid = element_blank()'添加到'theme',但您需要用'geom_hline's和'geom_hline's重新创建网格,请参阅http://stackoverflow.com/questions/20808009/remove-额外的空间和环在极点的边缘 –

+1

啊,并且对于'table(useNA =“no”)'抱歉,我有一个包装它,所以它总是打印NAs并忘记在发布之前删除它。 –

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()) 

enter image description here

在这里,我来到了缩放的数量,启发式。 下一步是找出一个定义它的算法方法。 类似这样:取点的最大y值,并用直方图的最大y值除以 。