深度剖析可视化

2024-01-01

我正在尝试使用变量深度、距离和温度创建深度剖面图。收集的数据来自 9 个不同的点,这些点之间的距离已知(距离 5m,9 个站点,9 组不同的数据)。温度读数是根据这 9 个站直接落下的探空仪,每 2 秒读取一次温度读数。 9 个站点的最大深度也是从船上测得的。

所以我拥有的数据是:

  1. 9 个站点中每个站点的深度(y 轴)
  2. 9 个站点中每个站点的温度读数,垂直方向间隔约 0.2m,直至到达底部(填充区域)
  3. 站点之间的距离(x 轴)

是否可以创建与此类似的深度剖面? (显然该图中没有更高的分辨率)

我已经尝试过使用 ggplot2 和 raster 但我似乎不知道如何做到这一点。 我遇到的问题之一是如何使 ggplot2 区分站点 1 的 5m 深度温度读数和站点 5 的 5m 温度读数,因为它们具有相同的深度值。

即使您可以引导我使用另一个程序来创建这样的图表,那就太好了


[ 修订 ]
(如果您知道更合适的插值方法,尤其是不需要切割底部数据的插值方法,请评论我。)

ggplot()需要长数据形式。

library(ggplot2)

# example data
max.depths <- c(1.1, 4, 4.7, 7.7, 8.2, 7.8, 10.7, 12.1, 14.3)
depth.list <- sapply(max.depths, function(x) seq(0, x, 0.2))
temp.list <- list()
set.seed(1); for(i in 1:9) temp.list[[i]] <- sapply(depth.list[[i]], function(x) rnorm(1, 20 - x*0.5, 0.2))
set.seed(1); dist <- c(0, sapply(seq(5, 40, 5), function(x) rnorm(1, x, 1)))
dist.list <- sapply(1:9, function(x) rep(dist[x], length(depth.list[[x]])))

main.df <- data.frame(dist = unlist(dist.list), depth = unlist(depth.list) * -1, temp = unlist(temp.list))

# a raw graph
ggplot(main.df, aes(x = dist, y = depth, z = temp)) + 
  geom_point(aes(colour = temp), size = 1) +
  scale_colour_gradientn(colours = topo.colors(10))

# a relatively raw graph   (don't run with this example data)
ggplot(main.df, aes(x = dist, y = depth, z = temp)) + 
  geom_raster(aes(fill = temp)) + # geom_contour() +
  scale_fill_gradientn(colours = topo.colors(10))

如果你想要一个像你展示的那样的图表,你必须进行插值。有些软件包为您提供空间插值方法。在这个例子中,我使用了akima包但你应该认真思考使用哪种插值方法。

I used nx = 300 and ny = 300在下面的代码中,但我认为最好决定这些值小心. Large nx and ny给出了高分辨率图表,但不要忘记真实的nx and ny(在这个例子中,真实的nx只有 9 并且ny是 101)。

library(akima); library(dplyr)

interp.data <- interp(main.df$dist, main.df$depth, main.df$temp, nx = 300, ny = 300)
interp.df <- interp.data %>% interp2xyz() %>% as.data.frame()
names(interp.df) <- c("dist", "depth", "temp")

# draw interp.df
ggplot(interp.df, aes(x = dist, y = depth, z = temp)) + 
  geom_raster(aes(fill = temp)) + # geom_contour() +
  scale_fill_gradientn(colours = topo.colors(10))

# to think appropriateness of interpolation (raw and interpolation data)
ggplot(interp.df, aes(x = dist, y = depth, z = temp)) + 
  geom_raster(aes(fill = temp), alpha = 0.3) +                 # interpolation
  scale_fill_gradientn(colours = topo.colors(10)) + 
  geom_point(data = main.df, aes(colour = temp), size = 1) +  # raw
  scale_colour_gradientn(colours = topo.colors(10))

底裤不搭配!!
I found ?interp说“仅在凸包内插值!”,哎呀......我担心问题区域周围的插值,可以吗?如果没问题的话,只需要把底部下面的数据剪掉就可以了。如果没有,...我无法立即回答(下面是要剪切的示例代码)。

bottoms <- max.depths * -1

# calculate bottom values using linear interpolation
approx.bottoms <- approx(dist, bottoms, n = 300) # n must be the same value as interp()'s nx

# change temp values under bottom into NA
library(dplyr)
interp.cut.df <- interp.df %>% cbind(bottoms = approx.bottoms$y) %>% 
  mutate(temp = ifelse(depth >= bottoms, temp, NA)) %>% select(-bottoms)

ggplot(interp.cut.df, aes(x = dist, y = depth, z = temp)) + 
  geom_raster(aes(fill = temp)) + 
  scale_fill_gradientn(colours = topo.colors(10)) + 
  geom_point(data = main.df, size = 1)

