[R] Plotting wind direction as arrows with precipitation
John Kane
jrkrideau at inbox.com
Tue Aug 11 16:16:30 CEST 2015
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==================
> -----Original Message-----
> From: rhelp10 at gmail.com
> Sent: Mon, 10 Aug 2015 00:05:27 -0400
> To: r-help at r-project.org
> Subject: [R] Plotting wind direction as arrows with precipitation
>
> 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)
>> head(Sandy)
> 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
>
>> class(Sandy)
> [1] "data.frame"
>
>> str(Sandy)
> '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" ...
>
>> require(ggplot2)
> Loading required package: ggplot2
>
> # this graph does the precipitation vs time graph, but not the wind
>
>> ggplot(Sandy, aes(x = Deal1, y = Rainfall_cm, group = 1)) +
> 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!
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
More information about the R-help
mailing list