r - ggplot2 中的等高线

标签 r plot ggplot2 scale contour

我想知道如何使用 ggplot2 中的 stat_contour 更改等高线的比例。

现在我正在使用这段代码(下方)将 PAR 的等高线图绘制为 30m,但我的图仅显示顶部 10m 的等高线并且仅显示达到最小值 10^-5 的线.但是,我有长达 30 m 的 z 数据,其范围从 4*10^-9 到 1.4*10^-3 。我如何让轮廓线反射(reflect)这一点?

require(ggplot2)
library(directlabels)

Depth<- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30)
Time.hour.of.the.day. <-c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)
PAR<-c(8.42E-04,9.75E-04,9.02E-04,9.47E-04,9.22E-04,9.36E-04,9.91E-04,1.04E-03,8.62E-04,9.95E-04,1.04E-03,1.28E-03,1.37E-03,1.46E-03,1.25E-03,1.09E-03,1.07E-03,9.79E-04,1.01E-03,9.42E-04,9.42E-04,8.43E-04,3.53E-04,4.09E-04,3.79E-04,3.98E-04,3.87E-04,3.93E-04,4.16E-04,4.35E-04,3.62E-04,4.18E-04,4.38E-04,5.37E-04,5.74E-04,6.13E-04,5.24E-04,4.57E-04,4.47E-04,4.11E-04,4.23E-04,3.96E-04,3.95E-04,3.54E-04,1.49E-04,1.73E-04,1.60E-04,1.68E-04,1.63E-04,1.66E-04,1.76E-04,1.84E-04,1.53E-04,1.76E-04,1.85E-04,2.27E-04,2.42E-04,2.59E-04,2.21E-04,1.93E-04,1.89E-04,1.74E-04,1.79E-04,1.67E-04,1.67E-04,1.49E-04,6.29E-05,7.29E-05,6.74E-05,7.08E-05,6.89E-05,6.99E-05,7.40E-05,7.74E-05,6.44E-05,7.44E-05,7.79E-05,9.55E-05,1.02E-04,1.09E-04,9.32E-05,8.14E-05,7.96E-05,7.32E-05,7.52E-05,7.04E-05,7.04E-05,6.30E-05,2.70E-05,3.12E-05,2.89E-05,3.03E-05,2.95E-05,3.00E-05,3.17E-05,3.32E-05,2.76E-05,3.19E-05,3.34E-05,4.10E-05,4.38E-05,4.67E-05,3.99E-05,3.49E-05,3.41E-05,3.14E-05,3.23E-05,3.02E-05,3.02E-05,2.70E-05,1.16E-05,1.35E-05,1.25E-05,1.31E-05,1.27E-05,1.29E-05,1.37E-05,1.43E-05,1.19E-05,1.37E-05,1.44E-05,1.77E-05,1.89E-05,2.02E-05,1.72E-05,1.50E-05,1.47E-05,1.35E-05,1.39E-05,1.30E-05,1.30E-05,1.16E-05,5.12E-06,5.93E-06,5.48E-06,5.76E-06,5.60E-06,5.69E-06,6.02E-06,6.29E-06,5.24E-06,6.05E-06,6.34E-06,7.77E-06,8.31E-06,8.87E-06,7.58E-06,6.62E-06,6.48E-06,5.95E-06,6.12E-06,5.73E-06,5.72E-06,5.12E-06,2.28E-06,2.64E-06,2.44E-06,2.56E-06,2.49E-06,2.53E-06,2.68E-06,2.80E-06,2.33E-06,2.69E-06,2.82E-06,3.46E-06,3.69E-06,3.94E-06,3.37E-06,2.94E-06,2.88E-06,2.65E-06,2.72E-06,2.55E-06,2.55E-06,2.28E-06,1.02E-06,1.18E-06,1.09E-06,1.15E-06,1.12E-06,1.13E-06,1.20E-06,1.25E-06,1.04E-06,1.21E-06,1.26E-06,1.55E-06,1.66E-06,1.77E-06,1.51E-06,1.32E-06,1.29E-06,1.19E-06,1.22E-06,1.14E-06,1.14E-06,1.02E-06,4.60E-07,5.33E-07,4.93E-07,5.18E-07,5.04E-07,5.12E-07,5.42E-07,5.66E-07,4.71E-07,5.44E-07,5.70E-07,6.99E-07,7.47E-07,7.98E-07,6.82E-07,5.95E-07,5.83E-07,5.35E-07,5.51E-07,5.15E-07,5.15E-07,4.61E-07,2.09E-07,2.43E-07,2.24E-07,2.36E-07,2.29E-07,2.33E-07,2.46E-07,2.57E-07,2.14E-07,2.47E-07,2.59E-07,3.18E-07,3.40E-07,3.63E-07,3.10E-07,2.71E-07,2.65E-07,2.43E-07,2.50E-07,2.34E-07,2.34E-07,2.09E-07,9.57E-08,1.11E-07,1.03E-07,1.08E-07,1.05E-07,1.06E-07,1.13E-07,1.18E-07,9.81E-08,1.13E-07,1.19E-07,1.45E-07,1.55E-07,1.66E-07,1.42E-07,1.24E-07,1.21E-07,1.11E-07,1.15E-07,1.07E-07,1.07E-07,9.58E-08,4.39E-08,5.09E-08,4.70E-08,4.94E-08,4.81E-08,4.88E-08,5.17E-08,5.40E-08,4.50E-08,5.19E-08,5.44E-08,6.67E-08,7.13E-08,7.61E-08,6.50E-08,5.68E-08,5.56E-08,5.11E-08,5.25E-08,4.91E-08,4.91E-08,4.39E-08,2.01E-08,2.33E-08,2.15E-08,2.26E-08,2.20E-08,2.23E-08,2.36E-08,2.47E-08,2.06E-08,2.37E-08,2.49E-08,3.05E-08,3.26E-08,3.48E-08,2.98E-08,2.60E-08,2.54E-08,2.34E-08,2.40E-08,2.25E-08,2.25E-08,2.01E-08,9.20E-09,1.07E-08,9.86E-09,1.03E-08,1.01E-08,1.02E-08,1.08E-08,1.13E-08,9.42E-09,1.09E-08,1.14E-08,1.40E-08,1.49E-08,1.59E-08,1.36E-08,1.19E-08,1.16E-08,1.07E-08,1.10E-08,1.03E-08,1.03E-08,9.20E-09,4.20E-09,4.86E-09,4.50E-09,4.72E-09,4.59E-09,4.66E-09,4.94E-09,5.16E-09,4.30E-09,4.96E-09,5.20E-09,6.37E-09,6.81E-09,7.27E-09,6.21E-09,5.43E-09,5.31E-09,4.88E-09,5.02E-09,4.70E-09,4.69E-09,4.20E-09)

