Can't get axis labels to show on r plot() - r

I'm working with the meuse dataset in the sp library in R and I'm just trying to obtain a simple plot of the meuse grid which highlights the different areas of flooding frequency. However, I can't seem to get the axis labels to display. I've tried using a par() statement beforehand but it doesn't appear to be doing anything?
data(meuse.grid) #in sp library
summary(meuse.grid)
str(meuse.grid)
coordinates(meuse.grid) = ~x+y
proj4string(meuse.grid)<-CRS("+init=epsg:28992")
gridded(meuse.grid)=TRUE
class(meuse.grid)
par(mar=c(10,10,4,2)+0.1,mgp=c(5,1,0))
plot(meuse.grid["ffreq"], scale.frac = 0.6,main="Flooding Frequency Class Map",
xlab="Easting",ylab="Northing",axes=TRUE)
Any suggestions?

You could use mtext as a fix, expand slightly outer margins oma in advance. You could also fix the title with this method.
par(mar=c(10,10,4,2) + 0.1, mgp=c(5,1,0), oma=c(2, 2, 2, 2))
plot(meuse.grid["ffreq"], scale.frac = 0.6,main="",
xlab="",ylab="",axes=TRUE)
mtext("Easting", side=1, line=3, font=2)
mtext("Northing", side=2, line=3, font=2)
mtext("Flooding Frequency Class Map", side=3, line=1, font=2, cex=1.2)

Try reducing the plot margins by setting par() before your plot() function. The default values are:
par(mar = c(5, 4, 4, 2) + 0.1)
where each number represents a side of the plot (bottom, left, top, right). setting the outer margins via par(oma) (in a similar way to above) might also help.

Related

How to add squares to the back of my R plot? [duplicate]

Is there a command to easily add a grid onto an R plot?
The grid command seems to draw grid lines where-ever it feels like. I usually use abline to put lines exactly where I want them. For example,
abline(v=(seq(0,100,25)), col="lightgray", lty="dotted")
abline(h=(seq(0,100,25)), col="lightgray", lty="dotted")
Good luck!
See help(grid) which works with standard graphics -- short example:
R> set.seed(42)
R> plot(cumsum(rnorm(100)), type='l')
R> grid()
The ggplot2 package defaults to showing grids due to its 'Grammar of Graphics' philosophy. And lattice has a function panel.grid() you can use in custom panel functions.
By the way, there are search functions for help as e.g. help.search("something") and there is an entire package called sos to make R web searches more fruitful.
If you are not using a custom tick interval, you can control the grid and axes parameters directly from the plot() command:
plot(cumsum(rnorm(100)), type='l', panel.first=grid())
The plot.default() documentation provides more information about these parameters.
I agree with cbare.
Use abline to draw lines only where you really need.
Example from my last code:
abline(v=c(39448, 39814), col="grey40")
abline(h=c(-0.6, -0.4, -0.2, 0.2,0.4,0.6), col="grey10", lty="dotted")
remember that:
v is for vertical lines.
h for horizontal.
exploit the commands
lty for dotted line
color for light coloured line
in order to obtain "no heavy grid".
Another option is using the axis function for vertical and horizontal grid lines:
x <- rnorm(100)
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
# Only vertical grid
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Only horizontal grid
plot(x)
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
Created on 2022-08-20 with reprex v2.0.2
You can specify the position of the grid lines using the at argument.

Creating a reactive rectangular plot

I am relatively new to R. I am making an R Shiny app, and based on the input of the user, I would like to analyze the data and output a bar that shows the Jaccard index. This is what I want it to look like, although obviously a smooth gradient:
Please note that the Jaccard index (in this case, 0.35) will change after each input, so I'd like something reactive. I just have no idea where to start or if making plots like this is even possible in R.
Thanks.
edit: I used an online gradient generator to come up with this plot instead: how could I overlay a vertical line with its corresponding Jaccard index and corresponding location on the bar on this particular image?
edit: I want to remove the white space before the actual plot and after my text. any ideas?
With the plotrix package:
library(plotrix)
# get an empty box
plot(0:10, type="n", axes=FALSE, xlab=NA, ylab=NA)
# rectangle filled with a gradient
gradient.rect(0, 0, 10, 5, col=smoothColors("red",38,"blue"), border=NA)
# vertical bar
segments(3.5, 0, 3.5, 5, lwd=2)
text(3.5, 0, "0.35", pos=1, xpd=TRUE)
For something in base R, an imperfectly modified version of this solution, might work.
color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
scale = (length(lut)-1)/(max-min)
dev.new(width=1.75, height=5)
plot(c(min,max), c(0,10), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
for (i in 1:(length(lut)-3)) {
x = (i-1)/scale + min
rect(x, 0 ,1, 30/scale, col=lut[i], border=NA)
}
}
Then make the graph as follows -
color.bar(colorRampPalette(c("light green", "yellow", "orange", "red"))(100), 0, 1)
At this point perhaps you can add an abline(v = 0.35) to get what you want?
You could even try pointing to the appropriate position using an arrow using
arrows(0.35, -1, 0.35, 0, length = 0.07, angle = 25)

