Plot multiple frames of different size on the same window - r

Consider this example:
par(mfrow=c(2,3))
frame()
image(matrix(1:100, nrow=100), main="my wide plot", axes=FALSE)
frame()
plot(rnorm(120), rnorm(120), main="plot 1")
plot(dpois(0:20, lambda=6), type="b", main="plot 2")
x = rnorm(100)
y = x+runif(100, 10, 12)
plot(x=x, y=y, , main="plot 3")
How can I do to make my first graph (image(...) titled my wide plot) to occupy the 3 frames on top of the window?

A simple way would be to use layout():
layout(mat=matrix(c(1,1,1,2,3,4), ncol=3, byrow=TRUE))
image(matrix(1:100, nrow=100), main="my wide plot", axes=FALSE)
plot(rnorm(120), rnorm(120), main="plot 1")
plot(dpois(0:20, lambda=6), type="b", , main="plot 2")
x = rnorm(100)
y = x+runif(100, 10, 12)
plot(x=x, y=y, main="plot 3")
(For a nice example of a much more sophisticated layout, see here.)

Related

Create gradient color bar with ggplot2

I have the following code to create color bars, modified from here:
color.bar <- function(lut, title='') {
min <- -1
max <- -min
nticks <- 5
ticks <- seq(min, max, len=nticks)
scale <- length(lut)/(max-min)
pdf(NULL)
dev.control(displaylist="enable")
plot(c(min,max), c(10,0), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title, cex.main=3)
axis(1, ticks, las=1, labels=c('MIN','','','','MAX'), cex.axis=2)
for (i in 1:length(lut)) {
x = (i-1)/scale + min
rect(x, 0, x+1/scale, 10, col=lut[i], border=NA)
}
P <- recordPlot()
invisible(dev.off())
return(P)
}
myplot <- color.bar(colorRampPalette(c("light green", "yellow", "orange", "red"))(100), "Intensity")
myplot
Which produces the following:
Now what I would need would be to do the exact same in ggplot2, cause I want to add the result, along with a list of ggplots, to a pdf using grid.arrange.
I do not really know how to start... Anybody can help me get started to produce the same output using ggplot2?

Changing size of axis labels in hexbin plot

Is there a way to change the font size of axis labels when plotting hexbin data?
library(hexbin)
myData <- hexbin(rnorm(100), rnorm(100))
myPlot <- plot(myData, xlab = "Variable 1", ylab = "Variable 2")
You can suppress the labels and add them separately with grid commands. It may take some trial and error to position them exactly where you want them:
library(grid)
myPlot <- plot(myData, xlab="", ylab="", lcex=.75)
grid.text("Variable 1", .45, .1, gp=gpar(fontsize=12))
grid.text("Variable 2", .05, .5, rot=90, gp=gpar(fontsize=12))

R - how two have two y-axes with zeroes aligned in the middle of the plot

I am plotting two graphs on the same plot. Each one has a different ylim, and I would like to have the zeroes aligned in the middle of the plot.
This is my code:
# data
time <- seq(0.1, 10, by = 0.1)
det_rot <- runif(100, min=-100, max=100)
vel_mag <- runif(100, min=0, max=5)
# first plot
smoothingSpline = smooth.spline(time, det_rot, spar=0.20)
plot(time, det_rot,lwd=2,
ann=FALSE, las=2, pch="", ylim=c(-100,250)) # , pch=""
lines(smoothingSpline, lwd=2, col="red")
par(new=TRUE)
# second plot
smoothingSpline2 = smooth.spline(time, vel_mag, spar=0.20)
plot(time, vel_mag,
xaxt="n",yaxt="n",xlab="",ylab="",pch="", ylim=c(0,6))
lines(smoothingSpline2, lwd=2, col="blue",)
axis(4)
See the plot:
Simple fix: change ylims to c(-250, 250) and c(-6,6) respectively.

In R plotting line with different color above threshold limits

I have the following data and code in R:
x <- runif(1000, -9.99, 9.99)
mx <- mean(x)
stdevs_3 <- mx + c(-3, +3) * sd(x/5) # Statndard Deviation 3-sigma
And I plotted as line (alongwith 3 standard deviation and mean lines) in R:
plot(x, t="l", main="Plot of Data", ylab="X", xlab="")
abline(h=mx, col="red", lwd=2)
abline(h=stdevs_3, lwd=2, col="blue")
What I want to do:
Anywhere on the plot, whenever line is crossing 3 sigma thresholds (blue lines), above or below it, line should be in different color than black.
I tried this, but did not work:
plot(x, type="l", col= ifelse(x < stdevs_3[[1]],"red", "black"))
abline(h=mx, col="red", lwd=2)
abline(h=stdevs_3, lwd=2, col="blue")
Is there any other way?
This is what is requested, but it appears meaningless to me because of the arbitrary division of x by 5:
png( )
plot(NA, xlim=c(0,length(x)), ylim=range(x), main="Plot of Data", ylab="X", xlab="", )
stdevs_3 <- mx + c(-3, +3) * sd(x/5)
abline(h=mx, col="red", lwd=2)
abline(h=stdevs_3, lwd=2, col="blue")
segments( 0:999, head(x,-1), 1:1000, tail(x,-1) , col=c("black", "red")[
1+(abs(tail(x,-1)) > mx+3*sd(x/5))] )
dev.off()

Inserting pch data point symbols into axis labels in R

I have combined two data sets into one graph and I would like to add the corresponding pch symbols right into the axis labels.
Now, I know that solutions using text() instead of xlab and ylab and Hershey vector fonts (instead of citing a pch=16, etc.) are given here and here but the symbols appear kinda wonky-shaped. Does anyone have a more "well-rounded" solution?
thinkoholic.com's reproducible example,
par(mar=c(5,5,2,5))
# create data and plot circles
x <- seq(0,5,0.5)
y <- seq(0,5,0.5)
plot(x,y, xlab="", ylab="")
#create random data and add bullets (pch=19)
x <- rnorm(20,2.5)
y <- rnorm(20,2.5)
points(x,y, pch=19)
#add y axis on right side
axis (side = 4)
#create text with symbols
text(-1,2.5,"\\#H0850 y1 axis text", vfont=c("sans serif","plain"), cex=1.25, adj=0.5, srt=90, xpd=TRUE)
text(6,2.5,"\\#H0902 y2 axis text", vfont=c("sans serif","plain"), cex=1.25, adj=0.5, srt=90, xpd=TRUE)
text(2.5,-1,"x axis text", vfont=c("sans serif","plain"), cex=1.25, adj=0.5, srt=0, xpd=TRUE)
If you insist on doing this:
par(mar=c(5,5,2,5), xpd=TRUE)
x <- seq(0,5,0.5)
y <- seq(0,5,0.5)
plot(x,y, xlab="", ylab="")
text(-1,2.5,"y axis text", cex=1.25, adj=0, srt=90)
points(-1,2.4)
This should work:
mtext(paste0("your label text", " (", intToUtf8(9679), ")"), side = 2, line=2.5)

Resources