Changing the fontsize of axis annotations in a hexbinplot - r

I would like to increase the font size of axis annotations in a hexbinplot.
library(hexbin)
df <- data.frame(x=rnorm(1000),y=rnorm(1000))
hb <- hexbin(x=df$x, df$y)
myPlot <- plot(hb, xlab="", ylab="", legend=FALSE)
I would like the -3, ..., 2 and the -2, ..., 3 on the axes to be larger.
This earlier thread already helped me with axis labels, but the suggestion about how to change the annotations ("use grid.ls()" - how?) is a little too cryptic for me. I am more fluent in base graphics than in lattice.

Try this.
library(grid)
myPlot <- plot(hb, xlab="", ylab="", legend=FALSE)
grid.ls()
# GRID.rect.250
# GRID.xaxis.251
# GRID.yaxis.252
# GRID.polygon.253
grid.edit("GRID.xaxis.251", gp=gpar(fontsize=20))
grid.edit("GRID.yaxis.252", gp=gpar(fontsize=20))
The grid.ls() function shows the parts of the graph. The axis labels are GRID.xaxis.251 and GRID.yaxis.252. The name labels should be the same but the numbers will be different so you will have to modify the grid.edit() lines to match the output from grid.ls().

Related

Get 2 plots and a vertical legend next to each other

I want to have two plots next to each other and a common legend for both.
Like This:
Plot 1 | Plot 2 | Legend
Unfortunately, the legend is plotted inside of the second plot...
This is my current approach:
par(mfrow=c(1,3))
plot(...)
plot.new()
plot(...)
legend("center", ...)
I thought with par(mfrow=c(1,3)) I would get one row with 3 columns of plots - so exactely the result I wanted to obtain.
Is the legend maybe not treated as a plot but as belonging to plot number 2 and so it is plotted with the same column?
You were close. legend needs a plot.new in front of it, if you want to have it as a "standalone" plot.
par(mfrow=c(1, 3))
plot(1:10)
plot(1:10)
plot.new()
legend("center", pch=1, legend=c("x", "y"))
Result
I hope this works for you as an example. Nevertheless, there are better libraries to be used such as ggplot2 or plotly.
par(mfrow = c(1, 2), oma = c(0, 0, 0, 2))
plot(hp~mpg, data=mtcars, col=cyl,pch=19)
plot(disp~wt, data=mtcars, col=cyl,pch=19)
legend(x=6, y=250, legend=as.numeric(levels(factor(mtcars$cyl))), pch=19, col= as.numeric(levels(factor(mtcars$cyl))) )

Barplot with Categorical X Axis

I am trying to generate a bar plot with a categorical X axis and two different y axis. I am trying to use twoord.plot to generate the bar plot as follows:
x <- c("A","B","C","D","E")
ry <- c(0.1,0.2,0.3,0.4,0.5)
ly <- c(0.15,0.25,0.35,0.45,0.55)
library(plotrix)
twoord.plot(x,ry,x,ly,
xlab="xLabel",
ylab="yLabel",
rylab="ryLabel",
main="Main",
type=c("bar","l"),lcol=rainbow(length(x)),rcol=4)
However, I am getting an error "Error in plot.window(...) : invalid 'xlim' value".
Is there a way to work with categorical/character variables as x-axis? Also, is there a way to rotate the X-axis labels so that they show up at 45 degrees?
I have been able to get this code to work with the following changes:
xNumeric <- seq(1:length(x))
twoord.plot(xNumeric,ly,xNumeric,ry,
xlab="xLabel",
ylab="yLabel",
rylab="ryLabel",
main="Main",
type=c("bar","o"),lcol=rainbow(length(x)),rcol = 4,xticklab = x)
However, I still need to figure out how to rotate the X-axis labels as well as adding a legend to differentiate between which is the box plot and which is the line plot. Any help on this would be appreciated
Thank you.
This isn't in plotrix, but...
ry <- c(0.1,0.2,0.3,0.4,0.5)
ly <- c(15,35,65,75,80)
x <- 1:5
xlabs <- c("A","B","C","D","E")
barplot(ly, xaxt="n", yaxt="n", xlab="xLabel", ylab="lyLabel", ylim=c(0,100))
axis(2, seq(0,100,by=5), seq(0,100,by=5), las=2) # you can adjust positions of ly labels
par(new=TRUE)
plot(ry~x, xaxt="n", yaxt="n", xlab="", ylab="", ylim=c(0,1))
axis(1, 1:5, xlabs)
axis(4, 1:10/10, 1:10/10, las=2) # you can adjust positions of ry labels
mtext("ryLabel", 4, line=2)
And you would obviously need to edit a bit to get the colors, etc. that you seem to be going for.

How to plot the value of abline in R?

