Help Legend positioning of a barchart

I'm trying to create a clustered bar chart with the following command:

barplot(condtab, main="Vision Status by Sex (Sex is Explanatory)",xlab="Sex", ylab="Percent", col=c("darkblue","red","green"),legend = rownames(counts), beside=TRUE,)

And I get this plot

As you can see, the legend is right over the bars in the second cluster. I've tried physically changing the size of the window. Any suggestions?

Please post some data in the form of a reproducible example so we can get the same result that you are seeing.

Here is my (nearly) minimal code.

counts=table(mtcars$carb,mtcars$cyl)
counts
condtab =100*prop.table(counts,2)
condtab
barplot(condtab, main="Carb Chambers by cylinder count",xlab="Cylinders", ylab="Percent", col=c("#a62bcd","#ffff9d","#6dffdc","#ffb96d","#736dff","#cedaff"), legend=rownames(counts),beside=TRUE)

Adding the code

args.legend = list(x = 9.1, y = 45)

enabled me to end up with something that works. I'm still not completely happy that the legend overlaps the bars, but if I move it too far to the right, it would get cut out of the plot window. (And I tried changing the size of the plot window. This didn't work for me, but some of my colleagues said it worked for them.)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

There are many ways to move the legend and you can read about them by entering

?legend

in the console. Here are a few examples.

counts=table(mtcars$carb,mtcars$cyl)
counts
#>    
#>     4 6 8
#>   1 5 2 0
#>   2 6 0 4
#>   3 0 0 3
#>   4 0 4 6
#>   6 0 1 0
#>   8 0 0 1
condtab =100*prop.table(counts,2)
condtab
#>    
#>             4         6         8
#>   1 45.454545 28.571429  0.000000
#>   2 54.545455  0.000000 28.571429
#>   3  0.000000  0.000000 21.428571
#>   4  0.000000 57.142857 42.857143
#>   6  0.000000 14.285714  0.000000
#>   8  0.000000  0.000000  7.142857
barplot(condtab, main="Carb Chambers by cylinder count",xlab="Cylinders", 
        ylab="Percent", 
        col=c("#a62bcd","#ffff9d","#6dffdc","#ffb96d","#736dff","#cedaff"), 
        legend.text =rownames(counts),beside=TRUE)


barplot(condtab, main="Carb Chambers by cylinder count",xlab="Cylinders", 
        ylab="Percent", 
        col=c("#a62bcd","#ffff9d","#6dffdc","#ffb96d","#736dff","#cedaff"), 
        legend.text =rownames(counts),beside=TRUE, 
        args.legend = list(x = "topright", horiz = TRUE))


barplot(condtab, main="Carb Chambers by cylinder count",xlab="Cylinders", 
        ylab="Percent", 
        col=c("#a62bcd","#ffff9d","#6dffdc","#ffb96d","#736dff","#cedaff"), 
        legend.text =rownames(counts),beside=TRUE, 
        args.legend = list(x = 8, y = 50, ncol = 2))

Created on 2020-04-15 by the reprex package (v0.3.0)

I tried playing with xlim and it help to push the legend to the right of the data.

counts=table(mtcars$carb,mtcars$cyl)
counts
#>    
#>     4 6 8
#>   1 5 2 0
#>   2 6 0 4
#>   3 0 0 3
#>   4 0 4 6
#>   6 0 1 0
#>   8 0 0 1
condtab =100*prop.table(counts,2)
condtab
#>    
#>             4         6         8
#>   1 45.454545 28.571429  0.000000
#>   2 54.545455  0.000000 28.571429
#>   3  0.000000  0.000000 21.428571
#>   4  0.000000 57.142857 42.857143
#>   6  0.000000 14.285714  0.000000
#>   8  0.000000  0.000000  7.142857
barplot(condtab, main="Carb Chambers by cylinder count",xlab="Cylinders", 
        ylab="Percent",
        col=c("#a62bcd","#ffff9d","#6dffdc","#ffb96d","#736dff","#cedaff"), 
        legend.text =rownames(counts),beside=TRUE)

barplot(condtab, main="Carb Chambers by cylinder count",xlab="Cylinders", 
        ylab="Percent", xlim = c(0,24),
        col=c("#a62bcd","#ffff9d","#6dffdc","#ffb96d","#736dff","#cedaff"), 
        legend.text =rownames(counts),beside=TRUE)

Created on 2020-04-15 by the reprex package (v0.3.0)