Reverse legend in raster - r

While plotting a raster image, for example:
library(raster)
r <- raster(nrow = 3, ncol = 3)
values(r) <- 1:9
plot(r, col = terrain.colors(255))
How can I get the legend being in ascending order, i.e. from 1 (top) to 9 (bottom)?
I thought of the legend.args, but couldn't find the right arguments.

I tried a bit and I think I've found a solution myself, even though it is not the most elegant way.
library(raster)
r <- raster(nrow = 3, ncol = 3)
values(r) <- 1:9
par(mar = c(3, 3, 4, 3))
plot(r, col = terrain.colors(255),legend = FALSE, xlim = c(-200,200),
ylim = c(-200,200))
vz = matrix(1:100, nrow = 1)
vx = c(185, 195)
vy = seq(-10, 10, length.out = 100)
par(new = TRUE, mar = c(3, 3, 4, 3))
plot(1, xlab = "", ylab = "", axes = FALSE, type = "n",
xlim = c(-200, 180), ylim = c(-20, 20))
image(vx, vy, vz, col = rev(terrain.colors(255)), axes = FALSE,
xlab = "", ylab = "", add = TRUE)
polygon(c(185, 195, 195, 185), c(-10, -10, 10, 10))
axis(4, at = seq(-10, 10, length.out = 9), labels = 9:1, las = 1)
Anyway, I'd appreciate other ideas!

Related

Rotate plot 90deg for marginal distribution

I am trying to get marginal distribution plots in base R (if not possible, non-base R code is okay, but only for doing the rotating).
How do you rotate the y-axis marginal plot (in yellow)?
The code I have to make this plot:
# plot layout
layout_mat <- matrix(c(2, 0,
1, 3),
nrow = 2,
byrow = T)
layout(layout_mat, c(3, 1), c(1, 3))
par(mar = c(3,3,1,1))
# main scatterplot
plot(x = mtcars[order(mtcars$qsec), c("wt", "mpg")],
xlab = "Vehicle Weight (1000 lbs)",
ylab = "Miles Per Gallon (MPG)",
pch = 16,
cex = seq(3.5, 1.25, length.out = nrow(mtcars)),
col = rgb(0, 0, 0, .5),
axes = F,
xlim = c(1, 6),
ylim = c(8, 35))
box(lwd = 1.5)
axis(side = 1, at = 1:6, labels = 1:6, lwd = 0, lwd.ticks = 1)
axis(side = 2, at = seq(10, 35, 5), label = seq(10, 35, 5), lwd = 0, lwd.ticks = 1, las = 1)
dx <- density(mtcars[order(mtcars$qsec), "wt"])
dy <- density(mtcars[order(mtcars$qsec), "mpg"])
# x axis plot
par(mar = c(0,3,1,1))
plot(dx, axes = F, main = "", xlab= "", ylab = "", lwd =2)
# y-axis plot
par(mar = c(3,0,1,1))
plot(dy, axes = F, main = "", xlab= "", ylab = "", lwd =2)
rect(xleft = par("usr")[1],
ybottom = par("usr")[3],
xright = par("usr")[2],
ytop = par("usr")[4],
col = rgb(235, 216, 52, .5*255, maxColorValue = 255))
Thanks to #user20650 for this answer
Passing the y-axis points for y's density to x and the x-axis points for y's density to y will plot it flipped:
plot(dy$y, dy$x, type="l", axes = F, main = "", xlab= "", ylab = "", lwd = 2)

Histogram how to change y-axis from counts to frequency and standardize across two data sets

