简体   繁体   English

有没有办法根据 x、y、z 值制作强度的 2D 圆形密度 plot; 看起来像 stat_density2d plot?

[英]Is there a way to make a 2D circular density plot of intensity from x, y, z values; that looks like the stat_density2d plot?

I'm trying to make a density plot of intensity in a circle, from radial intensity measuements of a bacterial population.我正在尝试根据细菌种群的径向强度测量值,在一个圆圈内制作密度 plot 强度。

I've looked into various 3D, 2D options to best represent the data.我研究了各种 3D,2D 选项以最好地代表数据。

I effectively want to create something like this:我实际上想创建这样的东西:

R code: R 代码:

df <- tibble(x_variable = rnorm(5000), y_variable = rnorm(5000)) df <- tibble(x_variable = rnorm(5000), y_variable = rnorm(5000))

ggplot(df, aes(x = x_variable, y = y_variable)) + stat_density2d(aes(fill =..density..), contour = F, geom = 'tile') ggplot(df, aes(x = x_variable, y = y_variable)) + stat_density2d(aes(fill =..density..), contour = F, geom = 'tile')

However, what i want to create , from this is a rough extract , where x and y are radial distances from the center point with a density/contours/shading being representing intensity z.但是,我想从中创建一个粗略的提取物,其中 x 和 y 是距中心点的径向距离,密度/轮廓/阴影表示强度 z。

I'm ok with it being a contour plot, or a 3D plot viewed above.我可以接受上面的轮廓 plot 或 3D plot。 Just feel like i've tried everything, with other examples i'll have surfaces which might overlap and 'submerge' and reappear at different distances.感觉就像我已经尝试了一切,在其他示例中,我将拥有可能重叠和“淹没”并在不同距离处重新出现的表面。 So any advice on how to tackle that would be great.所以任何关于如何解决这个问题的建议都会很棒。

Any help would be greatly appreciated.任何帮助将不胜感激。

It sounds as though you are trying to represent x, y, z data as a contour or 2D density plot.听起来好像您正试图将 x、y、z 数据表示为轮廓或 2D 密度 plot。 In order to do this, your data have to be organized into a regular grid.为了做到这一点,你的数据必须组织成一个规则的网格。

My understanding is that the x value in your data represents the radial distance from the center of the bacterial colony and the z value represents the density at that point.我的理解是,数据中的 x 值代表距细菌菌落中心的径向距离,z 值代表该点的密度。 This gives us enough information to create a 3D surface, providing that we can assume the colony is perfectly radially symmetrical.这为我们提供了足够的信息来创建 3D 表面,前提是我们可以假设菌落是完全径向对称的。

We start by loading in the data, and arrange it by increasing x value:我们首先加载数据,并通过增加 x 值来排列它:

df <- read.csv('my_data.csv')
df <- df[order(df$x),]

Now we create a sequence of radial distances from the centre which encompass our data set and will be used as the x and y co-ordinates of our grid.现在我们从中心创建一系列径向距离,其中包含我们的数据集,并将用作我们网格的 x 和 y 坐标。 We need enough points to make the grid smooth, so we will use 300 points along each side of our grid:我们需要足够的点来使网格平滑,所以我们将在网格的每一侧使用 300 个点:

radii <- seq(min(df$x), max(df$x), length = 300)

Now we can create a grid of all x, y combinations of these points using expand.grid , and then find the Euclidean distance from each point to the center:现在我们可以使用expand.grid创建这些点的所有 x、y 组合的网格,然后找到每个点到中心的欧几里得距离:

plot_df        <- expand.grid(x = radii, y = radii)
plot_df$radius <- sqrt(plot_df$x^2 + plot_df$y^2)

To get the z value at each grid point, we can use findInterval , which will identify the row of our original data frame which is closest to the radius of each grid point.要获取每个网格点的 z 值,我们可以使用findInterval ,它将识别原始数据帧中最接近每个网格点半径的行。 The z value of that row will be the z value of the grid point:该行的 z 值将是网格点的 z 值:

plot_df$z <- df$z[findInterval(plot_df$radius, df$x)]

Now we can plot the result using geom_raster :现在我们可以使用geom_raster plot 结果:

library(ggplot2)

p <- ggplot(plot_df, aes(x, y, fill = z)) + 
  geom_raster() +
  coord_equal() +
  scale_fill_viridis_c()

p

在此处输入图像描述

If you want to add labelled contour lines you could do:如果要添加标记的轮廓线,可以执行以下操作:

library(geomtextpath)

p + geom_textcontour(aes(z = z, label = ..level..), breaks = 5:9)

在此处输入图像描述


Update for three data sets更新三个数据集

This shows which colony has the highest density at which point:这显示了哪个菌落在哪个点具有最高密度:

plot_list <- lapply(paste0("my_data_", letters[1:3], ".csv"), 
  function(x) {
    df <- read.csv(x)
    df <- df[order(df$x),]
    radii <- seq(-2, 2, length = 300)
    plot_df <- expand.grid(x = radii, y = radii)
    plot_df$radius <- sqrt(plot_df$x^2 + plot_df$y^2)
    plot_df$z <- df$z[findInterval(plot_df$radius, df$x)]
    plot_df
  })

plot_list[[1]]$fill <- c("Col1", "Col2", "Col3")[apply(cbind(plot_list[[1]]$z, 
                          plot_list[[2]]$z, plot_list[[3]]$z), 1, which.max)]

ggplot(plot_list[[1]], aes(x, y, fill = fill)) +
  geom_raster() +
  coord_equal()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM