Do plots in R have handles? For example, if I were to use something like this:
plot(plotData[,1], plotData[,2], type='l', col='red', lty=2, xlab='x',
ylab='y', main='sample plot')
would I be able to change anything in this plot later like the color, or the x-axis label, or the x-axis range, etc.? Or do I have the plot this all over again?
Thanks!
Base R does not have handles in the sense that you are asking. Although you can modify some aspects of the plot after you've plotted it, much better suited to the needs in question would be to use ggplot
You can save the output to an object and append as needed.
You can also use last_plot()
Have a look at
http://www.cookbook-r.com/Graphs/ and http://docs.ggplot2.org/current/
Related
I’m using the metafor package to create Forest plots in R. I’d like to add text to my plots to create labels using the text() function. I’m wondering what the simplest way is to determine the x,y coordinates of where I want my text to go. Currently I just guess and see how it looks and then edit as necessary. Is there a way to overlay a grid over my plot or something to guide me (and then remove it after)?
Thank you!
Start by saving what forest() returns:
x <- forest(res)
And then take a look at x. Among other things, it contains xlim and ylim, which are the x-axis and y-axis limits. Try this:
abline(v=x$xlim)
abline(h=x$ylim)
Also useful:
text(x$xlim[1], x$rows, x$rows, xpd=NA)
I hope this helps.
I want to make a figure similar to this
See how the bottom portion is tiny... how do I do that?
I'm ok in ggplots2 so give me your expertise please.
I do not need to plot 2 values on the larger one. Essentially, I know how to generate everything I want from this figure, except how to make a tiny plot under a larger one (sans photoshop)
I have data.frames that contain the transcript information in the figure at the bottom, I'm only interested in one gene. To plot the genes you would do something like this
plot(transcript, type="l", lwd=2)
points(exons, type="l", lwd=3, col="blue")
points(utrExons, type="l", lwd=3)
To plot the large figure it would look like this
plot(genetic.variant, pch=16)
An exhaustive internet search turned into bupkis, how do you make two figures in the same plotting area with one of them being much smaller than the other?
With base graphics you can do something like this
dd<-data.frame(x=1:100, y1=runif(100), y2=cumsum(rnorm(100)))
layout(matrix(1:2, ncol=1), heights=c(3,1))
par(mar=c(0,3,3,2))
plot(y1~x,dd, xaxt="n", xlab="")
par(mar=c(3,3,0,2))
plot(y2~x,dd)
You can use layout
layout(c(1,2),widths=c(5,1),heights=c(5,1),T)
par(mar=c(1,1,1,1)
and just change the heights depending on your preference
##Example data to illustrate problem:
type=c("grp1","grp2","grp1","grp3","grp3","grp3","grp4")
num=c(1,1,2,4,3,5,1)
cols=c(rep("red",5),"green","red")
library(lattice)
bwplot(num~type)
par(new=T)
stripplot(num~type,col=cols)
I like the additional information displayed by the box plot but I need the information conveyed by the coloured points in the strip chart. Obviously par(new=T) doesn't work, so how can I overlay the points onto the box plot?
You can define a panel function like this :
library(lattice)
bwplot(num~type,panel=function(x,y,...){
panel.bwplot(x,y,...)
panel.stripplot(x,y,col=cols,...)
})
the answer by agstudy is quite good, though as I pointed out in a comment, any outliers plotted by bwplot will also be plotted by stripplot, which could get confusing.
Note this is only an issue if you were using jitter.data=TRUE as an option to stripplot, otherwise the duplicate points would plot directly over top of one another and you'd never know they were there. If the data had outliers, and you used jitter.data=TRUE, you'd see twice as many outliers as you should.
the following approach suppresses the outliers from bwplot to avoid this issue:
bwstrip <- function(x,y,...){
panel.stripplot(x,y,col=cols,do.out=FALSE,jitter.data=TRUE,...)
panel.bwplot(x,y,...)
}
bwplot(lobe.depths,panel=bwstrip)
I just wanted how know what category matches with everypoint when I do this:
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group)
I know how to make a legend and put text with an arbitrary vector c('A','B',C'), but is there a more "automatic" way for doing this? This is an easy example but I need to do it with residuals or survival functions plot
Thank you in advance.
The traditional graphics system provides the legend function for adding a
legend or key to a plot. But It should be noted that it is entirely the responsibility of the user to ensure that the legend corresponds to the plot. There is no automatic checking that
data symbols in the legend match those in the plot. It is simpler to do it using lattice or ggplot2. for example:
library(lattice)
xyplot(y~x,groups=group,auto.key=T)
if you want absolutly to use base graphics, you can do this :
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group,pch=as.numeric(group))
legend(2, 500, c('A','B','C'),
cex=1.5, pch=1:3,col=1:3)
I use par(new=T) before each of my plots to add my plot to the same graph.
However, when I do that it superimposes the two plots and the axis values get overwritten over each other and look messed up.
How do I properly add plot to the same graph that also normalizes axis intervals based on the two plots?
Use of par(new=TRUE) should be saved as a very last resort, usually there is a better/easier way. When creating the original plot set the xlim and ylim to include enough space for all the variables you will be plotting, then us functions like lines, points, symbols, or others to add the additional information: e.g.:
plot(x1,y1, xlim=range(x1,x2,x3), ylim=range(y1,y2,y3))
points(x2,y2, col='blue')
points(x3,y3, col='red')
There is also the matplot function which can plot several lines or sets of points in a single command.
Even better is to combine the data sets together then use xyplot from the lattice package or the ggplot2 package to do the multiple plots in one step.
There are also some functions in the plotrix package for combining graphs (with different scales as an option).
If you really need to use par(new=TRUE), then just specify the xlim and ylim in every plotting function to force them to line up. You can also supress the plotting of the default axes by specifying axes=FALSE or xaxt='n', yaxt='n', then, if wanted, you can use the axis function to put in axes on the other sides and can specify exactly where you want tick marks and labels.
Try ?lines, ?points, ?abline, or ?plot.xy.