I have two data sets showing the lengths of fish and would like to create side by side histogram plots to compare the data. The issue I'm having is scaling the y-axis and bin sizes so that they are comparable. Instead of counts, I wanted to use %frequency of the data.
I'm also having issues with plotting them side by side when they're coming from two different sources. Can you use the facet_grid or facet_wrap to do this?
Any help would be much appreciated!
EDIT
I used this code which just gives a basic histogram with the counts..
ggplot(snook, aes(sl)) +geom_histogram(binwidth = 20, color="black", fill= "light blue")+
ggtitle("All Snook")+
labs(x="Standard Length(mm)", y="Counts")+
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
Below are the results from using the code offered by SimeonL below
opar <- par(mfrow = c(1,2))
hist(snook$sl, breaks = seq(0, 1000, length = 50), freq = T, main = "All Snook", xlab = "Length (mm)", ylim = c(0, 50), las = 1)
hist(gut_Snook$SL, breaks = seq(0, 1000, length = 50), freq = T, main = "Culled Snook", xlab = "Length (mm)", ylim = c(0, 50), las = 1)
par(opar)
This is close, however it looks like it's still using the counts for the y-axis rather than % frequency.
Two options in base R:
using hist and change y-axis labels to match percentage:
set.seed(23)
df1 <- data.frame(f_size = rnorm(120, 20, 15))
x.1 <- approxfun(c(0, 100), c(0, nrow(df1)))
df2 <- data.frame(f_size = rnorm(70, 5, 5))
x.2 <- approxfun(c(0, 100), c(0, nrow(df2)))
opar <- par(mfrow = c(1,2))
hist(df1$f_size, breaks = seq(-20, 70, length = 40), freq = T, main = "", xlab = "df1_size",
ylim = x.1(c(0, 25)), las = 1, yaxt = "n", ylab = "% Cases")
axis(2, at = x.1(seq(0, 25, 5)), labels = seq(0, 25, 5), las = 1)
hist(df2$f_size, breaks = seq(-20, 70, length = 40), freq = T, main = "", xlab = "df2_size",
ylim = x.2(c(0, 25)), las = 1, yaxt = "n", ylab = "")
axis(2, at = x.2(seq(0, 25, 5)), labels = seq(0, 25, 5), las = 1)
par(opar)
Calculate percentage first and use barplot:
breaks <- seq(-20, 70, length = 40)
df1.perc <- aggregate(df1$f_size, by = list(cut(df1$f_size, breaks, labels = F)), FUN = function(x) (length(x)/nrow(df1))*100)
df2.perc <- aggregate(df2$f_size, by = list(cut(df2$f_size, breaks, labels = F)), FUN = function(x) (length(x)/nrow(df2))*100)
opar <- par(mfrow = c(1,2))
bp <- barplot(height = merge(data.frame(Group.1 = 1:length(breaks)), df1.perc, all.x = T)$x,
xlab = "df1_size", ylab = "% Cases", ylim = c(0, 25), las = 1)
axis(1, at = approx(breaks, bp, xout = seq(-40, 70, by = 10))$y, labels = seq(-40, 70, by = 10))
bp <- barplot(height = merge(data.frame(Group.1 = 1:length(breaks)), df2.perc, all.x = T)$x,
xlab = "df1_size", ylab = "", ylim = c(0, 25), las = 1)
axis(1, at = approx(breaks, bp, xout = seq(-40, 70, by = 10))$y, labels = seq(-40, 70, by = 10))

How to draw a level line in a filled contour plot and label it?

I have the following contour plot
x <- c(0,25,50,75,100)
y <- c(0,10,20)
z <- matrix(c(12,12,13,12,5,12,5,5,5,12,5,12,13,14,15), nrow = 5, ncol = 3, byrow = TRUE)
A <- matrix(seq(0, 100, by = 25), nrow = 3, ncol = 5, byrow = TRUE) #As x
B <- matrix(seq(0,20, by = 10), nrow = 3, ncol = 5) #As y
filled.contour(x,y,z, color=terrain.colors,#
plot.axes = { axis(1); axis(2); points(A,B)})
How can I draw a level line around the level with value 5 and label it and obtain something like:
You may use contour in plot.axes. It's not possible to add the line at exactly 5, so I used 5.01 instead and specified labels. This is at least the principle.
filled.contour(x, y, z, color = terrain.colors,
plot.axes = {axis(1); axis(2); points(A, B);
contour(x, y, z, levels = 5.01, labels = "5", col = "red", add = TRUE)})
library(fields)
library(emdbook)
x <- c(0,25,50,75,100)
y <- c(0,10,20)
z <- matrix(c(12,12,13,12,5,12,5,5,5,12,5,12,13,14,15), nrow = 5, ncol = 3, byrow = TRUE)
A <- matrix(seq(0, 100, by = 25), nrow = 3, ncol = 5, byrow = TRUE) #As x
B <- matrix(seq(0,20, by = 10), nrow = 3, ncol = 5) #As y
image.plot(x,y,z)
contour(x,y,z,
add=TRUE, lwd=2, cex=2)

How to make the trend-line in a scatter plot respect the boundaries of the x-axis?

