[R] Plotproblem, histogram, y-axis label?

JesperHybel jesperhybel at hotmail.com
Tue Aug 17 17:36:13 CEST 2010


How can I control label on y-axis? Specifically how high relative to the
y-axis it is plotted?

At the moment I am using mtext appearing in the function quotet below. 

The function is applied to a dataframe and plots histograms for all
numerical variables of the frame generating several histograms in a single
window. The height of the y-axis varies and unfortunately conflicts with the
yaxis label for some of the singular plots.

I could change the argument padj=-7 to -8 in the function line:
mtext("Density",side=2,cex=0.7,padj=-7,line=0,las=1)

but then "Density" is plotted to high for the other graphs. Is there a way I
can extract information on
the hight of the y-axis and put this to use in controlling the padj-value or
is there some other strategy??

I have tried changing the side-argument to side=3 as in

mtext("Density",at=0,side=3,cex=0.7,line=0,las=1)


But now something really strange (to me anyway
http://r.789695.n4.nabble.com/file/n2328545/image.jpg ) happens - as shown
in image: In the first row of histograms - with 3 columns - the "Density"
label is fitted nicely above the y axis. But in the second row the "Density"
label do not appear, the when I
make the plotwindow thinner - adjusting viewsize with mouse - they start to
appear. When I make the window wider the label of the histogram in the
second row second column moves to the left until it is above the label of
the histogram 2 row 1 column. When I continue to make the window wider the
labels of both mentioned histograms moves entirely out of view. This is what
the image.jpg shows....  




A secondary problem is that when i save the plot as .eps and later view it
adobe illustrater or GSview4.9
although the y-label did not conflict with the axis as shown in R-plotting
window it now conflicts with the axis.

 

The entire function used is as follows:


histogram<-function(x){

####Counts the number of numeric variables in dataset, and adjust plotframe
accordingly####
dataclass<-sapply(x,class)
count<-sum((dataclass=="numeric" | dataclass=="integer"))
par(mfrow=c(ceiling(count/3),3))


####Following code decides the design of the individual histogram plots####
f<-function(x,col.name){
list<-density(x)
y<-hist(x,breaks="FD",probability=TRUE,border="white",col=rgb(180,180,180,maxColorValue=255),las=1,ylab="",ylim=c(0,max(list$y)),xlab=col.name,
main="")
lines(density(x),col="steelblue4",lwd=1)
lines(quantile(x,c(0.025,0.975)),c(0,0),lwd=2,col="steelblue4")
mtext("Density",side=2,cex=0.7,padj=-7,line=0,las=1)
text(x=quantile(x,c(0.025,0.975)),y=c(max(y$density)/12,max(y$density)/12),labels=as.character(quantile(x,c(0.05,0.95))),
col="steelblue4",cex=0.8)
}


####Following code applies f - drawing of histogram - to all numeric
variables in dataframe####
for (i in 1:length(x)) {if (is.numeric(x[,i])) {
col.name<-names(x[i])
f(x[,i],col.name=col.name)}

}
}

Best regards

Jesper Hybel

-- 
View this message in context: http://r.789695.n4.nabble.com/Plotproblem-histogram-y-axis-label-tp2328545p2328545.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list