up_PAR = data.frame(Depth,Time.hour.of.the.day.,PAR)

par <- ggplot(up_PAR,aes(Time.hour.of.the.day., Depth, z = PAR))
parplot <- par + stat_contour(bins=20,bandwith=4,  aes(colour=..level..)) +
scale_colour_gradient(low="black", high="black") +
scale_y_reverse(lim=c(30,0)) +
theme_bw() + 
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
xlab("Hour of theDay") + ylab("Depth(m)")
direct.label(parplot)

parplot

enter image description here

最佳答案

4*10^-9 和 1.4*10^-3 之间有很大的区别。与 1.4*10^-3 相比,4*10^-9 附近的值几乎为零。见下图:

plot(up_PAR$PAR, up_PAR$Depth, ylim=rev(range(up_PAR$Depth)) ,ylab="Depth", xlab="PAR")

enter image description here

stat_contour 通常用于空间数据(空间中的二维),而你的是空间中的一维。

您必须以非常小的间隔设置 binwidth(而不是带宽)才能在所有深度范围内查看 PAR 信息,但不要这样做。它不仅会产生糟糕的情节(在上面的一半中线条会变得模糊),而且还会使 R 崩溃。

您可以以更有趣的方式显示此信息。例如:

require(ggplot2)

ggplot(up_PAR, aes(Time.hour.of.the.day., PAR, group=Depth,colour=Depth)) + 
  geom_line(size=1) + 
  scale_colour_gradient("Depth (m)", low="darkblue", high="brown", guide=guide_colourbar(reverse=T)) +
  xlab("Time of the day (hour)") +
  theme_bw() +
  theme(axis.title = element_text(size=20),
        axis.title.x = element_text(vjust=-0.3),
        axis.title.y = element_text(vjust=0.4),
        axis.text = element_text(size=16),
        legend.title = element_text(size=15),
        legend.text = element_text(size=13))

enter image description here

关于r - ggplot2 中的等高线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25062492/

相关文章:

r - 基于 R 中的其他列创建列序列

r - Shiny 中复选框输入的流体行问题

r - ggpubr 找不到 'mean_se' 除非 ggpubr 是通过 library() 附加的

r - ggplot在轴标签上的度数符号之前插入空格

r - Shiny :为什么在服务器端使用临时变量输出失败?

r - 使用 aes_string 时 ggplot2 中两个 geom_point() 的颜色

matlab - 读取 MATLAB 中的值并将坐标分配给条目

plot - 对 VegaLite map 的边缘设置限制

java - 为什么打开 Web 浏览器时 Java 的计时会受到影响?

r - scale_x_discrete 未显示在绘图上