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]]
Jeff Newmiller
2015-Aug-10 12:01 UTC
[R] Plotting wind direction as arrows with precipitation
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:>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.
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!
Bert Gunter
2015-Aug-11 14:55 UTC
[R] Plotting wind direction as arrows with precipitation
... 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:> 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! > > ______________________________________________ > 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.