Nucore
Nucore

Reputation: 69

Plotting barplots next to each other with multiple dataframes

I want to plot different bar plots next to each other, but I get the following error message:

"Error in axis(if (horiz) 2 else 1, at = at.l, labels = names.arg, lty = axis.lty, : formal argument "at" matched by multiple actual arguments"

How can I solve this problem?

Here the data for this example:

structure(c(2L, 9L, 1L), .Dim = c(3L, 1L), .Dimnames = list(c("Positiv.26", 
    "Negativ.26", "Keine.26"), NULL))

structure(c(11L, 17L, 11L), .Dim = c(3L, 1L), .Dimnames = list(
        c("Positiv.45", "Negativ.45", "Keine.45"), NULL))

structure(c(17L, 15L, 7L), .Dim = c(3L, 1L), .Dimnames = list(
                c("Positiv.85", "Negativ.85", "Keine.85"), NULL))

This is what I have so far;

barplot((Histo.26[1:3,]), ylim= c(0,20), xlim = c(0,10), col=c("red","lightblue","gray"), 
        beside = T, xaxt="n")

barplot((Histo.45[1:3,]), at =c(4,5,6), col=c("red","lightblue","gray"), 
        xaxt="n", add=TRUE)

barplot((Histo.85[1:3,]), at =c(7,8,9), col=c("red","lightblue","gray"), 
        xaxt="n", add=TRUE)


axis(1, at = c(2,5,8), labels = c("Group1","Group2","Group3") , tick=FALSE , cex=0.3) 

Upvotes: 1

Views: 3498

Answers (2)

CPak
CPak

Reputation: 13581

Is there a reason you're not combining your data into a single list:

L <- as.vector( rbind( Histo.26,Histo.45,Histo.85 )[,1] )
[1]  2  9  1 11 17 11 17 15  7

Then plotting with axis:

barplot(L, ylim = c(0,20), col=c("red","lightblue","gray"), xaxt="n")
axis(1, at = c(2,5,8), labels = c("Group1","Group2","Group3") ,tick=FALSE, cex=0.3)

I have to change at in axis to c(2, 5.5, 9) to make it work. This would be the output;


                     https://i.sstatic.net/csckS.png

Upvotes: 1

M--
M--

Reputation: 29053

There are different approaches toward this and I want to present a new one using data.table package setDT and melt to make a combined data.frame of your datasets;

df1 <- cbind(Histo.85,Histo.45,Histo.26)
df1 <- data.frame(df1)

library(data.table)
setDT(df1, keep.rownames = TRUE)[]
colnames(df1) <- c("rn","85","45","26")

df2 <- melt(df1, id.vars='rn')

library(ggplot2)
ggplot(df2, aes(x=rn, y=value, fill=variable)) +
  geom_bar(stat='identity', position='dodge')

This would be the plot:

                                  bar plot

Upvotes: 1

Related Questions