如果你想使用stat_contour
使用起来比较困难stat_contour than geom_raster因为它需要一个规则的网格形式。据我看到你的图表,你的数据(深度和距离)没有形成规则的网格,这意味着它很难使用stat_contour用你的原始数据。所以我用了interp.cut.df绘制等高线图。和stat_contour有地方性问题(参见如何使用 stat_contour 完全填充轮廓 https://stackoverflow.com/questions/28469829/how-to-fill-in-the-contour-fully-using-stat-contour/39701893),所以你需要扩展你的数据。

library(dplyr)

# 1st: change NA into a temp's out range value (I used 0)
interp.contour.df <- interp.cut.df
interp.contour.df[is.na(interp.contour.df)] <- 0

# 2nd: expand the df (It's a little complex, so please use this function)
contour.support.func <- function(df) {
  colname <- names(df)
  names(df) <- c("x", "y", "z")
  Range <- as.data.frame(sapply(df, range))
  Dim <- as.data.frame(t(sapply(df, function(x) length(unique(x)))))
  arb_z = Range$z[1] - diff(Range$z)/20
  df2 <- rbind(df,
               expand.grid(x = c(Range$x[1] - diff(Range$x)/20, Range$x[2] + diff(Range$x)/20), 
                           y = seq(Range$y[1], Range$y[2], length = Dim$y), z = arb_z),
               expand.grid(x = seq(Range$x[1], Range$x[2], length = Dim$x),
                           y = c(Range$y[1] - diff(Range$y)/20, Range$y[2] + diff(Range$y)/20), z = arb_z))
 names(df2) <- colname
 return(df2)
}

interp.contour.df2 <- contour.support.func(interp.contour.df)
# 3rd: check the temp range (these values are used to define contour's border (breaks))
range(interp.cut.df$temp, na.rm=T)        # 12.51622 20.18904

# 4th: draw ... the bottom border is dirty !!
ggplot(interp.contour.df2, aes(x = dist, y = depth, z = temp)) + 
  stat_contour(geom="polygon", breaks = seq(12.51622, 20.18904, length = 11), aes(fill = ..level..)) + 
  coord_cartesian(xlim = range(dist), ylim = range(bottoms), expand = F) + # cut expanded area
  scale_fill_gradientn(colours = topo.colors(10)) # breaks's length is 11, so 10 colors are needed

# [Note]
# You can define the contour's border values (breaks) and colors.
contour.breaks <- c(12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5) 
    # = seq(12.5, 20.5, 1) or seq(12.5, 20.5, length = 9)
contour.colors <- c("darkblue", "cyan3", "cyan1", "green3", "green", "yellow2","pink", "darkred")
   # breaks's length is 9, so 8 colors are needed.

# 5th: vanish the bottom border by bottom line
approx.df <- data.frame(dist = approx.bottoms$x, depth = approx.bottoms$y, temp = 0)  # 0 is dummy value

ggplot(interp.contour.df2, aes(x = dist, y = depth, z = temp)) + 
  stat_contour(geom="polygon", breaks = contour.breaks, aes(fill = ..level..)) +
  coord_cartesian(xlim=range(dist), ylim=range(bottoms), expand = F) + 
  scale_fill_gradientn(colours = contour.colors) +
  geom_line(data = approx.df, lwd=1.5, color="gray50")
bonus: legend technic
library(dplyr)
interp.contour.df3 <- interp.contour.df2 %>% mutate(temp2 = cut(temp, breaks = contour.breaks))
interp.contour.df3$temp2 <- factor(interp.contour.df3$temp2, levels = rev(levels(interp.contour.df3$temp2)))

ggplot(interp.contour.df3, aes(x = dist, y = depth, z = temp)) + 
  stat_contour(geom="polygon", breaks = contour.breaks, aes(fill = ..level..)) +
  coord_cartesian(xlim=range(dist), ylim=range(bottoms), expand = F) + 
  scale_fill_gradientn(colours = contour.colors, guide = F) +   # add guide = F
  geom_line(data = approx.df, lwd=1.5, color="gray50") +
  geom_point(aes(colour = temp2), pch = 15, alpha = 0) +        # add
  guides(colour = guide_legend(override.aes = list(colour = rev(contour.colors), alpha = 1, cex = 5))) +  # add
  labs(colour = "temp")   # add
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

深度剖析可视化 的相关文章

  • 使用facet_grid从ggplot中提取单个图

    我想使用 ggplot 和生成一些图facet grid并将绘图保存为对象 我的问题是我还想将每个子组 即每个方面 单独保存为一个对象 我现在的问题是你是否可以从中提取一个方面facet grid并将其保存为对象 这是一些简单的代码 lib
  • 在多面图中用 N 注释 x 轴

    我正在尝试生成一些按治疗条件和访问次数细分的数字结果的箱线图 每个框中的观察次数都放在图下方 并且也标记了访问次数 这里有一些虚假数据可以用来说明 我举了两个我尝试过但不太有效的例子 library ggplot2 library plyr
  • 创建后修改 ggplot 对象

    有没有首选的修改方式ggplot创建后的对象 例如 我建议我的学生将 r 对象与 pdf 文件一起保存以供以后更改 library ggplot2 graph lt ggplot mtcars aes x mpg y qsec fill c
  • 使用 ggplot 未完全填充等值线图

    我正在尝试使用以下方法绘制我的第一个填充等高线图ggplot 根据我的数据 我期待类似的结果 但我的结果是 a lt c 1 1 1 1 1 3 1 2 2 2 2 2 2 5 2 1 3 3 3 3 1 3 2 b lt c rep c
  • 双向条形图,两侧带有正标签ggplot2

    我尝试在 ggplot 中创建一个双向条形图 其中轴上方和下方的轴标签和数据标签均为正值 例如 如果您的数据是 myData lt data frame category c yes yes no no month c Jan Feb Ja
  • ggplot() 使用scale::percent_format() 缩放产生奇怪的结果

    library tidyverse mtcars gt count cyl gt mutate prop n sum n gt ggplot aes x cyl y prop geom point scale y continuous la
  • 如何在主图区域之外的 ggplot2 中添加多个标题

    我想为页脚添加两个标题 但 ggplot 似乎只需要 1 是否有解决方法可以将注释或 geom text 添加到左下角和右下角 library ggplot2 p lt ggplot mtcars aes x wt y mpg geom p
  • 无重叠的抖动点

    My data a lt sample 1 5 100 replace TRUE b lt sample 1 5 100 replace TRUE c lt sample 1 10 100 replace TRUE d lt sample
  • 如何融合颜色和形状?

    当我有一个超过 6 个值的变量时 我的麻烦就开始了 因为这是 ggplot2 中 scale shape 函数的当前最大值 由于这个问题 我尝试使用另一个变量来解决这个问题 我只是将原始变量的长度包裹起来 这是我的示例代码 dataf lt
  • 如何仅注释堆积条形图的一个类别

    我有一个数据框示例 如下所示 data Date 2021 07 18 2021 07 19 2021 07 20 2021 07 21 2021 07 22 2021 07 23 Invalid NaN 1 1 NaN NaN NaN N
  • stat_function 从函数生成平线

    我有以下代码 library ggplot2 f lt function x if x gt 2 1 x 0 3 else 0 graph lt ggplot data frame x c 0 10 aes x graph lt graph
  • 如何在 R 中绘制一列与其余列的关系图

    我有一个数据集 其中 1 是时间 接下来的 14 个是幅度 我想在一张图表上散布所有大小与时间的关系 其中每个不同的列都是网格化的 分层在另一个之上 我想使用原始数据来制作这些图表 并单独制作它们 但只想执行此过程一次 数据集A 唯一的自变
  • 使用officer R导出时如何提高ggplots的分辨率

    我想将图表导出到 PPT 并使用Officer 包来实现相同的目的 但是 图表的默认分辨率较低 我想更改它 我目前正在使用以下电话 ph with gg p1 type chart res 1200 其中 p1 是 ggplot 对象 运行
  • 从 data.frame 在 ggplot 图例中添加信息

    我想在图例中添加信息 哪个传感器具有该值 这是我的代码 z lt data frame a c sensor 1 sensor 2 sensor 3 sensor 4 sensor 5 sensor 6 sensor 7 sensor 8
  • 为什么这个 R ggplot2 代码会显示一个空白的显示设备?

    虽然 SO 通常不用于帮助解决错误 但这个显示了特别简单且特别烦人的行为 如果你是一个ggplot2用户 您可以在 10 秒或更短的时间内重现它 正如这个 GitHub 问题 ggplot gtable 创建空白显示 https githu
  • 如何根据 ggplot2 中的汇总数据创建堆积条形图

    我正在尝试使用 ggplot 2 创建堆积条形图 我的宽格式数据如下所示 每个单元格中的数字是响应的频率 activity yes no dontknow Social events 27 3 3 Academic skills works
  • 自定义轴缩放后 ggplot2 缺少标签

    我正在尝试使用我的 x 轴应用自定义缩放ggplot2 and scales trans new 但是 当我这样做时 一些轴标签丢失了 有人可以帮我弄清楚为什么吗 Setup library tidyverse the data ds lt
  • 更改 R 中 ggplot geom_polygon 的颜色方案

    我正在使用地图库和 ggplot 的 geom polygon 创建地图 我只是想将默认的蓝色 红色 紫色配色方案更改为其他颜色 我对 ggplot 非常陌生 所以如果我没有使用正确的数据类型 请原谅 我使用的数据如下所示 gt head
  • 如何使用plotmath更新ggplot图例标签

    我正在尝试更新ggplot要使用的图例标签plotmath但是 当我这样做时 它将之前组合的图例分成两部分 通过一个例子可能更容易理解 test data and the default plot gives the correct col
  • 如何使用 ggplot2 将 IPCC 点画添加到全球地图

    我需要将 IPCC style 点画添加到全球地图中 如下所示这个帖子 https stackoverflow com questions 11736996 adding stippling to image contour plot 不过

随机推荐