在R中使用ggplot2绘制复合2D密度和气泡图。

huangapple go评论55阅读模式
英文:

Compound 2d density + bubble plot in R w/ ggplot 2

问题

我制作了这个图来可视化物理海洋学变量(SST和HCI)与捕鱼总数(降落量)之间的关系。SST是海表温度,HCI是栖息地压缩指数。气泡的坐标是HCI ~ SST,气泡的大小按降落量缩放。我的参考代码在底部。

你可以看到渔业有“繁荣衰退”的时期,即有许多小到中等大小的气泡和少数很大的气泡。这些较大的气泡通常出现在HCI较低和SST较高的时候。HCI和SST都对预测降落量具有重要影响。

我喜欢这个图,但它很“嘈杂”,气泡重叠太多。有没有办法让它们强制排列成均匀的网格?我尝试过使用geom_bin2d,但这破坏了背景密度图。

这是一个参考图,显示了我想要做的事情。显然,我的图基本上是不同的,但这看起来像我想要制作的图。

这是我的代码:

# HCI ~ SST 密度图,作为背景
background_plot <- ggplot(path_data, aes(x = SST, y = HCI)) +
  stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
  scale_fill_continuous(type = "viridis") +
  theme_gray() +
  ylim(-0.1, 1)

# 添加气泡,按降落量缩放
bubble_chart <- background_plot +
  geom_point(data = path_data, aes(x = SST, y = HCI, size = SUM_RETAINED_NUM, color = SUM_RETAINED_NUM), 
             alpha = 0.6) +
  scale_size_continuous(range = c(1, 10), guide = "legend") +
  scale_color_cmocean(name = "ice", direction = 1, guide = "legend") +
  theme_gray()

# 组合图
bubble_chart

如果你有任何关于代码或图形的问题,请随时提出。

<details>
<summary>英文:</summary>

TLDR; Can I force the bubble coordinates onto a uniform grid?

I&#39;ve made this figure to visualize the relationship between physical oceanographic variables (SST and HCI) and the total number of fish caught (landings). SST is sea surface temperature, and HCI is habitat compression index. The coordinates of the bubbles are HCI ~ SST, and bubble size is scaled by landings. My reference code is down the bottom.

[![SST is sea surface temperature, and HCI is habitat compression index. The coordinates of the bubbles are HCI ~ SST, and bubble size is scaled by landings.][1]][1]


You can see that the fishery has &quot;boom-bust&quot; periods, i.e. there are many small-medium bubbles and a few very large ones. These larger bubbles tend to be when HCI is low and SST is high. Both HCI and SST are significant in predicting landings.

I like the figure but it&#39;s very &quot;noisy&quot;, with the bubbles overlapping so much. Is there a way that I can force them onto a uniform grid? I&#39;ve tried geom_bin2d, but that broke the background density plot. 

Here&#39;s a reference figure that show&#39;s what I&#39;m trying to do. Obviously my figure is fundamentally different, but this looks like the figure that I&#39;m trying to make.

[![From Phillips et al. 2014, Spatio-temporal associations of albacore CPUEs in the Northeastern Pacific with regional SST and climate environmental variables][2]][2]

Here&#39;s my code:

    # HCI ~ SST density plot, as the background
    background_plot &lt;- ggplot(path_data, aes(x = SST, y = HCI)) +
      stat_density_2d(aes(fill = ..density..), geom = &quot;raster&quot;, contour = FALSE) +
      scale_fill_continuous(type = &quot;viridis&quot;) +
      theme_gray() +
      ylim(-0.1, 1)
    
    # Add bubbles, scaled by # landings
    bubble_chart &lt;- background_plot +
      geom_point(data = path_data, aes(x = SST, y = HCI, size = SUM_RETAINED_NUM, color = SUM_RETAINED_NUM), 
                 alpha = 0.6) +
      scale_size_continuous(range = c(1, 10), guide = &quot;legend&quot;) +
      scale_color_cmocean(name = &quot;ice&quot;, direction = 1, guide = &quot;legend&quot;) +
      theme_gray()
    
    # Combined plot
    bubble_chart


  [1]: https://i.stack.imgur.com/VTDnV.png
  [2]: https://i.stack.imgur.com/j1N0Y.png

</details>


# 答案1
**得分**: 2

geom_point(data = dplyr::count(path_data, 
                               SST = round(SST*10)/10, 
                               HCI = round(HCI*20)/20, 
                               wt = SUM_RETAINED_NUM, 
                               name = "SUM_RETAINED_NUM"), 
           aes(x = SST, y = HCI, size = SUM_RETAINED_NUM, color = SUM_RETAINED_NUM), 
           alpha = 0.6) +

should make the points be summed by buckets, where here I've rounded SST to the closest 1/10th unit, and HCI to the closest 1/20th unit. Adjust to taste.

BTW, 'shape = 21' can get you something more like your example if you want hollow points.

<details>
<summary>英文:</summary>

    geom_point(data = dplyr::count(path_data, 
                                   SST = round(SST*10)/10, 
                                   HCI = round(HCI*20)/20, 
                                   wt = SUM_RETAINED_NUM, 
                                   name = &quot;SUM_RETAINED_NUM&quot;), 
               aes(x = SST, y = HCI, size = SUM_RETAINED_NUM, color = SUM_RETAINED_NUM), 
               alpha = 0.6) +

should make the points be summed by buckets, where here I&#39;ve rounded SST to the closest 1/10th unit, and HCI to the closest 1/20th unit. Adjust to taste.

BTW, `shape = 21` can get you something more like your example if you want hollow points.

</details>



# 答案2
**得分**: 1

以下是翻译好的部分:

- 你可以尝试使用 `+ scale_shape(solid = FALSE)`,以在气泡图中使用空心圆。这将使你的气泡图类似于参考图。

- 我认为你需要使用 `cut()` 这样的切分函数来创建一个新的 HCI 变量,并对其进行分组。`cut()` 函数将为你提供一个因子变量,但你可以将级别映射到数值变量(例如,中点)以创建一个网格。

- 假设你将 HCI 分成 5 个组。从生成的因子变量创建一个新的数值变量。你可以在数据框或 ggplot2 中为 HCI 值添加抖动。

- 但最终,你将使用相同的气泡图语句,只不过在 `y = HCI` 的地方,你会有类似 `aes(x = SST, y = HCI.binned.jittered` 的东西。

- 你可能还需要微调坐标轴,具体需要怎么调整不太确定。

<details>
<summary>英文:</summary>

Here are a couple of suggestions.  

You could try `+ scale_shape(solid = FALSE)` to give open circles in your bubble plot.  That would make your bubble plot resemble the reference figure.

I think that you&#39;d have to make a new HCI variable with bins and use a binning function like `cut()`.  The function `cut()` will give you a factor variable but you can map the levels to a numeric variable (e.g., a midpoint) to make a grid.  

Let&#39;s say you split HCI into 5 bins.  Make a new numeric variable from the resulting factor variable.  You can add jitter to the HCI values in the data.frame or in ggplot2.  

But ultimately you would use the same bubble chart statement except you&#39;d have something like `aes(x = SST, y = HCI.binned.jittered` instead of just `y = HCI`.

You may also have to tweak the axes, not sure.

</details>



huangapple
  • 本文由 发表于 2023年6月5日 08:19:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定