I have some data I want to draw over the London map.
But whenever I try to run following code:
library("Hmisc")
library("lubridate")
library("maptools")
require("rworldmap")
mtl <- readShapePoly('borough/London_Borough_Excluding_MHW.shp')
data = read.csv("test.csv")
data = data[,c("Accident_Index","Longitude","Latitude","Date")]
data$Date <- as.character(data$Date)
data$Date <- gsub(' ', '', data$Date)
data$Date <- dmy(data$Date, tz='UTC')
start <- (dmy("01/01/2011", tz='UTC'))
days <- start
# pre processed matrix for incidents
# trust me you don't want to calculate
# this from scratch (Takes hours)
load('time_mat.Rdata')
# plot initial world map
plot(getMap())
da <- 1
# where the plots will be outputted
png(paste('plots/',days[da],'.png',sep=''), height=800, width=1200, bg='grey')
# plot london map
plot(mtl,col='darkgrey')
text(51.5072, 0.1275,paste("Collisions in London\n",days[da]),cex=2)
points(data$Longitude,data$Latitude, pch=20, col='white', cex=2)
par(lwd=2.5,cex=1.4)
subplot(
plot(days[1:da],incident_rate[1:da],
lwd=1.5,type='l',
ylab='', xlab='',
col='red',
ylim=c(0,max(c(1,max(incident_rate[1:da]))))
),
size=c(5.5,2), 51.5072, -0.1275, vadj=0,hadj=0
)
dev.off()
Error in par(plt = c(xy$x, xy$y), new = TRUE) :
invalid value specified for graphical parameter "plt"
EDIT 2: Re-factored and reduced the code.
Zipped up the entire code folder, and dependent data for ease of deployment/testing, ended up being ~17mb compressed (due to the shapefiles):
Megafileupload link
Related
I am trying to apply Hierarchical Clustering for Time Series in order to identify the states with similar behaviors in the time series for residential_percent_change_from_baseline. I get the dendrogram but the index i get in the x axis are just numbers and I want the states names.
my data looks like this:
Data
And this is some part of my code
data <- dataset
#Convert to factor
cols <- c("country_region_code", "country_region", "sub_region_1", "iso_3166_2_code")
data[cols] <- lapply(data[cols], factor)
sapply(data, class)
data$date <- as.Date(data$date)
summary(data)
#Data preparation
n <- 10
s <- sample(1:100, n)
i <- c(s,0+s, 279+s, 556+s, 833+s, 1110+s, 1387+s, 1664+s, 1941+s, 2218+s, 2495+s, 2772+s, 3049+s, 3326+s, 3603+s, 3880+s, 4157+s, 4434+s, 4711+s, 4988+s, 5265+s, 5542+s, 5819+s, 6096+s, 6373+s, 6650+s, 6927+s, 7204+s, 7481+s, 7758+s, 8035+s, 8312+s, 8589+s, 8866+s)
d <- data[i,3:4]
d$residential <- data[i,11]
d[,2] =NULL
str(d)
pattern <- c(rep('Mexico', n),
rep('Aguascalientes', n),
rep('Baja California',n),
rep('Baja California Sur',n),
rep('Campeche',n),
rep('Coahuila',n),
rep('Colima',n),
rep('Chiapas',n),
rep('Chihuahua',n),
rep('Durango',n),
rep('Guanajuato',n),
rep('Guerrero',n),
rep('Hidalgo',n),
rep('Jalisco',n),
rep('México City',n),
rep('Michoacan',n),
rep('Morelos',n),
rep('Nayarit',n),
rep('Nuevo León',n),
rep('Oaxaca',n),
rep('Puebla',n),
rep('Querétaro',n),
rep('Quintana Roo',n),
rep('San Luis Potosí',n),
rep('Sinaloa',n),
rep('Sonora',n),
rep('Tabasco',n),
rep('Tamaulipas',n),
rep('Tlaxcala',n),
rep('Veracruz',n),
rep('Yucatán',n),
rep('Zacatecas.',n))
d <- data.matrix(d)
distance <- dist(d, method = 'euclidean')
hc <- hclust(distance, method="ward.D")
plot(hc, cex=.7, hang = -1, col='blue', labels=pattern)
I get this dendrogram when I don't specify labels
dendrogram with numeric labels
But when I do I get this error
Error in graphics:::plotHclust(n1, merge, height, order(x$order), hang, : invalid dendrogram input
I hope somebody can help me, I am little bit tired of this
Maybe it will work with an alternative to the base r plot function. Try ggdendroplot. It should display the labels on the axis. You will need ggplot2 for this.
devtools::install("nicolash2/ggdendroplot")
library(ggdendroplot)
library(ggplot2)
ggplot() + geom_dendro(hc)
If you want to modify it (turn it, color it, etc.) check out the github page: https://github.com/NicolasH2/ggdendroplot
I am using the rgl package to create an interactive .html file but I am getting an error in line 42. I am fairly new to this package and can't seem to define rgl_init function in line 44, so any help will be appreciated. I did find a solution (R shiny error: Cannot coerce type 'closure' to vector of type 'double'), but I don't know how to implement it to this code.
#3D convex hull of the individual tree LiDAR-derived
#Import the LAS file
LAS_File <- system.file("test.las")
#Reading
library(rLiDAR)
LAS <- readLAS("D:/Summer_2019/NRS_Project/01_data/01_las_2013/test.las")
#Setup the coordinates and data subsetting
xyz <- subset(LAS[,1:3],LAS[,3] >= 1.37)
#Find the clusters
clLAS<-kmeans(xyz, 32)
#Setup the id vector
ID<-as.factor(clLAS$cluster)
#Setting the alpha
Alpha<-0.6
#Setting the plotCAS parameter
plotit=TRUE
#Setting the convex hull color
col="forestgreen"
#Combine the xyz and id
xyzID<-cbind(xyz,ID)
#Computing the volume and surface area
library(rgl)
open3d()
voumelist<-chullLiDAR3D(xyzid = xyzID, plotit = plotit, col = col, alpha = Alpha)
summary(voumelist)
#summary
#Write CSV file
write.table(voumelist, file = 'test_volume.csv', row.names=FALSE,col.names=FALSE)
write.csv(voumelist, "test_volume.csv")
#Adding plot parameters
#Adding the 3D point cloud
plot3d(xyzID[, 1:3], col = xyzID[,4], add=TRUE)
#Axis
axes3d(c("x+", "y-", "z-"))
#Grids
grid3d(side = c('x+', 'y-', 'z'), col='gray')
title3d(xlab="UTM Easting", ylab="UTM Northing", zlab = "Height", col="red")
#Scaling
aspect3d(1,1,0.7)
#Save HTML file
library(rmarkdown)
writeWebGL(dir = "D:/Summer_2019/NRS_Project/01_data/01_las_2013", filename = file.path(dir, "D:/Summer_2019/NRS_Project/01_data/01_las_2013/Test_index.html"))
#Write a copy to display in Chrome
rgl_init()
rgl.spheres(x, y, z, r = 0.2,
color = get_colors(iris$Species))
rgl_add_axes(x, y, z, show.bbox = FALSE)
# This writes a copy into temporary directory 'webGL',
# and then displays it
browseURL(
paste("D:/Summer_2019/NRS_Project/01_data/01_las_2013/Test_index.html", writeWebGL(dir=file.path(tempdir(), "D:/Summer_2019/NRS_Project/01_data/01_las_2013"),
width=500), sep="")
)
I have an external function to make a series of plots:
This functions reads an object which is a raster sum made in advance, and then it plots this raster and finally on top of it, it draws a vector of a shapefile.
Since I have to do hundreds of these plots I wanted to parallelize the code. For this I used mclapply.
What happens is that when I use my desktop (Ubuntu LTS with a standard i7-6400 cpu) everything works fine and I get both the raster and the shapefile plotted with the parallel apply command.
When I run the code on my server (Debian 9, Xeon Silver 4108) and on a HPC I get the following problem:
the raster objects, which are in the memory, are plotted fine, while the vector object is not plotted and I get this error of "error packet 1. object not found".
Note that on the server/HPC this does not occur if I ran the code serially (with a serial loop, instead of mclapply).
Note that the raster is an object and it's plotted, while the vector is not plotted. I tried several workarounds:
- using get to get the object within the plot function
- read each time the vector shapefile inside the plot function
- hardcode in the plot function the name of the shapefile object
- export the object on the slaves
None of them work on my server and HPC service...
My feeling is that it is a problem of Lattice / LatticeExtra, which unfortunately are not that much maintained anymore after the arrival of ggplot
Any help is appreciated. I am enclosing the code at the bottom, although it is not a full MWE...
conc.plot <- function(i, main.list.con.file, path, dupl.sources = FALSE, tm.series = tm, bldng.shp = "buildings.vector", color.scale.type = "macc"){
library(raster)
library(rasterVis)
library(grid)
library(lattice)
library(sp)
library(latticeExtra)
library(rgdal)
## i is the element to be plotted in the main.list.con.file
## main.list.con.file is the vector with ALL (including duplicates) con. files
## path is the path to the folder of the main.list.con
## tm is the vector of simulation timesteps as read in the mettimeseries
conc.field <- get(paste0("sum.rast.",i))
## Define conc. limits
conc.field#data#values[conc.field#data#values < 1] <- 0
conc.field[conc.field > 210] <- 210
## arbitrary units
if(color.scale.type == "arbitrary"){
scale.tick <- seq(1,211,2)
scale.label <- c("very low", "low", "medium", "high", "very high")
scale.label.at <- c(10,40,80,150,200)
scale.col <- colorRampPalette(rev(c('#a50026','#d73027','#f46d43','#fdae61','#fee090','#ffffbf','#e0f3f8','#abd9e9','#74add1','#4575b4','#313695','#a1d99b'))) ##colorRampPalette(c("lightyellow","yellow","orange","red","darkred"))
}
time.step <- as.integer(sub(".*\\b(\\d{5})\\b.*", "\\1", main.list.con.file[i]))
## start plotting
png(filename = paste(path, "conc_map_lev_",sprintf("%04d",time.step), ".png", sep=""), width = 300*7, height = 300*5, res=300, pointsize = 12, type="cairo")
print(rasterVis::levelplot(conc.field, margin=FALSE, maxpixel=1e12,
main = format(tm.series$date[time.step],"%B %d, %H:%M %Z", tz="Europe/Rome"), ## FOR CESGA ONLY THERE IS NO HANDLING OF TIMEZONES
col.regions = scale.col, ## color vector to be used if regions is TRUE. The general idea is that this should be a color vector of moderately large length. This vector would be gradually varying in color
at = scale.tick, ## A numeric vector giving breakpoints along the range of z
colorkey = list(at = scale.tick, ## numeric vector specifying where the colors change. must be of length 1 more than the col vector
labels = list(at = scale.label.at, labels = scale.label), ## a character vector for labelling the "at" values, or a list including components "labels", "at", "cex", "col", "rot", "font", "fontface", "fontfamily".
col = scale.col), ## A color ramp specification, as in the col.regions argument in level.colors
xlab = "UTM Westing Coordinate (m)",
ylab = "UTM Northing Coordinate (m)",
scales = list(x = x.scale, y = y.scale),
panel = function(...){
panel.levelplot(...)
panel.abline(v = unlist(x.scale)[2:5] ,h = unlist(y.scale)[2:5], col = 1, lty = 2, lwd=.9)}
) + latticeExtra::layer(sp.polygons(get(bldng.shp), fill='grey', alpha=0.6, lwd=.1))
)
trellis.focus("legend", side="right", clipp.off=FALSE, highlight=FALSE)
dev.off()
message(paste0("Saved concentration map for time step ", time.step,", i.e. ",format(tm.series$date[time.step],"%B %d, %H:%M", tz="Europe/Rome")))
}
mc <- round(parallel::detectCores() * 0.5) + 1
clusterExport(makeCluster(mc), varlist = c("buildings.vector"))
list.conc <- which(dupl.sg)
parallel::mclapply(list.conc, function(i) conc.plot(i, main.list.con.file = list.conc, path = conc.file.path, bldng.shp = "buildings.vector", color.scale.type = "arbitrary"), mc.cores = mc, mc.preschedule = FALSE)
I am a newbie for R and have got stuck here. I am trying to draw a graph with price, sma and ema.
When I call the graph from the command line it draws fine including price, sma and ema:
tickers = c("BIIB","ISRG","AIG","FITB","GE","JNY","VIAB","WFM","WMB")
x= 1
print(paste("Preparing ADX graph for :",paste(tickers[x])))
tmp <- read.csv(paste(tickers[x],".csv", sep=""),as.is=TRUE, header=TRUE, row.names=NULL)
tmp$Date<-as.Date(tmp$Date)
ydat = xts(tmp[,-1],tmp$Date)
names(ydat) <- c("Open","High","Low","Close","Volume","Adjusted")
# convert it into montly price
ydat.monthly <- to.monthly(ydat)
jpegname <- paste(tickers[x], "MonthlyMovingAverage.jpeg", sep="")
jpeg( filename=jpegname,height=600, width=1600)
lineChart(ydat.monthly["1998/"], TA=NULL, name=paste(tickers[x],"Monthly & 10 Month Moving Average"))
addSMA(10)
addEMA(10)
dev.off()
But put into function as:
MovingMonthlyAverageGraph <- function(tickers)
{
source("code.r")
load.packages('quantmod')
for (x in 1:(length(tickers)) )
{
print(paste("Preparing ADX graph for :",paste(tickers[x])))
tmp <- read.csv(paste(tickers[x],".csv", sep=""),as.is=TRUE, header=TRUE, row.names=NULL)
tmp$Date<-as.Date(tmp$Date)
ydat = xts(tmp[,-1],tmp$Date)
names(ydat) <- c("Open","High","Low","Close","Volume","Adjusted")
# convert it into montly price
ydat.monthly <- to.monthly(ydat)
jpegname <- paste(tickers[x], "MonthlyMovingAverage.jpeg", sep="")
jpeg( filename=jpegname,height=600, width=1600)
lineChart(ydat.monthly["1998/"], TA=NULL, name=paste(tickers[x],"Monthly & 10 Month Moving Average"))
addSMA(10)
addEMA(10)
dev.off()
}
}
and called as:
tickers = c("BIIB","ISRG","AIG","FITB","GE","JNY","VIAB","WFM","WMB")
MovingMonthlyAverageGraph(tickers)
only draws the price, but ignores the sma and ema lines.
What am I doing wrong here?
wrap plot around your add* calls.
plot(addSMA(10))
plot(addEMA(10))
I think you could also just add these in the lineChart call instead. (untested)
lineChart(ydat.monthly["1998/"], TA="addSMA(10);addEMA(10)", name=paste(tickers[x],"Monthly & 10 Month Moving Average"))
I am generating plot in R and save it as PDF with:
pdf(
file='Plots/errors.pdf',
height=4,
width=7,
onefile=TRUE,
family='Helvetica',
pointsize=12
)
# Here is my graphics
dev.off()
Somewhere inside graphics I have:
mtext(
expression(mu[H1])
)
It produces neat PDF with correctly processed greek letter µ.
Then I import this PDF in LaTeX article with:
\includegraphics[width=1\textwidth,height=0.4\textheight]{../Plots/errors.pdf}
But instead of µ sign of infinity (∞) is displayed.
Why?
For seamless integration without the encoding issues, I would look at package 'TikzDevice'. It outputs Tikz images in LaTeX format. For example:
require(tikzDevice)
setwd("/Path/To/LaTeX/Files/")
#Names of LaTeX symbols
syms <- c('alpha', 'theta', 'tau', 'beta', 'vartheta', 'pi', 'upsilon', 'gamma', 'varpi', 'phi', 'delta', 'kappa', 'rho', 'varphi', 'epsilon', 'lambda', 'varrho', 'chi', 'varepsilon', 'mu', 'sigma', 'psi', 'zeta', 'nu', 'varsigma', 'omega', 'eta', 'xi', 'Gamma', 'Lambda', 'Sigma', 'Psi', 'Delta', 'Xi', 'Upsilon', 'Omega', 'Theta', 'Pi', 'Phi')
len <- length(syms)
# random colors (red, green, blue)
r <- round(runif(len), 2)
g <- round(runif(len), 2)
b <- round(runif(len), 2)
# calculate dummy data points
x <- runif(50,1,10)
y <- x + rnorm(length(x))
fit <- lm(y ~ x)
rsq <- summary(fit)$r.squared
rsq <- signif(rsq,4)
# plot the result, will create symbol-regression.tex in the working
# directory the first time this is run it may take a long time because the
# process of calulating string widths for proper placement is
# computationally intensive, the results will get cached for the current R
# session or will get permenantly cached if you set
# options( tikzMetricsDictionary='/path/to/dictionary' ) which will be
# created if it does not exist. Also if the flag standAlone is not set to
# TRUE then a file is created which can be included with \include{}
tikz('symbolr.tex', width = 4,height = 4,pointsize = 12)
# The syntax is mar=c(bottom, left, top, right).
par(mar=c(2,2,2,2))
# plot the box and the regression line
plot(x, y, type='n', xlab='', ylab='')
box()
abline(fit)
# add the latex symbols as points
text(x, y, paste('\\color[rgb]{',r,',',g,',',b,'}{$\\',syms,'$}',sep=''))
# Display the correlation coefficient
mtext(paste("Linear model: $R^{2}=",rsq,"$" ),line=0.5)
# and the equation of the line
legend('bottom', legend = paste("$y = ", round(coef(fit)[2],3),'x +', round(coef(fit)[1],3), '$', sep=''), bty= 'n')
# Close the device
dev.off()
Then all you have to do is include the file just output from R in your LaTeX document.