如何在 R 中制作具有对数刻度的密度散点图?

How can I make a density scatterplot with log scale in R?

我想在 R 中制作一个 log10 比例的密度散点图。我尝试使用 ggplotstat_density2d[= 来绘制它23=] 在 R 中。我使用了这段代码:

ggplot(data=vod_agb_df, aes(vod, agb)) + 
       stat_density2d(aes(fill = ..density..), geom = "tile", contour = FALSE, n = 100) + 
       scale_fill_distiller(palette = 'YlOrRd', direction = 1) +
       scale_x_continuous(breaks=seq(0, 1, 0.25), limits = c(0, 1)) + 
       scale_y_continuous(breaks=seq(0, 300, 50), limits = c(0, 300)) + 
       labs(x='L-VOD', y='AGB(Mg/ha)') +
       theme_bw()

但结果看起来很奇怪。 the density scatterplot with my code

This is the plot I want to plot
The original scatterplot

可以log10-变换密度;这是一个最小且可重现的例子

library(MASS)
library(tidyverse)

set.seed(2020)
mvrnorm(100, mu = c(0, 0), Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)) %>%
    as_tibble() %>%
    ggplot(aes(V1, V2)) + 
    stat_density2d(
        aes(fill = log10(..density..)), geom = "tile", contour = FALSE, n = 100) + 
    scale_fill_distiller(palette = 'YlOrRd', direction = 1) + 
    theme_bw()


更新

我不清楚你说的是什么意思""我想在点分布区域而不是整个区域绘制密度散点图。"" 如果你问如何增加渐变色条的高度,你可以这样做

set.seed(2020)
mvrnorm(100, mu = c(0, 0), Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)) %>%
    as_tibble() %>%
    ggplot(aes(V1, V2)) + 
    stat_density2d(
        aes(fill = log10(..density..)), geom = "tile", contour = FALSE, n = 100) + 
    scale_fill_distiller(palette = 'YlOrRd', direction = 1) + 
    theme_bw() + 
    guides(fill = guide_colorbar(barheight = unit(3.5, "in"), title.position = "right"))    

无论您显示什么图作为您的预期输出,您都可以使用以下代码

library(tidyverse)

# Bin size control + color palette
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length) ) +
  geom_bin2d(bins = 20) +
  scale_fill_distiller(palette = 'YlOrRd', direction = 1) +
  theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())