Two y-axis plot without overlapping - r

I am trying to make a graphic with to y-axis that the values of each y-axis start at the middle of the box so the 2 lines that I am plotting do not overlap. I have search for some time but have not found any way to do that with the basic X-Y graphics from R and i am adding some other things to the graphic so i don't want to do it with other package.
With the graphic in mind what I need it that the top one "merges" with the bottom and that the values on the left y-axis of the top graph change to the right but start at the middle, that way the lines wont overlap.

Do you mean something like this?
d1 <- data.frame(x=1:10, y=sin(1:10))
d2 <- data.frame(x=1:10, y=sin(1:10)+10)
plot(rbind(d1,d2), t="n", yaxt="n")
lines(d1)
lines(d2)
axis(2, at=pretty(d1$y))
axis(4, at=pretty(d2$y))

Related

R plot and barplot how to fix ylim not alike?

I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.

r - aligning x-axis image and base plot

When I try to align an image plot with a xy plot by their x-axis there is a small misalignment between the x-values. Can't figure out how to get rid of it!
Notice the misalignment of the vertical lines from the bottom plot with the x-axis on the top plot.
par(mfrow=c(2,1))
par(mar=c(0,5,5,5))
image(x=1:100,z=replicate(10, rnorm(100)))
par(mar=c(5,5,0,5))
par(xaxs="i")
plot(1:100,rnorm(100))
abline(v=1:100)
Gurus, help!! Thank you!!
PS: Could not figure it out with the post "plot-time-series-and-image-in-r-so-that-x-axis-labels-line-up-perfectly", still having issues!
The problem is that image draws data as a grid of cells which have a given width. If the length of x in image equals to nrow(z) then it specifies the midpoints of the cells. In your example this gives you rectangles centered around 1:100, effectively resulting in the x-axis covering the range from 0.5 to 100.5, which gives the observed misalignment.
In order to match the ranges in both plots you need to specify xlim to plot accordingly. In the following example I use n = 10 to make things more obvious.
par(mfrow=c(2,1))
par(mar=c(0,5,5,5))
image(x=1:n,z=replicate(10, rnorm(n)))
par(mar=c(5,5,0,5))
par(xaxs="i")
plot(1:n,rnorm(n), xlim=c(.5,n+.5))
abline(v=1:n)
Okay, so this is one of those things I had probably figured out on the past and then completely forgot about.
The trick is not only calling par(xaxs="i") but also enforcing both xlims!
par(mfrow=c(2,1))
par(mar=c(0,5,5,5))
image(x=1:100,z=replicate(10, rnorm(100)),xlim=c(0,100))
par(mar=c(5,5,0,5))
par(xaxs="i")
plot(1:100,rnorm(100),xlim=c(0,100))
abline(v=1:100)

R: Matching x-axis scales on upper and lower plot using layout with base graphics

