Title box in Base R plot - r

Is it possible to add a grey box for the title to a base R plot, similar to the ones that come with facet_wrap in ggplot?
I cannot use ggplot for a graphic, but I would like to add the gray title box when combining several plots.

you can try a very hackish one.
plot(1)
legend(x = c(0.567, 1.432), y = c(1.542015, 1.432), legend="", xpd=T, bg="grey")
mtext("this is a title", side=3, line=1)
More general approach...but still need to find out the ratios to expand the limits manually.
xlim = c(1,11)
ylim = c(1,15)
plot(1, xlim=xlim, ylim=ylim)
legend(x = xlim*c(0.84,1.032), y = c(ylim[2]*1.04, ylim[2]*1.16), legend="", xpd=T, bg="grey")
mtext("this is a title", side=3, line=1)

Related

What should I do to plot a picture

I want to plot a picture whose the beginning of xaxis and yaxis is 0.However, under my code, the result cann't match my requst. So I want to know where the mistakes are.Thanks!!
rm(list=ls())
axis.test<-function(){
t<-seq(0,200)
ntbase<-12/(1+exp(-(t-10)/25))
nt1<-12/(1+exp(-(t-30)/25))
nt2<-15.5334/(1+exp(-(t-20)/25))
nt3<-10.6997/(1+exp(-(t-5)/25))
nt4<-13.5906/(1+exp(-(t-30)/50))
nt<-data.frame(cbind(ntbase,nt1,nt2,nt3,nt4)) matplot(t,nt,col=1:5,type='l',lwd=2,lty=1:5,ylab="population(100,000)",xaxs='i',yaxs='i',xaxt='n',yaxt='n')
axis(side=1,at=c(0,20,40,60,80,100,120,140,160,180,200),labels=c(0,20,40,60,80,100,120,140,160,180,200),tck=0.03)
axis(side=2,at=c(0,5,10,15,20),labels=c(0,5,10,15,20),tck=0.03)
}
axis.test()
By "plotting a picture" I assume you mean plotting a graph. On a side note: I recommend using indentation and whitespaces to increase the readability of your code. Take a look at Hadley's Advanced R Style guide to improve your R coding style.
You can specify axis limits through matplot arguments xlim and ylim
matplot(
t, nt,
col=1:5,
type='l',
lwd=2,
lty=1:5,
ylab="population(100,000)",
xaxs='i', yaxs='i',
xaxt='n', yaxt='n',
xlim = c(0, 200),
ylim = c(0, 20))
axis(
side=1,
at=seq(0,200,20),
labels=seq(0,200,20),
tck=0.03)
axis(
side=2,
at=seq(0,20, 5),
labels=seq(0,20,5),
tck=0.03)

R horizontal barplot with aligned plot ontop

I am having trouble getting the spacing right on a plot on top of a horizontal barplot. It is the same general issue as described here:
http://www.r-bloggers.com/adding-lines-or-points-to-an-existing-barplot/
But I am trying to use "plot" instead of "points" or "lines". Is there a trick for using plot to get the spacing of the bars and the points to match?
Code:
barplot(df$DIC_mM,col=scalegreen, xlab="DIC mM", horiz=TRUE, xlim=c(0,0.7),
col.axis="white", col.lab="white", axes=FALSE, border="white")
axis(1,line=1,col="white",col.ticks="white",col.axis="white")
par(new = TRUE)
plot(df$d13DIC,df$Order, type="p", axes = FALSE, bty = "n", xlab ="",
col="deepskyblue2", lwd=5, xlim=c(-50,170), lend=2, col.lab="white", ylab="")
axis(3,at = c(-50,0,50,100,150), line=1, col="deepskyblue2", col.ticks="deepskyblue2",
col.axis="deepskyblue2")
mtext(expression(paste(delta ^{13},'DIC'," \u0028","\u2030","\u0029")), 3,
line=-0.5,at=50,col="deepskyblue2", cex=0.75)
Is there a reason why you don't want to use points to add the points? If you're willing to use points you can do it like this:
Create barplot and save the y-coordinates of the bars to y. You haven't provided sample data, so I'll use the built-in mtcars data frame:
y = barplot(mtcars$mpg[1:10], horiz=TRUE)
Now add the points. We use y as the y values, because those are the coordinates of the midpoints of each bar:
points(sqrt(mtcars$mpg[11:20]), y, col="red", pch=16, cex=2)
When you use par(new=TRUE) and then call plot again, you're overlaying a new plot with a new coordinate system that in general will be different from the original coordinate system.
This is what worked, based on this post suggested by eipi10: midpoints returned by barplot function do not actually line up with midpoints of bars
mp<-barplot(df$DIC_mM,col=scalegreen, xlab="DIC mM", horiz=TRUE, xlim=c(0,0.7), col.axis="white", col.lab="white", axes=FALSE, border="white", ylim=c(0,length(df$DIC_mM)+2))
axis(1,line=1,col="white",col.ticks="white",col.axis="white")
par(new = TRUE)
plot(df$d13DIC, mp, type="p", axes = FALSE, bty = "n", xlab ="",col="deepskyblue2", lwd=5, xlim=c(-50,170), lend=2, col.lab="white", ylab="", ylim=c(0,length(df$DIC_mM)+2))
axis(3,at = c(-50,0,50,100,150),line=1,col="deepskyblue2",col.ticks="deepskyblue2",col.axis="deepskyblue2")
mtext(expression(paste(delta ^{13},'DIC'," \u0028","\u2030","\u0029")),3,line=-0.5,at=50,col="deepskyblue2", cex=0.75)

