I have a curious question, I am using par() to make a muli-panel barplot, I notice that the bars are different sizes, I was wondering if it is possible to make the bars the same width (size) across each panel? This would create different size panels, but this would be fine. Any comments would be helpful.
I have this generic example:
# create data
a<-c(1:100)
b<-c(1:200)
c<-c(1:300)
d<-c(1:400)
e<-c(1:500)
#make dataframes for barplots
test<-as.data.frame(cbind(a,b))
test1<-as.data.frame(cbind(a,b,c))
test2<-as.data.frame(cbind(a,b,c,d))
test3<-as.data.frame(cbind(a,b,c,d,e))
#gets means for each column
a1<-colMeans(test)
a2<-colMeans(test1)
a3<-colMeans(test2)
a4<-colMeans(test3)
#lets plot
pdf(file= "/Users/Highf_000/Desktop/prac.pdf");
par(mfrow = c(2, 4), # 2 rows x 4 columns layout
oma = c(2, 2, 0, 0), # two rows of text at the outer left and bottom margin
mar = c(5, 5, 2, 1)+0.1, # space for one row of text at ticks and to separate plots
mgp = c(2, 1, 0), # axis label at 2 rows distance, tick labels at 1 row
xpd = NA)
barplot(mean(a))
barplot(a1)
barplot(a2)
barplot(a3)
barplot(a4)
dev.off()
How do we overcome the plot disappearing in a multiple plot layout and how do we fix the title to be centered over the bars not the plot?
Simplifying your example
a1 <- c(1,2)
a2 <- c(1,2,3)
a3 <- c(1,2,3,4)
barplot(a1) ## produces uneven bars
barplot(a2) ## produces uneven bars
barplot(a3) ## produces uneven bars
As explained here, you can do at least one of the following:
Option 1
df <- data.frame(bar1 = c(1, 2, 0,0),
bar2 = c(1, 2, 3, 0),
bar3 = c(1, 2, 3, 4))
barplot(as.matrix(df), beside = TRUE)
Option 2
bar1 <- barplot(a1, xlim = c(0, 1), width = 0.1)
bar2 <- barplot(a2, xlim=c(0,1), width = 0.1)
bar3 <- barplot(a3, xlim=c(0,1), width = 0.1)
There's also the ggplot option is you are not keen on barplot.