[R] barplot: legend: two rows

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue May 15 19:26:32 CEST 2012



On 15.05.2012 17:38, Marion Wenty wrote:
> Hello,
>
> I solved my problem with the uneven numbers of legend elements, which
> should be put into more than one rows, in the following way:
>
> I added another legend element which is empty ("") and is filled with
> "white":
>
> legend(0,-1.4,xjust=0,ncol=3,legend=c(colnames(dat)[-1],""),fill=c("navyblue","steelblue4","steelblue2","lightsteelblue3","lightsteelblue1","white"),bty="n",border="white",cex=1.2)
>
> Not quite such a nice way, but it works, anyway.


The previous version already worked for me, you just had to increase the 
bottom margin to make the second row of labels visible:

For example

par(mar=c(10,4.5+0.3529412*(max(nchar(colnames(dat1)))-1),1,1))

worked for me.

Uwe



> Marion
>
> 2012/5/11 Marion Wenty<marion.wenty at gmail.com>
>
>> Hello,
>>
>> thank you very much for your answers. Using the command ncol worked in my
>> special case, if I have got a legend with 4 elements, but with 5 it doesn't
>> work. I am using two functions which I had created and checked what might
>> be the difference, so that the second one doesn't work, but I couldn't find
>> out what the problem is. So I am including the text of my .csv files and my
>> functions:
>>
>>
>> My .csv file "abb8a" looks like this:
>>
>> ;sehr gut;eher gut;eher nicht gut;nicht gut
>> Kontakt mit der Schule;46;49;4;1
>> Die richtige Wahl;32;58;8;2
>>
>>
>> mw_g_4stap<-function(pfad="F:/04 Archiv/04 Programme/19
>> R_Syntaxe/Grafiken_Funktionen",abb="abb8a")
>> {
>>    setwd(pfad)
>>    postscript(file=paste(abb,".eps",sep=""))
>>    dat<-read.csv2(paste(abb,".csv",sep=""),header=T,check.names=F)
>>    dat1<-t(as.matrix(dat[,2:5],nrow=2))
>>    colnames(dat1)<-dat[,1]
>>    zehn<-seq(10,100,10)
>>    xmax<-max(dat1)
>>    ind<-min(which(zehn>=xmax))
>>    ticks<-seq(0,100,10)
>>    par(las=1)
>>    par(mar=c(5.9,4.5+0.3529412*(max(nchar(colnames(dat1)))-1),1,1))
>>
>> barplot(dat1,width=0.61,horiz=T,col=c("steelblue4","steelblue2","lightsteelblue3","lightsteelblue1"),border="NA",axes=F,beside=F,xlim=c(0,100),cex.names=1.2,ylim=c(0,10))
>>    par(xpd=TRUE)
>>    abline(v = seq(10, 100, by = 10), col = "white")
>>    par(xpd=F)
>>    axis(1,at=ticks,las=1,labels=paste(ticks,"%",sep=""))
>>    par(xpd=TRUE)
>>
>> legend(0,-1.4,xjust=0,ncol=2,legend=colnames(dat)[-1],fill=c("steelblue4","steelblue2","lightsteelblue3","lightsteelblue1"),bty="n",border="white",cex=1.2)
>>    par(xpd=FALSE)
>>    dev.off()
>> }
>> mw_g_4stap()
>>
>> ###
>>
>> My .csv file "probe8_5" looks like this:
>>
>> ;stimme zu;stimme zu;stimme zu;stimme zu;stimme zu
>> Eltern konnten mir beim ler;23;70;1;1;5
>> leichte Ent;20;10;10;10;50
>> leichte Ent;20;10;10;10;50
>> leichte Ent;20;10;10;10;50
>> leichte Ent;20;10;10;10;50
>> leichte Ent;20;10;10;10;50
>> leichte Ent;20;10;10;10;50
>>
>>
>> mw_g_5stap<-function(pfad="F:/04 Archiv/04 Programme/19
>> R_Syntaxe/Grafiken_Funktionen",abb="probe8_5")
>> {
>>    setwd(pfad)
>>    postscript(file=paste(abb,".eps",sep=""))
>>    dat<-read.csv2(paste(abb,".csv",sep=""),header=T,check.names=F)
>>    dat1<-t(as.matrix(dat[,2:6],nrow=2))
>>    dat1
>>    colnames(dat1)<-dat[,1]
>>    dat1
>>    zehn<-seq(10,100,10)
>>    xmax<-max(dat1)
>>    ind<-min(which(zehn>=xmax))
>>    ticks<-seq(0,100,10)
>>    par(las=1)
>>    par(mar=c(5,4.5+0.3529412*(max(nchar(colnames(dat1)))-1),1,1))
>>
>> barplot(dat1,width=0.61,horiz=T,col=c("navyblue","steelblue4","steelblue2","lightsteelblue3","lightsteelblue1"),border="NA",axes=F,beside=F,xlim=c(0,100),cex.names=1.2,ylim=c(0,10))
>>    par(xpd=TRUE)
>>    abline(v = seq(10, 100, by = 10), col = "white")
>>    par(xpd=F)
>>    axis(1,at=ticks,las=1,labels=paste(ticks,"%",sep=""))
>>    par(xpd=TRUE)
>>
>> legend(0,-1.4,xjust=0,ncol=3,legend=colnames(dat)[-1],fill=c("navyblue","steelblue4","steelblue2","lightsteelblue3","lightsteelblue1"),bty="n",border="white",cex=1.2)
>>    dev.off()
>> }
>> mw_g_5stap()
>>
>> ###
>>
>> The first function works but the second one doesn't.
>>
>> Does anyone know why?
>>
>> Thank you very much for your help in advance!!
>>
>> Marion
>>
>>
>>
>> 2012/5/9 Uwe Ligges<ligges at statistik.tu-dortmund.de>
>>
>>>
>>>
>>> On 09.05.2012 13:23, Marion Wenty wrote:
>>>
>>>> dear r-helpers,
>>>>
>>>> i have got another question:
>>>>
>>>> i am using the functions
>>>>
>>>> par(xpd=T)
>>>> legend
>>>>
>>>> to create a legend below the x-axis. i used the parameter horiz=T.
>>>>
>>>> now i would like to put the elements of the legend in two rows:
>>>>
>>>> e.g. if my legend has got 5 elements, i would like 3 elements in one row
>>>> and the last two elements in the next row.
>>>>
>>>> does anyone know how to do that?
>>>>
>>>
>>> Example:
>>>
>>> bp<- barplot(1)
>>> par(xpd = TRUE)
>>> legend(bp, 0, xjust=0.5, legend=letters[1:5], lwd=1:5, ncol=3)
>>>
>>> Uwe Ligges
>>>
>>>
>>>   thank you very much for your help in advance!
>>>>
>>>> marion
>>>>
>>>>         [[alternative HTML version deleted]]
>>>>
>>>> ______________________________**________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>>> PLEASE do read the posting guide http://www.R-project.org/**
>>>> posting-guide.html<http://www.R-project.org/posting-guide.html>
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>
>



More information about the R-help mailing list