I am trying to add a table to a plot with addtable2plot(). But I am running into a peculiar problem, viz. it's overriding the options in plot(). If I do:
plot(seq(1:10), axes=F)
the axes labels, ticks etc. are not printed. But if I then add:
addtable2plot(x=.8*max(axis(1)), y=.7*max(axis(2)), data.frame(matrix(1:9, nrow=3)))
the axes etc. get printed automatically.
that's because you call axis(). Use axTicks(1) instead.
Related
When I teach histograms using R, I use the yaxp parameter to adjust the y-axis. Here is an example of the code:
hist(bookspending$'Dollars on books', yaxp = c(0,9,9))
This produces a histogram where the y-axis has tick marks at 0, at 9, and a tick mark at every integer in between. Here is a picture of it:
I'm using Rstudio version 1.4.1717 and R version 4.1.1. This has worked the past 4 years. This semester the yaxp parameter isn't working for some students. I replicated the problem using the web-based version of R. Using the same code, the yaxp parameter makes no changes to the histogram. Here is the picture:
Why does yaxp no longer work for the online version, but it works for my version? How do you adjust the y-axis without yaxp?
This happens because there was a change in the function graphics:::plot.hist in R version 4.2.0.
Previously, the part of the function that controlled the drawing of the axes was simply:
if (axes) {
axis(1, ...)
axis(2, ...)
}
But it has been changed to
if (axes) {
axis(1, ...)
yt <- axTicks(2)
if (freq && any(ni <- (yt%%1) != 0))
yt <- yt[!ni]
axis(2, at = yt, ...)
}
Which means that the y axis breaks are now decided solely by the output of the function call axTicks(2). Your yaxp argument is still passed to axis, but unfortunately, specifying at nullifies yaxp.
This is sort of mentioned in the CRAN R News, where it says:
hist.default() gains new fuzz argument, and the histogram plot method no longer uses fractional axis ticks when displaying counts ("Frequency").
So the reason for the change was to prevent fractional axis ticks, but a side effect of this was preventing users from specifying y axis breaks. The only work-around for now it would seem, would be to add the axes manually as suggested in the last set of comments.
You may wish to file this as a bug report, since it seems to me that it should be possible to specify axis breaks and disallow fractional breaks without much difficulty.
Is there a way to have the second dataset plot on a separate axis, overlaid on the first plot?
using Plots; gadfly(size=(800,400))
plot(Vector[randn(100)], line = ([:green], :step))
plot!(Vector[randn(100)], line = ([:red], :step))
It is now done by adding a twinx() argument:
plot(rand(10))
plot!(twinx(),100rand(10))
There is, however, some unintentional axis and label behavior:
Labels are plotted on top of each other by default
xticks are plotted on top of the already existing plot
Default colors are not correlated to the total number of series in the subplot
Therefore, I suggest adding some additional arguments:
plot(rand(10),label="left",legend=:topleft)
plot!(twinx(),100rand(10),color=:red,xticks=:none,label="right")
There still seems to be an issue correlating all series associated with the subplot at the moment.
It's easy, but doesn't work with Gadfly. It should work fine with PyPlot and GR. Here's an example:
I can confirm (for GR) using Plots; gr()
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/
I am doing a barplot of 14 columns to represent some data, I set the names.arg option to show as vertical lables, unfortunately, this caused the new vertical lables to overlap with the "sub" and "xlab" options I have. How do I prevent this from happening?
Here's my command:
par(mar=c(6, 5, 4,7.5 ))
barplot(x, main=paste("title1 \n","subtitle"),
names.arg=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14),las=2, sub=("overlapping text1"),
xlab="overlapping text2", col = c("red2","darkorange3"))
Another question on my mind is, I am using a 2-line title in "main" as you can see from the command. Is it possible to make the second line font smaller, whilst keeping the first line in the same format?
Thanks,
One solution to change font size for one of titles, is to use two calls of functions mtext() in different lines with different cex= values and remove main= from barplot(). To overcome problem with overlapping text, mtext() also can be used instead of xlab= and sub=. You just have to find the right line= and increase space around plot with par(mar=..).
x<-sample(letters[1:14],300,replace=TRUE)
par(mar=c(9,3,5,2))
barplot(table(x),
names.arg=paste0("very_long_",1:14),las=2,
col = c("red2","darkorange3"))
mtext(side=3,"Title1",line=2,cex=2)
mtext(side=3,"subtitle",line=1,cex=1.5)
mtext(side=1,"overlapping text1",line=6)
mtext(side=1,"overlapping text2",line=7)
Another option to look at is the staxlab function in the plotrix package.
Also look at the mgp argument to the par function for a way to set the default placement of the axis title.
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.