Improving Weather Analysis Graphs with R programming

Improving Weather Analysis Graphs with R programming

Yesterday’s post got us through a basic box plot of the average temperatures in Sedona to help us continue to evaluate the best time of year to go based on my personal preferences for the weather. ┬áToday I’m going to clean up that chart and make it a little nicer to look at and easier to analyze. ┬áTo do that, I’m using a very common package in R called ‘ggplot2‘.

First, I’m going to define the graph item by telling it what dataset to use (temps), what variables to plot (month, Max.TemperatureF), and what variable defines color and grouping(month):

This line of code builds the framework to host the next details we’ll be adding:
graph<-ggplot(temps, aes(x=month, y=Max.TemperatureF, color=month, group=month))

Graph Object

With the framework built, I can start adding on design elements.

First, let’s add a jitter to spread out the datapoint for each day and let’s assign the colors automatically:

graph+ geom_point(position=position_jitter(width=0.3), alpha=0.1) +
scale_fill_brewer()

Graph points

Next, we’ll add back in the box plot and reference lines.


graph+
geom_point(position=position_jitter(width=0.3), alpha=0.1) +
scale_fill_brewer()+
geom_boxplot(fill="grey")+
geom_hline(yintercept=55, linetype="dashed")+
geom_hline(yintercept=72, linetype="dashed")

BoxPlot_Nolabels

Finally, we’ll clean it up with some better labels.

graph+
geom_point(position=position_jitter(width=0.3), alpha=0.1) +
scale_fill_brewer()+
geom_boxplot(fill="grey")+
geom_hline(yintercept=55, linetype="dashed")+
geom_hline(yintercept=72, linetype="dashed")+
labs(y="Max Temperature Range")+
labs(x="Month")+
labs(title="Max Temperature Range in Sedona, AZ by Month")

MaxTemp_FinalBoxplot

As you can see, this is a really flexible package that allows for a lot of customization. Looking at the Max temp data instead of Average temp also tells a little different story about when might be the optimal time for a visit. April and October now fall squarely in the desired range, while the summer months are now above it. It also shows that the hottest max temps typically occur in June, not in the later summer months.

Leave a Reply

Your email address will not be published. Required fields are marked *