I am creating a plot where I plot the variable on the X-axis against that on the Y-axis, and I am adding histograms of the variables as well. I have added a trend-line to the plot using abline().
The problem is that it does not appear to respect the xlim = c(0, 20) in the plot region as it extends beyond the limits of the x-axis. I tried playing around with the xpd option, but to no avail. Next I tried fiddling with the different par()options, but found nothing that could help with this issue.
What I want is for the trend-line to be the exact length of the x-axis. Any help is much appreciated. In this particular case the trend-line is almost flat, but the slope will change when I do the same for other variables.
MWE -- NOTE: I am only providing 15 data points to illustrate the issue so the graph will differ from the image provided.
df.data <- data.frame(id = 1:15,
ll = c(-9.53026, -6.50640,-6.50640, -7.68535, -11.80899, -8.42790,
-6.50640, -6.50640, -7.92405, -6.50640, -8.95522, -9.99228,
-10.02286, -8.95969, -6.07313),
aspm = c(4.582104, 0.490244, 0.737765, 0.256699, 1.575931, 1.062693,
1.006984, 0.590355, 1.014370, 0.924855, 0.735989, 0.831025,
1.197886, 1.143220, 0.928068))
str.col.light.blue <- c(rgb(r = 110/255, g = 155/255, b = 225/255))
str.col.dark.blue <- c(rgb(r = 50/255, g = 100/255, b = 185/255))
layout(matrix(c(2, 4, 1, 3), 2, 2, byrow = TRUE), widths = c(5, 2), heights = c(2, 5))
layout.show(4)
par(omi = c(0.1, 0.1, 0.1, 0.1))
par(mar = c(2, 2, 0, 0))
par(mai = c(1, 1, 0, 0))
plot(df.data[, "ll"] ~ df.data[, "aspm"], col = str.col.light.blue,
xlim = c(0, 20), ylim = c(-15, -5), axes = FALSE,
xlab = "X1", ylab = "X2",
cex.lab = 1.25)
abline(a = -8.156670, b = -0.000879, lty = 5, col = "black", lwd = 2, xpd = FALSE)
axis(1, at = seq(0, 20, by = 5), labels = seq(0, 20, by = 5), cex.axis = 1)
axis(2, at = seq(-15, -5, by = 3), labels = seq(-15, -5, by = 3), cex.axis = 1, las = 1)
rect(0, -15, 20, log(1/3)*8, density = 10, angle = 45, lwd = 0.5, col = "gray")
par(mar = c(0, 2, 0, 0))
par(mai = c(0, 1, 0.25, 0))
x.hist <- hist(df.data[, "aspm"], plot = FALSE, breaks = 20)
barplot(x.hist$density, axes = FALSE, horiz = FALSE, space = 0, col = str.col.dark.blue)
par(mar = c(2, 0, 0, 0))
par(mai = c(1, 0, 0, 0.25))
y.hist <- hist(df.data[, "ll"], plot = FALSE, breaks = 20)
barplot(y.hist$density, axes = FALSE, horiz = TRUE, space = 0, col = str.col.dark.blue)
In order to avoid working out the start and end points of the segments, you can program a helper function to do it for you.
linear <- function(x, a, b) a + b*x
Then, I've used your code with the following changes. abline was replaced by segments, with all the graphics parameters you had used in your original call.
x0 <- 0
y0 <- linear(x0, a = -8.156670, b = -0.000879)
x1 <- 20
y1 <- linear(x1, a = -8.156670, b = -0.000879)
segments(x0, y0, x1, y1, lty = 5, col = "black", lwd = 2, xpd = FALSE)
This call to segment was placed where ablinewas.
In the final graph, I see a well behaved segment.

R: Suppress ticks and labels at endpoints of axis

I have code producing the below pasted plot
x <- c(2, 3, 4)
y <- c(2.5, 4.1, 5.5)
plot(x, y, type = "o", xlim = c(1, 5), ylim = c(2, 6), axes = FALSE, bty = "n")
axis(side = 1, at = seq(1, 5, 1))
axis(side = 2, at = seq(2, 6, 1), las = 2)
I would like to have neither ticks nor labels at position 1 and 5, but the axis should still be drawn. This is what I am looking for:
When using labels = c("", 2, 3, 4, "") ticks are drawn. When using tick = FALSE, I get no axis. Does anybody have a solution for this?
You just need to draw the line manually. Using the line2user function in this answer:
x <- c(2, 3, 4)
y <- c(2.5, 4.1, 5.5)
plot(x, y, type = "o", xlim = c(1, 5), ylim = c(2, 6), axes = FALSE, bty = "n")
axis(side = 1, at = 2:4)
lines(x = c(1, 5), y = rep(line2user(0, side = 1), 2), xpd = TRUE)
axis(side = 2, at = seq(2, 6, 1), las = 2)
Note, the line2user function just gives the position of the lines in the user coordinate system. You need the xpd = TRUE to draw outside the plotting region.

Resources