Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I auto-title a plot with the R call that produced it?

Tags:

People also ask

Which method should be used to change the title of a plot in R?

title() function in R Language is used to add main title and axis title to a graph. This function can also be used to modify the existing titles. Syntax: title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, …)

How do I add labels to a plot in R?

Use the title( ) function to add labels to a plot. Many other graphical parameters (such as text size, font, rotation, and color) can also be specified in the title( ) function. # labels 25% smaller than the default and green.

How do I add a title to a histogram in R?

You can change the title of the histogram by adding main as an argument to hist() function. In this case, you make a histogram of the AirPassengers data set with the title “Histogram for Air Passengers”: If you want to adjust the label of the x-axis, add xlab .


R's plotting is great for data exploration, as it often has very intelligent defaults. For example, when plotting with a formula the labels for the plot axes are derived from the formula. In other words, the following two calls produce the same output:

plot(x~y)
plot(x~y, xlab="x", ylab="y")

Is there any way to get a similar "intelligent auto-title"?

For example, I would like to call

plot(x~y, main=<something>)

And produce the same output as calling

plot(x~y, main="plot(x~y)")

Where the <something> inserts the call used using some kind of introspection.

Is there a facility for doing this in R, either through some standard mechanism or an external package?

edit: One suggestion was to specify the formula as a string, and supply that as the argument to a formula() call as well as main. This is useful, but it misses out on parameters than can affect a plot, such as using subsets of data. To elaborate, I'd like

x<-c(1,2,3)
y<-c(1,2,3)
z<-c(0,0,1)
d<-data.frame(x,y,z)
plot(x~y, subset(d, z==0), main=<something>)

To have the same effect as

plot(x~y, subset(d, z==0), main="plot(x~y, subset(d, z==0))")