I used this code to make this plot:
plot(p, cv2,col=rgb(0,100,0,50,maxColorValue=255),pch=16,
panel.last=abline(h=67,v=1.89, lty=1,lwd=3))
My plot looks like this:
1.) How can I plot the value of the ablines in a simple plot?
2.) How can I scale my plot so that both lines appear in the middle?
to change scale of plot so lines are in the middle change the axes i.e.
x<-1:10
y<-1:10
plot(x,y)
abline(a=1,b=0,v=1)
changed to:
x<-1:10
y<-1:10
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)
by "value" I am assuming you mean where the line cuts the x-axis? Something like text? i.e.:
text((0), min(y), "number", pos=2)
if you want the label on the x axis then try:
abline(a=1,b=0,v=1)
axis(1, at=1,labels=1)
to prevent overlap between labels you could remove the zero i.e.:
plot(x,y,xlim=c(-30,30),yaxt="n")
axis(2, at=c(1.77,5,10,15,20,25))
or before you plot extend the margins and add the labels further from the axis
par(mar = c(6.5, 6.5, 6.5, 6.5))
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)
axis(2, at=1.77,labels=1.77,mgp = c(10, 2, 0))
Similar in spirit to the answer proposed by #user1317221, here is my suggestion
# generate some fake points
x <- rnorm(100)
y <- rnorm(100)
# positions of the lines
vert = 0.5
horiz = 1.3
To display the lines at the center of the plot, first compute the horizontal and vertical distances between the data points and the lines, then adjust the limits adequately.
# compute the limits, in order for the lines to be centered
# REM we add a small fraction (here 10%) to leave some empty space,
# available to plot the values inside the frame (useful for one the solutions, see below)
xlim = vert + c(-1.1, 1.1) * max(abs(x-vert))
ylim = horiz + c(-1.1, 1.1) * max(abs(y-horiz))
# do the main plotting
plot(x, y, xlim=xlim, ylim=ylim)
abline(h=horiz, v=vert)
Now, you could plot the 'values of the lines', either on the axes (the lineparameter allows you to control for possible overlapping):
mtext(c(vert, horiz), side=c(1,2))
or alternatively within the plotting frame:
text(x=vert, y=ylim[1], labels=vert, adj=c(1.1,1), col='blue')
text(x=xlim[1], y=horiz, labels=horiz, adj=c(0.9,-0.1), col='blue')
HTH

Get plot() bounding box values

I'm generating numerous plots with xlim and ylim values that I'm calculating on a per-plot basis. I want to put my legend outside the plot area (just above the box around the actual plot), but I can't figure out how to get the maximum y-value of the box around my plot area.
Is there a method for even doing this? I can move the legend where I want it by manually changing the legend() x and y values, but this takes a LONG time for the amount of graphs I'm creating.
Thanks!
-JM
Here's a basic example illustrating what I think you're looking for using one of the code examples from ?legend.
#Construct some data and start the plot
x <- 0:64/64
y <- sin(3*pi*x)
plot(x, y, type="l", col="blue")
points(x, y, pch=21, bg="white")
#Grab the plotting region dimensions
rng <- par("usr")
#Call your legend with plot = FALSE to get its dimensions
lg <- legend(rng[1],rng[2], "sin(c x)", pch=21,
pt.bg="white", lty=1, col = "blue",plot = FALSE)
#Once you have the dimensions in lg, use them to adjust
# the legend position
#Note the use of xpd = NA to allow plotting outside plotting region
legend(rng[1],rng[4] + lg$rect$h, "sin(c x)", pch=21,
pt.bg="white", lty=1, col = "blue",plot = TRUE, xpd = NA)
The command par('usr') will return the coordinates of the bounding box, but you can also use the grconvertX and grconvertY functions. A simple example:
plot(1:10)
par(xpd=NA)
legend(par('usr')[1], par('usr')[4], yjust=0, legend='anything', pch=1)
legend( grconvertX(1, from='npc'), grconvertY(1, from='npc'), yjust=0,
xjust=1, legend='something', lty=1)
The oma, omd, and omi arguments of par() control boundaries and margins of plots - they can be queried using par()$omd (etc). and set (if needed) using par(oma=c()) (where the vector can have up to 4 values - see ?par)

Remove plot axis values

I was just wondering if there is a way to get rid of axis values, either the x-axis or y-axis respectively, in an r-plot graph.
I know that axes = false will get rid of the entire axis, but I would only like to get rid of the numbering.
Remove numbering on x-axis or y-axis:
plot(1:10, xaxt='n')
plot(1:10, yaxt='n')
If you want to remove the labels as well:
plot(1:10, xaxt='n', ann=FALSE)
plot(1:10, yaxt='n', ann=FALSE)
Using base graphics, the standard way to do this is to use axes=FALSE, then create your own axes using Axis (or axis). For example,
x <- 1:20
y <- runif(20)
plot(x, y, axes=FALSE, frame.plot=TRUE)
Axis(side=1, labels=FALSE)
Axis(side=2, labels=FALSE)
The lattice equivalent is
library(lattice)
xyplot(y ~ x, scales=list(alternating=0))
#Richie Cotton has a pretty good answer above. I can only add that this page provides some examples. Try the following:
x <- 1:20
y <- runif(20)
plot(x,y,xaxt = "n")
axis(side = 1, at = x, labels = FALSE, tck = -0.01)
you can also put labels inside plot:
plot(spline(sub$day, sub$counts), type ='l', labels = FALSE)
you'll get a warning. i think this is because labels is actually a parameter that's being passed down to a subroutine that plot runs (axes?). the warning will pop up because it wasn't directly a parameter of the plot function.
Change the axis_colour to match the background and if you are modifying the background dynamically you will need to update the axis_colour simultaneously.
* The shared picture shows the graph/plot example using mock data ()
### Main Plotting Function ###
plotXY <- function(time, value){
### Plot Style Settings ###
### default bg is white, set it the same as the axis-colour
background <- "white"
### default col.axis is black, set it the same as the background to match
axis_colour <- "white"
plot_title <- "Graph it!"
xlabel <- "Time"
ylabel <- "Value"
label_colour <- "black"
label_scale <- 2
axis_scale <- 2
symbol_scale <- 2
title_scale <- 2
subtitle_scale <- 2
# point style 16 is a black dot
point <- 16
# p - points, l - line, b - both
plot_type <- "b"
plot(time, value, main=plot_title, cex=symbol_scale, cex.lab=label_scale, cex.axis=axis_scale, cex.main=title_scale, cex.sub=subtitle_scale, xlab=xlabel, ylab=ylabel, col.lab=label_colour, col.axis=axis_colour, bg=background, pch=point, type=plot_type)
}
plotXY(time, value)

Resources