R: Boxplot - how to move the x-axis label down? - r

#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)

Related

R - Boxplot with label in the left bottom corner

I want to put some labels into a boxplot, to show significance values. My favorite would be the position left bottom. I tried to do it with legend, but had the problem that I don't have any symbols to show and couldn't find anything to show the legend without symbols. I found this, but thought it's way too complicated. There should exist an easier solution.
If it's possible I would prefer to use the base R functionality and no plugin.
So this is how it looks like now and I want the labels (in rows) in the bottom left corner without the gap in the beginning.
Maybe the legend command isn't the command I'm looking for?
One way to do this is to write the text directly using the text function.
Since you do not provide your data, I will illustrate with some built-in data, but you will need to adjust the placement for your specific plot.
boxplot(Sepal.Width ~ Species, data=iris, ylim=c(1,5))
text(0.45,0.95, "1960-2002**\n2002-2012**\nt-test verb.", adj=c(0,0))
The adj=c(0,0) part is to left-justify the text.
If you want the box around the text, add:
rect(0,0,1.2,1.6)
You also can do this with legend and get rid of the gap by specifying the x,y coordinates of the legend. Once again, you will need to adjust for your plot.
boxplot(Sepal.Width ~ Species, data=iris, ylim=c(1,5))
legend(0.23, 1.65, c("1960-2002**", "2002-2012**", "t-test verb."))

R: plotROC main title cannot be changed from default

I use the plotROC from hmeasure-package to (try to) plot an ROC curve. I get a plot like shown below. The code I used is:
require(hmeasure)
predictions_LIMO <- data.frame(df)
h_LIMO <- HMeasure(mydata$churn, predictions_LIMO)
plotROC(h_LIMO,which=1)
If I try to customize the main title, I get the two titles overlapping each other, like shown in the lower picture. The additional code is:
title("ROC curve for LIMO4 to LIMO8")
Is there a way to get rid of the automatically assigned title? I tried main=0 but received unused argument (main = 0).
In the ?plotROC I did not find anything about that.
Thanks for any advice (maybe also on different packages that I can use to circumvent the problem)!
As #joran said, it's hard-coded. Here's a quick hack if you don't want to mess with the plotROC function. You can "cover" the existing title with a white rectangle and then add your own title. For example:
# Adjust location of rectangle as needed. These coordinates should work
# for the graph you posted.
rect(0, 1.1, 1, 1.7, xpd=TRUE, col="white", border="white")
title("My Title")

Lattice in R: Bottom of bar charts not aligned with x=0

I tried making a simple bar chart using the barchart function from the Lattice library with the following code
plot_sum_h <- barchart(event~value|incidence, data=msum_h_combo,
groups=variable,
ylab="Event", xlab="", layout=c(2,1),
main="Total Number of Casualities",
scales=list(y=list(relation="same"),
x=list(relation="free"))
)
and got
this graph. Links not working anymore.
The bottoms of the bars, which should be aligned with the zero mark on the x-axis are behind x=0. Does anyone have any idea what might have gone wrong? Any advice would be appreciated.
You can set that by setting limits on the x-axis. In your scales argument you can do
x=list(relation="free", limits= list(c(0,2000),c(0.20000)) )
In the limits list this will set the scales for each panel (when you use relation="free")
That will put the edge of the graph at 0, and I just guessed what would be good for the upper limits. You could also set limits like =c( 0,( 1.1*max(values) )) Which would set the max at 110% of the maximum of its Value in each panel.

How to plot just one axis label in R?

A beginner question: I currently have some plots that look like this.
I'm keeping the plots free of other annotation because I have to fit a lot of them onto one page and specific values aren't important, just the general trend that the red line is to the right of the black one. However, I'd like to be able to indicate that the dashed line in my plot actually represents zero, like so:
Is there any way to tell R to only display that value on the x-axis labels? I don't think using text() would be a good idea because it relies on absolute coordinates, but I'm not really sure how to work with axis options to make it do what I want.
Try:
axis(side = 1, at = 0)
See ?axis for details.

R/Zoo: show a tick every year on x-axis

I've a zoo object, with a yearqtr index, covering about 50 years. When plotted the x-axis shows labels every 10 years, which feels a bit barren:
b=zoo(1:200,as.yearqtr(1900+seq(1,200)/4))
plot(b)
Some study got me this:
plot(b,xaxt="n");axis(1,time(b))
Which feels like swinging from one extreme to the other, as the x-axis is a blur of ticks, with ugly fractional labels. Is there an easy way to have it just show years? (What I was looking for initially was a way to say: "lower the x-axis label spacing a bit", but there seems nothing like that? cex.axis just alters the font-size.)
Did you read help(axis)?
Here is one way, just creating a simple index every four quarters:
R> ind <- seq(1, length(b), by=4)
and using it to index the axis placement and labels:
R> plot(b,xaxt="n")
R> axis(1,time(b)[ind], format(time(b)[ind]), las=2, cex.axis=0.5)
I used las=2 and the lower cex value to make this fit. Once every year may still be too plenty.
Computing "good" axis labels is really hard.
This is probably one of those (rare) situations when you want to use grid rather then ticks to better show your data. As #dirk-eddelbuettel pointed out - tweaking good axis labels is hard, especially with such density. You also might want your labels inside plot, so the grid will slightly hide their density. The easiest grid to get is with abline, unless you want to play with ggplot2, but it's uglier then standard plots in R (personal opinion). Also - make the plot wider. In fact, it's better to get rid of box around plot too ;) Below is mod of Dirk's approach:
png("strangeplot.png",width=800)
#extend y-axis to fit inside labels and remove box
plot(b,type="n",xaxt="n",yaxt="n",ylab="",xlab="",ylim=c(min(b)-30,max(b)),bty="n"))
#use 'mpg' to get labels inside
axis(1,time(b)[ind], format(time(b)[ind]), las=2, cex.axis=0.6,tick=F,mgp=c(0,-2.5,0))
axis(2,tick=F,las=1)
#you locate lines slightly to the left of label...
abline(h=seq(0,200,by=50),v=time(b)[ind]-0.5,col=gray(0.9))
#...so you need to add extra single line in the end
abline(v=max(time(b)[ind])+0.5,col=gray(0.9))
#plot at the end to get it above grid
points(b,type="l")
dev.off()

Resources