Contour lines in ggplot2

為{幸葍}努か 提交于 2019-12-01 21:24:11

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!