Making boxplot. Specifying the axes separately. Heres the code.
boxplot(data$overall~data$league, horizontal=T, las=2, axes='n', outpch=20,
outcex=.5, cex.axis=.5, col=rainbow(7, alpha=.5))
axis(1, lty=1, at=seq(45,90,5))
axis(2, at=data$league, labels=as.character(data$league), las=2, cex.axis=.3)
It takes a long time to run the line that puts the axis there, and the words look so weird and blurry. That makes me think they are getting stacked on top of each other. How do I prevent this?
Also, how do I flip the y-axis (data$league) from Z-A sort to an A-Z sort?
Related
To draw X-axis labels for barplots in R, I use text() like this:
text(mean(bp), par("usr")[3] - 0.05*yDiff, xpd=NA, labels=journey, cex=0.9, font=2)
To draw a legend, I use something like this:
legend("bottom", legend=abbrevLabels, fill=c(colors), xpd=NA, horiz=TRUE, bty="n",
cex=1.0, inset=c(0, -0.3), xjust=0, adj=0.035,
text.width=rep(meanLabelLen/7.5, length(legendLabels)))
Both vertical offsets here -- the 0.05*yDiff and the insert offset of -0.3 for the legend -- are in Y-axis space. For various reasons, that's a problem for me. Instead, I prefer offsets in units of pixels. That is, what I really want to say is: "render labels 10 pixels below the bottom of the graph".
How can this be done?
For constant vertical offset of horizontal, X-axis labels situated underneath bar plots (at the bottom of a larger grid of such plots), it turns out that using title with the line attribute has consistent behavior / placement independent of device size. Here is what I now do:
title(xlab=journey, line=0, cex=0.8, font.lab=2, xpd=NA)
where line is integral and 0 is nearest the plot, and a higher integer is further away (in the negative Y direction).
For the legend, I could not come up with a constant vertical offset that behaved independently of either grid dimensions or device size. Several points here:
yjust parameter to legend did not work at all. I looked online for examples, but no matter what value I used, yjust was a no-go.
y parameter does work, but regrettably, not in conjunction with a descriptive attribute for x; that is, if x is set to bottom, you cannot concurrently set y. I found this behavior disconcerting, as I would generally expect these coordinates to be independent of each other.
To work around these limitations, here is what I did:
yInset <- 0.4 * (1.075 - par("fin")[2] / dev.size("in")[2])
legend(x='bottom', yjust=-0.75, inset=c(0, -yInset), legend=abbrevLabels, fill=c(colors), xpd=NA, horiz=TRUE, bty="n", cex=1.0, adj=0.035, text.width=rep(meanLabelLen/7.5, length(legendLabels)))
This logic effectively sets up a sliding Y-offset in proportion to grid dimensions and device size. The empirically determined scaling / offset factors of 1.075 and 0.4 are truly gross, but for now suffice.
It would help in the future if some of the limitations for legend cited above are resolved (or possibly better explained in the case that I misunderstood the docs).
I manually added in some tick marks, but these tick marks are too close and thus overlap with numbers already there. I can specify the exact location for these number labels? Here is an example picture of my problem. I tried changing the size, but there is still overlap.
You can use the mtext function to manually add text on the margins. The line and at arguments give the position of the text. See ?mtext for more details.
plot(x=1:4, y=1:4)
abline(h=c(2.6,3.4))
mtext(c(2.6, 3.4), side=2, line=0, at=c(2.6, 3.4))
x=1:10
y=10:1
use the xaxt= 'n' to rid yourself of the wrong axis
plot(x,y,xaxt='n')
then use the axis function to get them exactly where you want
l1=1:10
at1=1:10
axis(1, labels=l1,at=1:10)
now add a new tick mark somewhere, take out a tick mark if you want.
l2=c(l1[-3],3.14)
at2=c(at1[-3],pi)
plot(x,y,xaxt='n')
axis(1, labels=l2,at=at2)
#RGR ~ Treatment:Geno boxplot
fit <- aov(Total.RGR~Treatment:Geno, data=For.R)
summary(fit)
t <- TukeyHSD(fit)
t
boxplot(Total.RGR~Treatment:Geno,
data=For.R,las=2,ylim=c(-20,100),xlab="Treatment:Geno",ylab="RGR (mg/day)")
text(1:8, 95 ,c("a","ac","a","a","a","bd","bcd","ad"))
Is my code, it does the job, but when I rotate my x-axis labels the obstruct the x-axis title.
Does any of you know of a way to move the title down?
It must be easy but I cant find anything in the reference.
Thanks.
Mathias
I would set xlab="" and add it afterwards using mtext. So, either you play with line parameter an put it the bottom under your lables Or change completely the side to put it in the top of the plot.
mtext("Treatment:Geno", side=1, line=5)
mtext("Treatment:Geno", side=3)
I have a location quotient plot drawn in R and want to draw a horizontal line along the plot where Y = 1. I have the code abline(h=1, col="black") but when the line is drawn, it intersects the Y-axis and crosses out my Y-axis labels.
Does anyone know how to terminate the line at the Y-axis rather than having it intersect?
Many thanks.
As mentioned in the comments, it looks like the parameter xpd has been changed, so one option is to change it back to FALSE, see ?par. you can control the clipping region using the clip function to further limit the range that abline and other functions plot within. This may also be affected by you plotting device (different devices can deal with clipping differently).
Here is my code; basically I am putting four graphs on the same plot device and putting a legend on each. [edit: I am working with RStudio]
dev.new()
par(mfrow=c(2,2), oma=c(0,0,2,0))
#plot1
plot(parameters...)
par(new=TRUE)
plot(parameters, col="red")
legend("bottomright", c("seed match", "background"), bty="n", lty=c(1,1),
col=c("red","black"), cex=0.8, inset=0)
#plot2
plot(parameters...)
par(new=TRUE)
plot(parameters..., col="red")
legend("bottomright", c("seed match", "background"), bty="n", lty=c(1,1),
col=c("red","black"), cex=0.8,inset=0)
#etc. same for plot2 and plot 3
title("bla bla bla", outer=TRUE)
I have two issues with this.
(1) even though I specified "bottomright", the legend doesn't seem to be aligned to the bottom right, the wider I resize the graph horizontally, the more space there is between the legend and the right of the graph.
(2) the amount of space the legend occupies is inadequate. I tried modifying cex= but that only takes care of font size, the overall space occupied by the legend remains, meaning that the smaller the font, the bigger the space between lines. I would like the legend to be a little less "spread out".
Illustration
This looks sort of OK, although I would like to decrease the space between lines inside the legend:
But when I resize horizontally it doesn't. I would like to tether the legend to the right of the graph.
(1):
As your graphs are all scaled the same way, you could use x and y coordinates to position the legend, not the key words. e.g.:
legend(x = 0.25, y = 35, c("seed match", "background"), bty="n", lty=c(1,1), col=c("red","black"), cex=0.8, inset=0)
(2):
I don't know if there exists a way to manipulate line spacing via legend(), I didn't find one. I always switch to manual generation of the legend via mtext(), abline() and such when the legend has to look really pretty. It is more work but you got control over every aspect of your legend.
One last comment: I guess you want your graph to like nice not on your screen but on some sort of paper or presentation. I always generate graphs with devices like cairo_ps(), svg() or jpeg() (jpeg only on rare occasions because it is raster, not vector-based). These functions give you more control about your graphs than exporting the R Graphics Device. But the way a graph looks changes with the device, each one needs to be configured separately. Better do it only for the one you are going to use in the end.
I hope this helps
Instead of using "bottomright", you could use legend() twice, for each legend element with position values you choose such as
legend(x1,y1,c("seed match"), bty="n", lty=1, col="red", cex=0.8)
for the first one. This way you can choose their position individually and therefore control the spacing between them. I think this solve your two issues.