1
votes

I would like to plot 3 plots in the same window. Each will have a different amount of bar plots. How could I make them all the same size and close together (same distance from each other) without doing NAs in the smaller barplots. example code below. I do want to point out my real data will be plotting numbers from dataframes$columns not a vector of numbers as shown below. I am sure there is magic way to do this but cant seem to find helpful info on the net. thanks

pdf(file="PATH".pdf");
par(mfrow=c(1,3));
par(mar=c(9,6,4,2)+0.1);
barcenter1<- barplot(c(1,2,3,4,5));
mtext("Average Emergent", side=2, line=4);
par(mar=c(9,2,4,2)+0.1);
barcenter2<- barplot(c(1,2,3));
par(mar=c(9,2,4,2)+0.1);
barcenter3<- barplot(c(1,2,3,4,5,6,7));

Or would there be a way instead of using the par(mfrow....) to make a plot window, could we group the barcenter data on a single plot with an empty space between the bars? This way everything is spaced and looks the same?

1

1 Answers

2
votes

Using the parameters xlim and width:

par(mfrow = c(1, 3))
    par(mar = c(9, 6, 4, 2) + 0.1)
    barcenter1 <- barplot(c(1, 2, 3, 4, 5), xlim = c(0, 1), width = 0.1)
    mtext("Average Emergent", side = 2, line = 4)
    par(mar = c(9, 2, 4, 2) + 0.1)
    barcenter2 <- barplot(c(1, 2, 3), xlim = c(0, 1), width = 0.1)
    par(mar = c(9, 2, 4, 2) + 0.1)
    barcenter1 <- barplot(c(1, 2, 3, 4, 5, 6, 7), xlim = c(0, 1), width = 0.1)

enter image description here

Introducing zeroes:

df <- data.frame(barcenter1 = c(1, 2, 3, 4, 5, 0, 0), 
                 barcenter2 = c(1, 2, 3, 0, 0, 0, 0), 
                 barcenter3 = c(1, 2, 3, 4, 5, 6, 7))
barplot(as.matrix(df), beside = TRUE)

enter image description here

With ggplot2 you can get something like this:

df <- data.frame(x=c(1, 2, 3, 4, 5,1, 2, 3,1, 2, 3, 4, 5, 6, 7),
y=c(rep("bar1",5), rep("bar2",3),rep("bar3",7)))
library(ggplot2)
ggplot(data=df, aes(x = x, y = x)) +
  geom_bar(stat = "identity")+
  facet_grid(~ y)

enter image description here

For the option you mentioned in your second comment you would need:

x <- c(1, 2, 3, 4, 5, NA, 1, 2, 3, NA, 1, 2, 3, 4, 5, 6, 7)
barplot(x)

enter image description here