Hi Mackay and anybody
(a) Is it possible to select randomly (let say five grids) and plot?
(b) Is it possible to plot five nearest grid in one figure?
The original question and improved codes:
#I am ploting gridded time series data. I would like the actual lat
#and lon value appear on the graph-if possible inside the graph as
#numbers. If there is also more elegant ways to plot the graphs I
#>will appreciate more suggestions.
#################################
library(ggplot2)
library(lattice)
month <- c("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov",
"Dec")
month <- factor(month, levels = month.abb)
data <- as.data.frame(expand.grid(lon=seq(4,5, 1), lat=seq(-3,-2,1),
year=seq(2010, 2012,1), month=month))
n=nrow(data)
data$prec <- rgamma(n, 2, 0.25)
data$year <- factor(data$year)
data$lon <- factor(data$lon)
data$lat <- factor(data$lat)
ndata <- data[order(data$lon, data$lat),]
fix(ndata)
max <- max(ndata$prec, na.rm=TRUE)
min <- min(ndata$prec, na.rm=TRUE)
#Plot the graph and save as pdf by Mackay suggestion
pdf("H:/file.pdf")
library(latticeExtra)
useOuterStrips(
xyplot(prec~month|year*paste(lat,lon), data=ndata,
as.table = T,
type = c("l", "l","p"), ylim=c(min, max),
layout=c(1,4)) )
dev.off()#The actual data have more than 5 thousand data points, Just want to
plot a few
#selected randomly. Also, if I can put 5 data points (nearest grids) in one
graph
#will be more helpful.
Peter Maclean
Department of Economics
UDSM
[[alternative HTML version deleted]]
> The actual data have more than 5 thousand data points, Just want to plot a few > #selected randomly.Based on this, it seems you're looking to pull a random sample of 5 from your dataset. Does this link help? http://stat.ethz.ch/R-manual/R-patched/library/base/html/sample.html ~Nicole Ford Ph.D. Student Graduate Assistant/ Instructor Department of Government and International Affairs University of South Florida office: SOC 012M e: nmhicks@mail.usf.edu http://gia.usf.edu/student/nford/ Sent from my iPhone On Feb 22, 2013, at 6:15 PM, Peter Maclean <pmaclean2011@yahoo.com> wrote:> Hi Mackay and anybody > (a) Is it possible to select randomly (let say five grids) and plot? > (b) Is it possible to plot five nearest grid in one figure? > The original question and improved codes: > > > #I am ploting gridded time series data. I would like the actual lat > #and lon value appear on the graph-if possible inside the graph as > #numbers. If there is also more elegant ways to plot the graphs I > #>will appreciate more suggestions. > ################################# > library(ggplot2) > library(lattice) > month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", > "Aug", "Sep", "Oct", "Nov", "Dec") > month <- factor(month, levels = month.abb) > data <- as.data.frame(expand.grid(lon=seq(4,5, 1), lat=seq(-3,-2,1), > year=seq(2010, 2012,1), month=month)) > n=nrow(data) > data$prec <- rgamma(n, 2, 0.25) > data$year <- factor(data$year) > data$lon <- factor(data$lon) > data$lat <- factor(data$lat) > ndata <- data[order(data$lon, data$lat),] > fix(ndata) > max <- max(ndata$prec, na.rm=TRUE) > min <- min(ndata$prec, na.rm=TRUE) > > #Plot the graph and save as pdf by Mackay suggestion > pdf("H:/file.pdf") > library(latticeExtra) > useOuterStrips( > xyplot(prec~month|year*paste(lat,lon), data=ndata, > as.table = T, > type = c("l", "l","p"), ylim=c(min, max), > layout=c(1,4)) ) > dev.off()#lso, if I can put 5 data points (nearest grids) in one graph > #will be more helpful. > > > Peter Maclean > Department of Economics > UDSM > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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.[[alternative HTML version deleted]]
As to your xyplot question, I found a multiline solution. (Below) I am pretty certain there is a multipolt/ graph solution in the effects package (I haven't used that functionality in some years). http://r.789695.n4.nabble.com/XYplot-in-Lattice-Package-td878511.html ~Nicole Ford Ph.D. Student Graduate Assistant/ Instructor Department of Government and International Affairs University of South Florida office: SOC 012M e: nmhicks@mail.usf.edu http://gia.usf.edu/student/nford/ Sent from my iPhone On Feb 22, 2013, at 6:15 PM, Peter Maclean <pmaclean2011@yahoo.com> wrote:> Hi Mackay and anybody > (a) Is it possible to select randomly (let say five grids) and plot? > (b) Is it possible to plot five nearest grid in one figure? > The original question and improved codes: > > > #I am ploting gridded time series data. I would like the actual lat > #and lon value appear on the graph-if possible inside the graph as > #numbers. If there is also more elegant ways to plot the graphs I > #>will appreciate more suggestions. > ################################# > library(ggplot2) > library(lattice) > month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", > "Aug", "Sep", "Oct", "Nov", "Dec") > month <- factor(month, levels = month.abb) > data <- as.data.frame(expand.grid(lon=seq(4,5, 1), lat=seq(-3,-2,1), > year=seq(2010, 2012,1), month=month)) > n=nrow(data) > data$prec <- rgamma(n, 2, 0.25) > data$year <- factor(data$year) > data$lon <- factor(data$lon) > data$lat <- factor(data$lat) > ndata <- data[order(data$lon, data$lat),] > fix(ndata) > max <- max(ndata$prec, na.rm=TRUE) > min <- min(ndata$prec, na.rm=TRUE) > > #Plot the graph and save as pdf by Mackay suggestion > pdf("H:/file.pdf") > library(latticeExtra) > useOuterStrips( > xyplot(prec~month|year*paste(lat,lon), data=ndata, > as.table = T, > type = c("l", "l","p"), ylim=c(min, max), > layout=c(1,4)) ) > dev.off()#The actual data have more than 5 thousand data points, Just want to plot a few > #selected randomly. Also, if I can put 5 data points (nearest grids) in one graph > #will be more helpful. > > > Peter Maclean > Department of Economics > UDSM > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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.[[alternative HTML version deleted]]
Hi Peter
Try the 2 examples below - it may give you some ideas
useOuterStrips(strip =
strip.custom(factor.levels = paste("Year", c(2010:2012)),
par.strip.text = list(cex = 0.75)),
strip.left =
strip.custom(factor.levels = paste("Lat.",
rep(c(3,2) ,each = 2), "? S, Long.", rep(5:4, 2), "? W"),
par.strip.text = list(cex = 0.75)),
xyplot(prec~month|year*paste(lat,lon), data=ndata,
as.table = T,
scales = list(x = list(alternating = FALSE,
relation = "same",
rot = 60),
y = list(alternating = FALSE,
relation = "same",
rot = 0)
),
ylim=c(min, max),
type = c("b"),
col = 1
) # xyplot **
) ## useOuterStrips
useOuterStrips(strip =
strip.custom(factor.levels = paste("Year", c(2010:2012)),
par.strip.text = list(cex = 0.75)),
strip.left = FALSE,
xyplot(prec~month|year*paste(lat,lon), data=ndata,
as.table = T,
scales = list(x = list(alternating = FALSE,
relation = "same",
rot = 60),
y = list(alternating = FALSE,
relation = "same",
rot = 0)
),
ylim=c(min, max),
col = 1,
panel = function(x,y ,...){
pnl = panel.number()
if (pnl %in% c(1:6))
panel.xyplot(x,y, type = "b",...) else panel.xyplot(x,y, type =
"l",...)
grid.text(label =
paste("Lat.", rep(c(3,2) ,each = 6)[pnl], "? S,
Long.", rep(rep(5:4, 3),2)[pnl], "? W"),
x = unit(.1, "npc"),
y = unit(.8, "npc"),
hjust = NULL, vjust = NULL, rot = 0,
just = "left", default.units =
"npc",
gp = gpar(cex = 0.8) )
}
) # xyplot **
)
If you want to have selected? points aggregate first
For sampling you would have to sample that first
and then use abs()[5] to get the five nearest
Not sure what you mean exactly and without a
sample of the 5000 cannot help further
HTH
Regards
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
At 09:15 23/02/2013, you wrote:>Hi Mackay and anybody
>(a) Is it possible to select randomly (let say five grids) and plot?
>(b) Is it possible to plot five nearest grid in one figure?
>The original question and improved codes:
>
>
>#I am ploting gridded time series data. I would like the actual lat
>#and lon value appear on the graph-if possible inside the graph as
>#numbers. If there is also more elegant ways to plot the graphs I
>#>will appreciate more suggestions.
>#################################
>library(ggplot2)
>library(lattice)
>month <- c("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul",
>"Aug", "Sep", "Oct", "Nov",
"Dec")
>month <- factor(month, levels = month.abb)
>data <- as.data.frame(expand.grid(lon=seq(4,5, 1), lat=seq(-3,-2,1),
>year=seq(2010, 2012,1), month=month))
>n=nrow(data)
>data$prec <- rgamma(n, 2, 0.25)
>data$year <- factor(data$year)
>data$lon <- factor(data$lon)
>data$lat <- factor(data$lat)
>ndata <- data[order(data$lon, data$lat),]
>fix(ndata)
>max <- max(ndata$prec, na.rm=TRUE)
>min <- min(ndata$prec, na.rm=TRUE)
>
>#Plot the graph and save as pdf by Mackay suggestion
>pdf("H:/file.pdf")
>library(latticeExtra)
>useOuterStrips(
>xyplot(prec~month|year*paste(lat,lon), data=ndata,
>as.table = T,
>type = c("l", "l","p"), ylim=c(min, max),
>layout=c(1,4)) )
>dev.off()#The actual data have more than 5
>thousand data points, Just want to plot a few
>#selected randomly. Also, if I can put 5 data
>points (nearest grids) in one graph
>#will be more helpful.
>
>
>Peter Maclean
>Department of Economics
>UDSM
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>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.