12
$\begingroup$

I would like to plot four barplots on a single graph in R. I have used the following code. Here, how can keep a legend on top of the graph, specifically the legend should be between 2 and 3 barplots. I also tried with par(mar=c(4.1,4.1,8.1,4.1) but there is no success. Moreover, I also tried to run legend() after the second barplot, but there is no use. The legend is for all the four barplots. Please help me in this.

    par(mfrow=c(1,4))
    barplot(t(A), beside=T, ylim=c(-100,100),..)
    barplot(t(B), beside=T, ylim=c(-100,100),..)
    barplot(t(C), beside=T, ylim=c(-100,100),..)
    barplot(t(D), beside=T, ylim=c(-100,100),..)
    legend(...)
$\endgroup$

closed as off-topic by gung, kjetil b halvorsen, Sven Hohenstein, Silverfish, John Oct 4 '15 at 16:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – gung, kjetil b halvorsen, Sven Hohenstein, Silverfish, John
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Someone tag this with R $\endgroup$ – Brandon Bertelsen Aug 11 '11 at 3:31
  • $\begingroup$ @Kevin This is a valid Q here; the fact that R has command line interface does not mean any R question is a programming one. $\endgroup$ – user88 Aug 11 '11 at 7:36
  • $\begingroup$ @Brandon Sure; you can use suggested edits in future, you would also earn 2 rep for accepted suggestion. $\endgroup$ – user88 Aug 11 '11 at 7:37
17
$\begingroup$

Dr. Mike's answer is a good one, but I thought I'd provide solutions that take advantage of the faceting (or trellising) features of ggplot2 and lattice. First prep the data slightly:

mydata$id <- 1:nrow(mydata)
dat <- melt(mydata,id.vars = "id")

and then we can make the following in ggplot2:

ggplot(dat,aes(x=factor(id), y = value)) + 
    facet_wrap(~variable) +
    geom_bar(aes(fill = factor(id)))

enter image description here

and using lattice:

barchart(~value|variable,group = factor(id),data=dat,
         key = simpleKey(text = as.character(1:5),
                rectangles = TRUE,points = FALSE,space = "right"))

enter image description here

$\endgroup$
  • $\begingroup$ @Brandon - It's my new mission. These questions are better references for others if they contain examples from base, ggplot2 and lattice. $\endgroup$ – joran Aug 11 '11 at 3:38
  • $\begingroup$ @Joran: Fantastic. $\endgroup$ – samarasa Aug 11 '11 at 3:39
  • $\begingroup$ How to rename the label on "factor(id)" in the ggplot version? $\endgroup$ – alphabetagamma Aug 21 '17 at 21:42
11
$\begingroup$

I think the most simple solution is to use barplot command's inherent capabilities to solve your problem. The following code does what I interpret that you want done.

mydata <- data.frame(Barplot1=rbinom(5,16,0.6), Barplot2=rbinom(5,16,0.25),
                     Barplot3=rbinom(5,5,0.25), Barplot4=rbinom(5,16,0.7))
barplot(as.matrix(mydata), main="Interesting", ylab="Total", beside=TRUE, 
        col=terrain.colors(5))
legend(13, 12, c("Label1","Label2","Label3","Label4","Label5"), cex=0.6, 
       fill=terrain.colors(5))

plot

Hope this answers your question.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.