Skip to content

Plotting wind direction as arrows with precipitation

7 messages · DJ L, Jeff Newmiller, Bert Gunter +1 more

#
Hello R users!

I am trying to create a time series in R with two variables, precipitation
and wind direction vs Date/Time.

I am looking for suggestions and maybe even sample code.

My workbook is called "Sandy" and has columns with Date/Time, Raindall_cm,
Wind Direction in both degree format (0-359) and in character form (N, NW,
S, SW, SE, E, NE, NW).

I have done some reading for it on stackoverflow and other sites but not
making head way.

I will be graphing with ggplot most likely and am a beginner in R, self
taught from books and online resources.

This is the code I have and a small peak into the data.

Sandy<-read.csv("Sandy.csv", header=TRUE, sep=",",stringsAsFactors=FALSE)
Deal1     Rainfall_cm    Wind_Direction1 Wind_Direction2
1 10/22/2012 0:00           0        296         W
2 10/22/2012 0:15           0        317        NW
3 10/22/2012 0:30           0        323        NW
4 10/22/2012 0:45           0        323        NW
5 10/22/2012 1:00           0        326        NW
6 10/22/2012 1:15           0        326        NW
[1] "data.frame"
'data.frame':   1832 obs. of  4 variables:
 $ Deal1         : chr  "10/22/2012 0:00" "10/22/2012 0:15" "10/22/2012
0:30" "10/22/2012 0:45" ...
 $ Rainfall_cm   : num  0 0 0 0 0 0 0 0 0 0 ...

 $ Wind_Direction: num 296 317  323   323  326  326

 $ Wind_Direction: chr  "W" "NW" "NW" "NW" ...
Loading required package: ggplot2

# this graph does the precipitation vs time graph, but not the wind
geom_line(stat = "identity")


Ideally I want it to have the precipitation graph vs time, then wind vs
time on the same graph. I would like the wind direction to be arrows
pointing in the designated direction (i.e. North points north).

Thank you!
#
I don't see any conversion of your time data from character to a time type, so it is probably converting to factor within the ggplot function. Something like

Sys.setenv(TZ="Etc/GMT+5") # you need to study time types, including ?strptime

Sandy$Deal1 <- as.POSIXct( Sandy$Deal1, format="%m/%d/%Y %H:%M")

The other problem I see is that you probably need to have your data in long form to get the results you want, so you should look at the vignettes for the reshape2 or tidyr packages.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On August 10, 2015 12:05:27 AM EDT, DJ L <rhelp10 at gmail.com> wrote:
1 day later
#
Hi, 
Good example and data.  Thanks.

Here are a couple of approaches that may help. 
I tend to use a lot of what I tend to think of as the ggplot family of associated so you may need to install a couple packages. I used lubridate to transform your character dates to  POSIXct. Jeff N's code does exatly the same in base R. so you don't really need the lubridate package.

 I changed the data set name to dat1 and transformed the column names to lower case just for my convenience.  Data is now in dput() form. See ?dput() for more information. It is the preferred way to share data on R-help

Given what appears to be vastly different y-scales for rainfall and wind direction it struck me that it might be better to have the data in two plots so I included that option.

I am not really sure how to get the arrows you want. You may be able to do it using scale_shape_manual  but I am not sure if the required symbols are available.

You may have to manually draw them. See http://stackoverflow.com/questions/3421331/example-needed-using-arrow-with-ggplot2 for how to draw an arrow.  

John Kane
Kingston ON Canada
##============Start code==============
library(ggplot2)
library(reshape2)
library(lubridate)
library(gridExtra)

dat1  <-  structure(list(deal1 = c("10/22/2012 0:00", "10/22/2012 0:15", 
"10/22/2012 0:30", "10/22/2012 0:45", "10/22/2012 1:00", "10/22/2012 1:15"
), rainfall_cm = c(0L, 0L, 0L, 0L, 0L, 0L), wind_direction1 = c(296L, 
317L, 323L, 323L, 326L, 326L), wind_direction2 = c("W", "NW", 
"NW", "NW", "NW", "NW")), .Names = c("deal1", "rainfall_cm", 
"wind_direction1", "wind_direction2"), class = "data.frame", row.names = c(NA, 
-6L))


dat1$deal1 <- mdy_hm(dat1$deal1) # Lazy man's equivalent of Jeff N's Sandy$Deal1 <- as.POSIXct( Sandy$Deal1, format="%m/%d/%Y %H:%M")

dat2  <-  melt(dat1[ , 1:3], id.var = "deal1")  # use reshape to rearrange data.
p  <-  ggplot(dat2, aes(deal1, value, colour = variable) )+ geom_point()
p

## possible option
g1  <-  ggplot(dat1, aes(deal1,  wind_direction1)) + geom_point() + theme(axis.title.x=element_blank())
g2  <-  ggplot(dat1, aes(deal1, rainfall_cm ) )+ geom_point()

grid.arrange( g1, g2, ncol=1)

##===========end code==================
____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
#
... don't know if this will help, but grid graphics, which is the
graphics engine for both trellis and ggplot, has a basic arrow()
function. Trellis's provides an interface to it with the
panel.arrows() panel function. I suspect ggplot has something similar,
but as I don't use it, I don't know for sure.

There is also an arrows() function in the basic (non-grid) graphics
engine, but this is probably irrelevant for your needs.

Cheers,
Bert


Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll
On Tue, Aug 11, 2015 at 7:16 AM, John Kane <jrkrideau at inbox.com> wrote:
#
Thanks Bert,
I think that arrow()  would do what the OP needs. The main problem would be calculating the angles properly if I understand the issue. Still there cannot be "that" many points on a compass, can there?

John Kane
Kingston ON Canada
http://stackoverflow.com/questions/3421331/example-needed-using-arrow-with-ggplot2
____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
#
Thanks Bert,
I think that arrow()  would do what the OP needs. The main problem would be calculating the angles properly if I understand the issue. Still there cannot be "that" many points on a compass, can there?

John Kane
Kingston ON Canada
http://stackoverflow.com/questions/3421331/example-needed-using-arrow-with-ggplot2
____________________________________________________________
Can't remember your password? Do you need a strong and secure password?
Use Password manager! It stores your passwords & protects your account.
15 days later
#
Hello All,

Thank you so much for replying. Sorry I have been out of contact, I was in
the field with limited internet collecting more data. I will read over the
suggestions and will provide feedback or more questions. Looks like I have
some looking up to do on the packages suggested.

Also something I should have included was a general picture idea. See
picture attached of the goal in mind. I was able to create a rose diagram
with the wind data, but that is more of a summary vs time series showing
the direction and time.

I have also separated the degrees into 8 categories, to simply things. Rose
diagram, also attached.

Thank you again and talk soon!
On Tue, Aug 11, 2015 at 12:52 PM, John Kane <jrkrideau at inbox.com> wrote:

            
-------------- next part --------------
A non-text attachment was scrubbed...
Name: example.png
Type: image/png
Size: 151219 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150826/337e567c/attachment.png>