Adding label to secondary axis in R

I have this code:
# Plotting everything
plot( p1, col= "lightgreen", xlim=c(-2.5,4.5), ylim=c(0, 700), main="Daily Total Precipitation for AR and Oct-May", xlab="ln(x)" , ylab="Frequency", xaxt = "n") # first histogram
plot( p2, col="red", xlim=c(-2.5,4.5), ylim=c(0, 700), xaxt = "n" , add=T)
# Adding in text labels on top of the bars
text(x, y, paste(round(percents,2),"%"), cex=0.50, pos=3, offset=0.3, col="black")
axis(side=1, at=breaks) # new x-axis
# parameter that needs to be set to add a new graph on top of the other ones
par(new=T)
plot(x, percents, xlim=c(-2.5,4.5), type="l", col="yellow", lwd=3.0, axes=F, ylab=NA, xlab=NA)
axis(side=4, at=seq(0,100,by=10), col="yellow", col.axis="yellow") # additional y-axis
mtext("Percent", side=4, col="yellow")
# legend settings
legend("topleft", c("AR", "Oct-May"), lwd=10, col=c("red", "lightgreen"))
Which produces this graph:
And I can't seem to figure out how to get the secondary y-axis label to show up in the correct position. Any help or suggestions is greatly appreciated.
Edit: Using RStudio.
One option is to specify the line argument to mtext(). In the example below I add a couple more lines to the right (side = 4) margin of the plot using par(), and then I draw three labels using mtext() at the default (line = 0), line 3 (line = 3), and line -3 (line = -3):
op <- par(mar = c(5,4,4,4) + 0.1)
plot(1:10)
mtext("line0", side = 4)
mtext("line3", side = 4, line = 3)
mtext("line-3", side = 4, line = -3)
par(op)
Note that line numbers increase away from the plot region and that negative line values move into the plot region, or to the left of the right boundary of the plot region.
It takes a little playing with the number of margin lines (as set in par(mar = x)) and which line you want to draw on using mtext(), but a little trial and error should get you what you want.
Note also that you don't need to specify integer values for the line argument. You can specify fractions of lines too: line = 2.5.

Pie-chart and legend are overlapping

I'm trying to create a pie-chart, like here, but the legend and the actual pie are overlapping.
The code I used is here:
library(ggplot2)
library(grid)
par(mai = c(1,1,1,1))
numb <- c(41, 30, 21, 8)
colors <- c("black", "grey70", "grey30","white")
numb_labels <- round(numb/sum(numb) * 100, 1)
numb_labels <- paste(numb_labels, "%", sep="")
xx <- c("the process of familiarizing with the codebase",
"the patch review process",
"the impact on the users of the project",
"high degree visibility of the contributions")
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend("top", legend = xx, fill=colors, bty="n")
Can someone help me?
I don't know why you attach ggplot2.
You can use layout:
par(mai = c(0,0,0,0))
layout(c(1,2),heights=c(0.3,1))
plot.new()
legend("bottom", legend = xx, fill=colors, bty="n")
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
One solution is to use argument xpd=TRUE with function par() and then set legend outside the plot region with coordinates of legend.
par(xpd=TRUE)
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend(-1.4,1.6, legend = xx, fill=colors, bty="n")
par(mai = c(1,1,4,1), xpd=TRUE)
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend("top", inset=c(0,-0.5),cex=0.9,legend = xx, fill=colors, bty="n")
Note: First element of inset shift your legend horizontally and the second one vertically.
cex parameter can resize your legend. So may adjust your legend by changing those parameter values.

Plotting a grid behind data, not in front in R

