POTM-03 (July): Overlaying histograms

1 year 11 months ago - 1 year 10 months ago #25 by Kari_Pohl
Kari_Pohl created the topic: POTM-03 (July): Overlaying histograms
Hi Everyone! Here is July’s Plot of the Month!

This plot shows three overlapping histograms displaying the monthly maximum, minimum, and mean atmospheric temperature for Jug Bay, MD (CBMJB).

For my research, I look at temperature and precipitation extremes. So, I had an interest in visualizing the frequency of each month’s hottest and coldest measured temperature. Rather than look at 3 different histograms, I combined them into on plot using semi-transparent colors so they can overlap.

Thus, dark purple represents the temperature range were the minimum, maximum, and mean temperatures all intersected.

But before we get the plot, the data has to be aggregated from the 15 minute collection intervals to a monthly value (either mean, max, or min). SWMPr has a function for that!

Note that I downloaded 4 years of meteorological data from the Centralized Management Data Office website for Jug Bay, MD, a Chesapeake Bay NERR site.
#load SWMPr package
#Set your working directory 
mypath <- 'C:/Users/Kari/Documents/RData/CBNERRs'
#use the import_local function to get the Jug Bay CBNERR data 
#previously downloaded for 2010-2014
#Import_local function is great since it will now be a SWMPr object
mydataMET <- import_local(mypath, 'CBMJBMET', trace = T)
#I always like to check my script to see if everything input correctly
###Now to aggregate into months for max, min, and mean 
max<-aggreswmp(mydataMET, 'months', function(x) max(x, na.rm = TRUE), 
params = 'atemp')
min<-aggreswmp(mydataMET, 'months', function(x) min(x, na.rm = TRUE), 
params = 'atemp')
mean<-aggreswmp(mydataMET, 'months', function(x) mean(x, na.rm = TRUE), 
params = 'atemp')

This script should have computed the monthly mean air temperature (or any other parameter), as well as the highest and lowest temperature measured for each month.

Now onto the plot! A histogram allows us to see the temperature spread as well as the frequency of each temperature. I like the rgb (red, green, blue) color specifier since you can easily adjust the color transparency, allowing for overlays.

In my plot, I choose red to represent the monthly maximum temperature, blue to represent the monthly minimum, and violet for the mean; this way, it was easier to see the overlaps by the shade of violet produced. You can play around with the colors (or do it in shades of gray; setting each rgb color to 0 equates to black and 1 equates to white) to optimize your viewing experience!
#Now for the histograms!
hist(max, col=rgb(1,0,0,0.5),xlim=c(-20,45),
ylim=c(0,20), main="Air Temperature", 
hist(min, col=rgb(0,0,1,0.5), add=T)
hist(mean, col=rgb(.5,0,.5,0.5), add=T,axes = F)
#I like having a box around my plot, but you can leave this out if you want!
legend("topleft", c("Max","Min","Mean"), cex=1.0, bty="n",
   col=c(rgb(200,0,0, 100, maxColorValue=255),
rgb(0,0,200, 100, maxColorValue=255),
rgb(125,0,175, 100, maxColorValue=255)),pch=19)

Okay, this code will get you the Figure above. But play around with it to “visualize” your data how you want. For example, you could subset the monthly data to look at only extreme temperatures in July (see last month’s post for guidance!).

This was one of the first things I did with the CBNERR data when I started, so I hope it helps!
Last Edit: 1 year 10 months ago by Todd.OBrien. Reason: Standardizing Titles

Please Log in to join the conversation.

1 year 11 months ago #26 by Marcus Beck
Marcus Beck replied the topic: July's Plot of the Month: Overlaying histograms
Great post Kari, and thanks for sharing. Excellent use of the aggreswmp function

The color with transparencies is a neat trick and a great way to visualize areas of the distribution that are shared between categories. I might steal this idea for some of my own plots!

Please Log in to join the conversation.

Time to create page: 0.327 seconds
Powered by Kunena Forum