Is there an easy way to place grid in plot to background R [duplicate]

Is there a command to easily add a grid onto an R plot?
The grid command seems to draw grid lines where-ever it feels like. I usually use abline to put lines exactly where I want them. For example,
abline(v=(seq(0,100,25)), col="lightgray", lty="dotted")
abline(h=(seq(0,100,25)), col="lightgray", lty="dotted")
Good luck!
See help(grid) which works with standard graphics -- short example:
R> set.seed(42)
R> plot(cumsum(rnorm(100)), type='l')
R> grid()
The ggplot2 package defaults to showing grids due to its 'Grammar of Graphics' philosophy. And lattice has a function panel.grid() you can use in custom panel functions.
By the way, there are search functions for help as e.g. help.search("something") and there is an entire package called sos to make R web searches more fruitful.
If you are not using a custom tick interval, you can control the grid and axes parameters directly from the plot() command:
plot(cumsum(rnorm(100)), type='l', panel.first=grid())
The plot.default() documentation provides more information about these parameters.
I agree with cbare.
Use abline to draw lines only where you really need.
Example from my last code:
abline(v=c(39448, 39814), col="grey40")
abline(h=c(-0.6, -0.4, -0.2, 0.2,0.4,0.6), col="grey10", lty="dotted")
remember that:
v is for vertical lines.
h for horizontal.
exploit the commands
lty for dotted line
color for light coloured line
in order to obtain "no heavy grid".
Another option is using the axis function for vertical and horizontal grid lines:
x <- rnorm(100)
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
# Only vertical grid
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Only horizontal grid
plot(x)
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
Created on 2022-08-20 with reprex v2.0.2
You can specify the position of the grid lines using the at argument.

Aligning grid lines in R, bReeze package

I am trying to get grid lines work properly in the image below. Using the bReeze package to plot the power curves of the turbines with:
library(bReeze)
pc=pc("Vestas_V90_1.8MW.wtg")
plot(pc)
The output plot is:
but assigning grid lines to the plot with the help of:
grid()
gives the image below:
Any suggestions on how to fix the distorted grid lines?
If you don't give some arguments (e.g., mar, xlim, ylim),
plot(pc) uses par(mar = c(5, 5, 1, 5) and treats data.ranges as xlim and ylim. By using these properties, you can use grid().
pc.data = pc("Vestas_V90_1.8MW.wtg")
plot(pc.data)
par(mar = c(5, 5, 1, 5), new=T) # set par() and order to overlay
plot(pc.data[[1]], pc.data[[2]], type="n", ann=F, axes=F) # nothing but setting xy-cordinates
grid(NULL) # here, the same xy-coordinates are reproduced
# If you want to adjust grid lines to right y-axis, use berow code
:
par(mar = c(5, 5, 1, 5), new=T) # plot(pc) uses right ylim=c(0,1)
plot(pc.data[[1]], pc.data[[2]], ylim=c(0,1), type="n", ann=F, axes=F)
grid(NULL) # the xy(right)-coordinates are reproduced
# If you plot pc.object having single y-axis, use mar = c(5, 5, 1, 1)

Grid in an R plot

Is there a command to easily add a grid onto an R plot?
The grid command seems to draw grid lines where-ever it feels like. I usually use abline to put lines exactly where I want them. For example,
abline(v=(seq(0,100,25)), col="lightgray", lty="dotted")
abline(h=(seq(0,100,25)), col="lightgray", lty="dotted")
Good luck!
See help(grid) which works with standard graphics -- short example:
R> set.seed(42)
R> plot(cumsum(rnorm(100)), type='l')
R> grid()
The ggplot2 package defaults to showing grids due to its 'Grammar of Graphics' philosophy. And lattice has a function panel.grid() you can use in custom panel functions.
By the way, there are search functions for help as e.g. help.search("something") and there is an entire package called sos to make R web searches more fruitful.
If you are not using a custom tick interval, you can control the grid and axes parameters directly from the plot() command:
plot(cumsum(rnorm(100)), type='l', panel.first=grid())
The plot.default() documentation provides more information about these parameters.
I agree with cbare.
Use abline to draw lines only where you really need.
Example from my last code:
abline(v=c(39448, 39814), col="grey40")
abline(h=c(-0.6, -0.4, -0.2, 0.2,0.4,0.6), col="grey10", lty="dotted")
remember that:
v is for vertical lines.
h for horizontal.
exploit the commands
lty for dotted line
color for light coloured line
in order to obtain "no heavy grid".
Another option is using the axis function for vertical and horizontal grid lines:
x <- rnorm(100)
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
# Only vertical grid
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Only horizontal grid
plot(x)
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
Created on 2022-08-20 with reprex v2.0.2
You can specify the position of the grid lines using the at argument.

Resources