I like to produce my own grid lines when plotting so I can control tick marks, etc. and I am struggling with this with the 'hist' plotting routine.
hist(WindSpeed, breaks=c(0:31), freq=TRUE, col="blue", xaxt="n", yaxt="n", xlab="Wind Speed (m/s)",main="Foo", cex.main=1.5, cex.axis=1, cex.lab=1, tck=1, font.lab=2)
axis(1, tck=1, ,col.ticks="light gray")
axis(1, tck=-0.015, col.ticks="black")
axis(2, tck=1, col.ticks="light gray", lwd.ticks="1")
axis(2, tck=-0.015)
minor.tick(nx=5, ny=2, tick.ratio=0.5)
box()
Plot:
I have then just been able to use the 'lines' or 'points' command to replot the data over top for other types of plots, but with the histogram its not so easy.
Any help would be great.
I added my code below and image based upon John's response...
I added my code below and image based upon John's response...
hist(WindSpeed, breaks=30, freq=TRUE, col="blue", xaxt="n", yaxt="n", xlab="Wind Speed (m/s)",main="Foo", cex.main=1.5, cex.axis=1, cex.lab=1, font.lab=2)
axis(1, tck=1, col.ticks="light gray")
axis(1, tck=-0.015, col.ticks="black")
axis(2, tck=1, col.ticks="light gray", lwd.ticks="1")
axis(2, tck=-0.015)
minor.tick(nx=5, ny=2, tick.ratio=0.5)
box()
hist(WindSpeed, add=TRUE, breaks=30, freq=TRUE, col="blue", xaxt="n", yaxt="n", xlab="Wind Speed (m/s)", main="Foo", cex.main=1.5, cex.axis=1, cex.lab=1, font.lab=2)
Actually, R has a way to do this! It's the panel.first argument to plot.default, which hist calls to do most of the work. It takes an expression which is evaluated "after the plot axes are set up but before any plotting takes place. This can be useful for drawing background grids or scatterplot smooths," to quote from ?plot.default.
hist(WindSpeed, breaks=c(0:31), freq=TRUE, col="blue", xaxt="n", yaxt="n",
xlab="Wind Speed (m/s)", main="Foo",
cex.main=1.5, cex.axis=1, cex.lab=1, tck=1, font.lab=2,
panel.first={
axis(1, tck=1, col.ticks="light gray")
axis(1, tck=-0.015, col.ticks="black")
axis(2, tck=1, col.ticks="light gray", lwd.ticks="1")
axis(2, tck=-0.015)
minor.tick(nx=5, ny=2, tick.ratio=0.5)
box()
})
See How do I draw gridlines using abline() that are behind the data? for another question that uses this method.
This is relatively easy.
Generate the histogram but don't plot it.
h <- hist(y, plot = FALSE)
Now generate your base plot... I've added some features to make it look more like a standard historgram
plot(h$mids, h$counts, ylim = c(0, max(h$counts)), xlim = range(h$mids)*1.1,
type = 'n', bty = 'n', xlab = 'y', ylab = 'Counts', main = 'Histogram of y')
add your grid
grid()
add your histogram
hist(y, add = TRUE)
Or, as I discovered through this process... you can do it even easier
hist(y)
grid()
hist(y, add = TRUE, col = 'white')
This last method is just redrawing the histogram over the grid.
In R, order matters when you plot. As you've discovered, adding things to a plot adds on top of what you've plotted before. So we need a way to plot the grid first and then the histogram. Try something like this:
plot(1:10,1:10,type = "n")
grid(10,10)
hist(rnorm(100,5,1),add = TRUE)
I haven't recreated your example, since it isn't reproducible, but this general idea should work. But the key idea is to create an empty plot with the correct dimensions using the type = "n" option to plot, then add the grid, then add the histogram using the add = TRUE argument.
Note that the add argument is actually for plot.histogram, hist passes it along via ....
The base graphics solution suggested by #joran is fine. Alternatives:
d <- data.frame(x=rnorm(1000))
library(lattice)
histogram(~x,data=d,panel=function(...) {
panel.grid(...)
panel.histogram(...) }
)
Or:
library(ggplot2)
qplot(x,data=d,geom="histogram",binwidth=0.1)+theme_bw()+
labs(x="Wind speed", y="Frequency")
(But of course you will have to learn all the details of adjusting labels, titles, etc. ... I'm not actually sure how to do titles in ggplot ...)
Another methods for grid lines in background:
A)
hist( y, panel.first=grid() ) # see: help( plot.default )
box()
B)
plot.new() # new empty plot
nv <- length( pretty(x) ) - 1 # number of vertical grid lines (or set by hand)
nh <- length( pretty(y) ) - 1 # number of horizontal grid lines (or set by hand)
grid( nx = nv, ny = nh ) # preplot grid lines
par( new = TRUE ) # add next plot
plot( x, y ) # plot or hist, etc
box() # if plot hist
Arbitrary lines in background with abline:
C)
How do I draw gridlines using abline() that are behind the data?
D)
# first, be sure there is no +/-Inf, NA, NaN in x and y
# then, make the container plot with two invisible points:
plot( x = range( pretty( x ) ), y = range( pretty( y ) ), type = "n", ann = FALSE )
abline( h = hlines, v = vlines ) # draw lines. hlines, vlines: vectors of coordinates
par( new = TRUE ) # add next plot. It is not necessary with points, lines, segments, ...
plot( x, y ) # plot, hist, etc
box() # if plot hist

Resources