Contour lines in ggplot2

2019-07-06 01:11发布

问题:

I am wondering how to change the scale of my contour lines using stat_contour in ggplot2.

Right now I am using this bit of code (below) to make contour plot of PAR to 30m, but my plot only shows contour lines in the top 10m and only shows lines that go to a minimum value of 10^-5. However, I have z data for up to 30 m and which range from values from 4*10^-9 to 1.4*10^-3 . How do I get my contour lines to reflect this?

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

回答1:

There is a huge difference between 4*10^-9 and 1.4*10^-3. In comparison with 1.4*10^-3, values around 4*10^-9 are practically zero. See the plot below:

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

stat_contour is usually used for spatial data (bidimensional in space) and yours is unidimensional in space.

You would have to set binwidth (and not bandwith) with very small interval to see PAR information in all range of Depths, but don't do that. Not only would it yield to an awful plot (in the above half the lines would be blurred), but also it would crash R.

You can show this information in a more interesting way. For example:

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))



回答2:

Leaving aside the question of whether this is a good way to display your data, here are two options that display contours all the way down to 30m. The basic idea is that, since the range in PAR is several orders of magnitude, you might want to use a log scale.

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)

This takes advantage of the (poorly documented) breaks=... argument to stat_contour(...). You can set breaks to any vector, so for example if you want the contour lines closer together at the top, use something like:

brks <- c(seq(1e-3,1e-4,len=5),10^-(4:9))

And here's the ggplot solution with a legend.

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