I am trying to arrange 3 plots together. All 3 plots have the same y axis scale, but the third plot has a longer x axis than the other two. I would like to arrange the first two plots side by side in the first row and then place the third plot on the second row aligned to the right. Ideally I would like the third plot's x values to align with plot 2 for the full extent of plot 2 and then continue on below plot one. I have seen some other postings about using the layout function to reach this general configuration (Arrange plots in a layout which cannot be achieved by 'par(mfrow ='), but I haven't found anything on fine tuning the plots so that the scales match. Below is a crappy picture that should be able to get the general idea across.
I thought you could do this by using par("plt"), which returns the coordinates of the plot region as a fraction of the total figure region, to programmatically calculate how much horizontal space to allocate to the bottom plot. But even when using this method, manual adjustments are necessary. Here's what I've got for now.
First, set the plot margins to be a bit thinner than the default. Also, las=1 rotates the y-axis labels to be horizontal, and xaxs="i" (default is "r") sets automatic x-axis padding to zero. Instead, we'll set the amount of padding we want when we create the plots.
par(mar=c(3,3,0.5,0.5), las=1, xaxs="i")
Some fake data:
dat1=data.frame(x=seq(-5000,-2500,length=100), y=seq(-0.2,0.6,length=100))
dat2=data.frame(x=seq(-6000,-2500,length=100), y=seq(-0.2,0.6,length=100))
Create a layout matrix:
# Coordinates of plot region as a fraction of the total figure region
# Order c(x1, x2, y1, y2)
pdim = par("plt")
# Constant padding value for left and right ends of x-axis
pad = 0.04*diff(range(dat1$x))
# If total width of the two top plots is 2 units, then the width of the
# bottom right plot is:
p3w = diff(pdim[1:2]) * (diff(range(dat2$x)) + 2*pad)/(diff(range(dat1$x)) + 2*pad) +
2*(1-pdim[2]) + pdim[1]
# Create a layout matrix with 200 "slots"
n=200
# Adjustable parameter for fine tuning to get top and bottom plot lined up
nudge=2
# Number of slots needed for the bottom right plot
l = round(p3w/2 * n) - nudge
# Create layout matrix
layout(matrix(c(rep(1:2, each=0.5*n), rep(4:3,c(n - l, l))), nrow=2, byrow=TRUE))
Now create the graphs: The two calls to abline are just to show us whether the graphs' x-axes line up. If not, we'll change the nudge parameter and run the code again. Once we've got the layout we want, we can run all the code one final time without the calls to abline.
# Plot first two graphs
with(dat1, plot(x,y, xlim=range(dat1$x) + c(-pad,pad)))
with(dat1, plot(x,y, xlim=range(dat1$x) + c(-pad,pad)))
abline(v=-5000, xpd=TRUE, col="red")
# Lower right plot
plot(dat2, xaxt="n", xlim=range(dat2$x) + c(-pad,pad))
abline(v=-5000, xpd=TRUE, col="blue")
axis(1, at=seq(-6000,-2500,500))
Here's what we get with nudge=2. Note the plots are lined up, but this is also affected by the pixel size of the saved plot (for png files), and I adjusted the size to get the upper and lower plots exactly lined up.
I would have thought that casting all the quantities in ratios that are relative to the plot area (by using par("plt")) would have both ensured that the upper and lower plots lined up and that they would stay lined up regardless of the number of pixels in the final image. But I must be missing something about how base graphics work or perhaps I've messed up a calculation (or both). In any case, I hope this helps you get the plot layout you wanted.

Adding arrows to plotrix gap.plot() with y-axis breaks

I'm trying to add confidence intervals to a gap.plot made with the plotrix package when there is a y-axis break. Here is a MWE:
twogrp=c(rnorm(5)+4,rnorm(5)+20,rnorm(5)+5,rnorm(5)+22)
gap.plot(twogrp,gap=c(8,16),
xlab="X values",ylab="Y values",xlim=c(1,30),ylim=c(0,25),
main="Test two gap plot with the lot",xtics=seq(0,25,by=5),
ytics=c(4,6,18,20,22,24),
lty=c(rep(1,10),rep(2,10)),
pch=c(rep(2,10),rep(3,10)),
col=c(rep(2,10),rep(3,10)),
type="b")
arrows(2,18,2,24,length=0.05,angle=90,code=3)
This doesn't add the arrow, presumably because the upper plotting region has been closed.
This works though:
gap.plot(twogrp,gap=c(8,16),
xlab="X values",ylab="Y values",xlim=c(1,30),ylim=c(0,25),
main="Test two gap plot with the lot",xtics=seq(0,25,by=5),
ytics=c(4,6,18,20,22,24),
lty=c(rep(1,10),rep(2,10)),
pch=c(rep(2,10),rep(3,10)),
col=c(rep(2,10),rep(3,10)),
type="b")
arrows(2,4,2,6,length=0.05,angle=90,code=3)
Anybody know of a work around that doesn't involve using another package. I know how to do this in ggplot2, I just prefer to use plotrix.
You need to subtract the gap value from y value when you put arrows in the upper plot area.
arrows(2,18,2,24,length=0.05,angle=90,code=3)
should be as follows.
arrows(2,18 - 8,2,24 - 8,length=0.05,angle=90,code=3)

How to add a scale bar in R?

Lets say I want to have a plot and lose the box in R. But still I would need a scale bar so one can understand the scaling. I didn't find a solution.
plot(1,1, type="n", xlim=c(0,5), ylim=c(0,5))
When I use the scalebar function from the raster package, the scaling is not right:
require(raster)
scalebar(1)
The added scalebar is too short to represent 1 in the x axis.
I tried to find something else, but most scalebar functions are related to maps.
edit:
So what I want is something like this:
plot(1,1, type="n", xlim=c(0,5), ylim=c(0,5)
, yaxt="n",
xaxt="n", frame.plot=F, ann=F
# adding a blank plot without the axes
)
#adding some simple function
x=c(1:5)
y=x*x
lines(x=x, y=y)
#defining where the scale bar should appear
lines(x=c(4,5), y=c(5,5))
#placing the text right under the line
text(x=4.5, y=5, pos=1, label="1 km")
Is there an easier way to do something like this?
There might be a function that does what you want, but you can also create your own function that will hopefully serve well enough. See below for one possibility. You can of course adjust the function settings to get the positioning you want. In particular, I've included yadj as an argument to the function, with a default value of 1.5. You can change this if the scalebar label isn't positioned properly under the scale line.
If the x-axis spans a larger range than the values used below, you'll want to adjust the x-coordinates of the scale line so that it spans 10, 100, etc. x-units, as the case may be. If you want to get fancy, you can have the function itself determine how many x-units to span, based on the x-range of the plot and then use the magnitude of that span in the units label.
# Function to add a scalebar to a base-graphics plot
myScalebar = function(units_label, yadj=1.5) {
# Get plot coordinates
pc = par("usr")
# Position scale line between last two major x-axis tick marks
# and 1/10th of the total y-range above the lower y-axis coordinate
lines(c(floor(pc[2]-1),floor(pc[2])),
rep(pc[3] + 0.1*(pc[4] - pc[3]), 2))
# Place the units label at the midpoint of and just below the scale line
text(x=mean(c(floor(pc[2]-1), floor(pc[2]))),
y=pc[3] + 0.1*(pc[4] - pc[3]),
label=units_label, adj=c(0.5, yadj))
}
# Now redo your plot
# Start with blank plot
plot(1,1, type="n", xlim=c(0,5), ylim=c(0,5),
yaxt="n", xaxt="n", frame.plot=F, ann=F)
# Add a simple function
x=c(1:5)
y=x*x
lines(x=x, y=y)
# Add scalebar
myScalebar("1 km")
I usually use this sort of function that allows for lots of flexibility across plots. I have expanded the variables names to help with debugging. Please note: this is designed to work with raster converted to utms only (don't use geographic projections).
ScaleBar <- function(reference_raster_utm, round_to_nearest_km, width_percent, y_percent_from_bottom, x_percent_from_left, y_text_percent_from_bottom, ...) {
# Round by max to nearest... e.g. 5 km
mround <- function(x,base){
base*round(x/base)
}
# scale bar size adjustment to avoid decimals
scale_size <- ((xmax(reference_raster_utm)-xmin(reference_raster_utm))*width_percent)/1000
scale_size_adj <- mround(scale_size, round_to_nearest_km)
scale_size_adj_plot <- (scale_size_adj*1000)/2
# Horizontal percent position (x) for scale bar
x_position <- ((xmax(reference_raster_utm)-xmin(reference_raster_utm))*x_percent_from_left)+xmin(reference_raster_utm)
# Vertical percent position y for scale bar
y_position <- ((ymax(reference_raster_utm)-ymin(reference_raster_utm))*y_percent_from_bottom)+ymin(reference_raster_utm)
y_position_text <- ((ymax(reference_raster_utm)-ymin(reference_raster_utm))*y_text_percent_from_bottom)+ymin(reference_raster_utm)
# Draw line on plot
library(sp)
x_ends <- c((x_position-scale_size_adj_plot), (x_position+scale_size_adj_plot))
y_ends <- c((y_position), (y_position))
scale_bar_line <- SpatialLines(list(Lines(Line(cbind(x_ends, y_ends)), ID="length")))
projection(scale_bar_line) <- projection(reference_raster_utm)
plot(scale_bar_line, add=TRUE, ...)
text(x_position, y_position_text, paste0(scale_size_adj, "km"))
}
Arguments:
reference_raster_utm: One of your personal raster files to source extent/projection from.
round_to_nearest_km: round to nearest kilometre e.g. max out on 2km, 5km ect.
width_percent: percent of plot width that the scale bar should cover (e.g. big 50% small 10%).
y_percent_from_bottom: vertical position from bottom. 0% at bottom, 100% at top, 50% in the middle.
x_percent_from_left: horizontal position from left. 0% at left, 100% at right, 50% in the middle.
y_text_percent_from_bottom: same as y_percent_from_bottom but for text.
Example:
plot(my_raster)
ScaleBar(reference_raster_utm=my_raster, round_to_nearest_km=5, width_percent=0.25, y_percent_from_bottom=0.10, x_percent_from_left=0.50, y_text_percent_from_bottom=0.07, lwd=2)

Resources