spplot: put labels on the map - r

I seek help on how to change font size and font color for sp.text labels on the attached map—to avoid overlapping of labels and to improve readability.
Map was produced as below. If needed, one may download the SpatialPolygonsDataframe object 'mymap' here.
trellis.par.set() is not solving my problem. Am I using it incorrectly?
library(sp)
library(latticeExtra)
# Create list object for sp.layout (Got these functions here on stackoverflow, thank you owner)
sp.label <- function(x, label) {list("sp.text", coordinates(x), label)}
NUMB.sp.label <- function(x) {sp.label(x, as.vector(x#data$NUMB))}
make.NUMB.sp.label <- function(x) {do.call("list", NUMB.sp.label(x))}
# Spplot
tps <- list(fontsize=list(text=5), fontcolor=list(text="green"))
trellis.par.set(tps)
spplot(mymap, "indic",
col.regions=c("#D3D3D3","#A9A9A9"),
sp.layout = make.NUMB.sp.label(mymap),
cex = 0.5,
bg = "white", col="light grey", border="light grey")

Found the solution: Needed to add cex, col arguments in the list:
list("sp.text", coordinates(x), label, cex=0.5, col="green")

Related

R plot legend: Reduce space between legend columns

I am using vegan library to make some plots, with this code:
raremax <- min(colSums(mydata))
col <- palette()
lty <- c("solid", "dashed", "longdash", "dotdash")
pars <- expand.grid(col = col, lty = lty, stringsAsFactors = FALSE)
out <- with(pars[1:18, ], rarecurve(mydata, step = 100, sample = raremax,
cex =0.6, ylab="OTUs", label=F, col=col, lty=lty, lwd=2))
Then I add a legend using this code:
legend("bottomright", names(mydata), col=pars[1:18,1], lty= pars[1:18,2],
lwd=2, cex=0.5, xjust=1, ncol=2, x.intersp=0.5, y.intersp=0.5, bg="white")
The resulting graph looks like this:
I would like to reduce the space between legend columns, also reducing the size of the legend box, but I can't find a way to do that.
Anyone could provide me some help?
A combination of the legend() parameters "x.intersp" and "text.width" should be helpful.
Decreasing "x.intersp" (default value = 1, for me 0.25 looked good) should move your the legend labels closer to their respective points. Decreasing "text.width" (default value=NULL, for me 0.045 looked good) moves the columns closer together.

Adding Weighted Points to Multiple Maps Plot

How is it possible to generate the following plot using spplot or ggplot. I tried both ways, but unfortunately didn't prevail in any of them.
The porblem I am faced is connected with the appropiate adding points to the maps.
All necessary data (.shp,.prj,.dbf,.shx) you need to reproduce the problem are available here
My code is following:
library(sp)
library(maptools)
library(RColorBrewer)
germK <- readShapePoly("C:/Users/XYZ/Dropbox/R Skripts/PolygonG/vg250_krs.shp")
germK <- germK[germK#data$GF==4,]
germK#data$EWZ2 <- cut(germK#data$EWZ, breaks=quantile(germK#data$EWZ, 1:10/10))
germK#data$EWZ <- germK#data$EWZ2
mypalette <- rev(brewer.pal(9, "RdYlGn"))
pts1 <- list("sp.points", germK, pch = 21,lwd=2,cex=sample(1:412,412)/200, col = "black")
pts2 <- list("sp.points", germK, pch = 21,lwd=2,cex=sample(1:412,412)/200, col = "black")
p.layout <- list(pts1,pts2)
spplot(germK,zcol=c("EWZ","EWZ2"), sp.layout = p.layout, col.regions=mypalette)
The result I got by carrying out the upper code lines is:
The points from pts1 have to be plotted in the first map, and respectively the points from pts2 should be depictied on the second map. In my case you can see both points are present in both maps. Any idea how to settle the problem?
Recently, I have found a way for proper depicting points, but by this the titles disappear:
p1 <- spplot(germK,zcol="EWZ", sp.layout = pts1, col.regions=mypalette,
names.attr="X")
p2 <- spplot(germK,zcol="EWZ", sp.layout=pts2, col.regions=mypalette,
names.attr="Y")
p3 <- c(p1,p2, layout=c(2,1))
p3
After this part of code I am getting the desired look, which is not quite pretty but acceptable.
Any help will be highly appriciated!
I think you can make use of the which argument:
pts1 <- list("sp.points", germK, pch = 21,lwd=2,cex=sample(1:412,412)/200, col = "black", which = 1)
pts2 <- list("sp.points", germK, pch = 21,lwd=2,cex=sample(1:412,412)/200, col = "black", which = 2)
spplot(germK,zcol=c("EWZ","EWZ2"), sp.layout = list(pts1, pts2), col.regions=mypalette)

Dynamic background in plot() based on x-axis values?

I'm looking to implement EXACTLY this, but using plot() instead of ggplot2. There is even a reply on the blog by someone saying they did this with plot, but the link to their code is dead. I'm literally trying to do the exact same thing. I've downloaded data from FRED using quantmod's getSymbols(), and I have that data in a df, which I'm plotting with plot(). The x-axis are dates, and I want to change the background color based on a specific set of dates. Any ideas or hints how I can do this with plot?
Thank you!
Based on this question: R: change background color of plot for specific area only (based on x-values)
## plotting area with no axes
plot(unrate.df, type = "n")
lim <- par("usr")
## adding one rectangle
for (i in 1:nrow(recessions.trim)) {
rect(recessions.trim[i, 1], lim[3],
recessions.trim[i, 2], lim[4], border = "pink", col = "pink")
}
## adding the data
lines(unrate.df)
box()
You can use this:
plot(unrate.df, type="n")
makeRectangles(recessions.trim, col="pink", alpha=0.5)
lines(unrate.df)
grid()
where the function makeRectangles is defined as:
makeRectangles = function(x, col, alpha=1, border=NA, ...) {
col = col2rgb(col=col, alpha=FALSE)
col = rgb(red=col[1], green=col[2], blue=col[3],
alpha=floor(255*alpha) , maxColorValue=255)
rect(x[,1], par("usr")[3], x[,2], par("usr")[4], col=col, border=border, ...)
return(invisible())
}

Apply lines() to columns of a data frame/matrix; each line with a different color

I am trying to come up with a solution that doesn't involve using other packages such as ggplot. While plotting multiple lines is pretty straightforward, I haven't figured out a way to apply different values of an argument - e.g., different colors - to different lines. The code below (with the resulting plot) was my attempt, which obviously didn't do what I would like it to do. I also don't want to use a loop because I am trying to make my script as simple as possible.
df = cbind(sort(rnorm(10)), sort(rnorm(10,-2)), sort(rlnorm(10)))
plot(0, xlim = c(1,10), ylim=range(df), type="n")
apply(df, 2, lines, type="b", col = c("red", "blue", "black"))
What I really want is a plot like below:
plot(0, xlim = c(1,10), ylim=range(df), type="n")
color = c("red","blue","black")
for(i in 1:3){
lines(1:10, df[,i], type = "b", col=color[i])
}
Thank you in advance!
Try matplot():
df <- cbind(sort(rnorm(10)), sort(rnorm(10,-2)), sort(rlnorm(10)))
matplot(df, type="b", lty=1, pch=1, col=c("blue", "red", "black"))

how to create a plot with customized points in R?

I know I can create a plot with line and dots using the type = "o" argument in the plot command. I would like some more control over this -- I want to be able to draw the "o" as full dots, with black border and fill-in color of my choice, of customized size and of a different color than the line. Same for the line, I want to make it thicker, and of my choice of color. How would I go on about doing that?
What I found until now is just a plain
plot(y, type= "o")
which is too poor for my needs.
I am not interested in using ggplot, but instead use the internal plot library of R.
Any help appreciated.
All the information you need should be present in ?plot and ?points, as suggested by #BenBolker. In particular, you want to be using pch=21, and specifying background colour with the bg argument, size with cex, and line width with lwd.
If you want the line to be a different thickness to the point borders, you need to plot the line first, and then overlay the points.
For example:
y <- sample(10)
plot(y, lwd=6, type='l')
points(y, bg='tomato2', pch=21, cex=3, lwd=3) # tomato2 is a personal fave
You could also provide a vector of lwd, cex and col to the points call:
plot(y, lwd=6, type='l')
points(y, bg=rainbow(10), pch=21, cex=seq(1, by=0.2, length.out=10),
lwd=seq(2, by=1, length.out=10))
You could use layering (I don't work in base too much any more as a social researcher I love the facet_grid of ggplot, so there may be a better way) as in:
x <- sort(rnorm(25))
y <- sort(rnorm(25))
z <- as.factor(sample(LETTERS[1:5], 25, r=TRUE))
plot(x, y, pch = 19, cex = 1.3)
par(new = TRUE)
plot(x, y, pch = 19, cex = 1, col = z)
Which gives you:

Resources