我想知道如何使用ggplot2中的stat_contour更改轮廓线的比例。
现在我正在使用这段代码(下图)将PAR的等高线图绘制到30米,但我的绘图仅显示前10米的等高线,并且仅显示最小值为10 ^ -5的线条。但是,我有z数据长达30米,其范围从4 * 10 ^ -9到1.4 * 10 ^ -3。如何让轮廓线反映出来?
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
答案 0 :(得分:1)
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")
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))
答案 1 :(得分:1)
不考虑这是否是显示数据的好方法的问题,这里有两个显示轮廓直到30米的选项。基本思路是,由于PAR
中的范围是几个数量级,您可能需要使用对数刻度。
brks <- 10^-(3:9)
par <- ggplot(up_PAR,aes(Time.hour.of.the.day., Depth, z = PAR))
parplot <- par + stat_contour(aes(colour=..level..),breaks=brks) +
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)
这利用了breaks=...
stat_contour(...)
的{{1}}参数。您可以将breaks
设置为任何矢量,例如,如果您希望顶部的轮廓线更接近,请使用以下内容:
brks <- c(seq(1e-3,1e-4,len=5),10^-(4:9))
这是带有图例的ggplot
解决方案。
library(RColorBrewer) # for brewer.pal(...)
par <- ggplot(up_PAR,aes(Time.hour.of.the.day., Depth, z = PAR))
parplot <- par + stat_contour(aes(colour=..level..), breaks=brks) +
scale_colour_gradientn("PAR",trans="log10",breaks=brks,colours=brewer.pal(9,"Reds")[3:9]) +
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)")
parplot