Tomas Alonso Rehor
Tomas Alonso Rehor

Reputation: 265

Overlay barplot with 2 y Axis

I want a barplot that Overlays with 2 variables (2 y axis).

This is my plot

enter image description here

And this is what im looking for.

enter image description here

Here is all the data.

What I used to make the plot.

ylim3 <- max(mesbar) + 2000
mesbar <- c(total_septiembre, total_octubre)
barplot(mesbar, main = "Month income",
        ylim = c(0,ylim3))
grid()

> mesbar
[1]  1260 12710

And i want to overlapp it with this data (days worked)

> dias_trabajados_sep
[1] 2
> dias_trabajados_oct
[1] 22

Upvotes: 0

Views: 68

Answers (1)

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

It can be done. However, keep in mind that you are playing with the axis ylims and that viewers could say that it is misleading. The trick is to add par(new = TRUE) and a new barplot. I chose to add the blue color trough rgb as it allows transparency trough its alpha argument. This will be useful when the gray bars will be shorter than the blue bars.

mesbar <-c(1260,12710)
dias <- c(2,22)
ylim3 <- max(mesbar) + 2000
#mesbar <- c(total_septiembre, total_octubre)
barplot(mesbar, main = "Month income",
        ylim = c(0,ylim3))
grid()
par(new = TRUE)
barplot(dias, main = "Month income", col=rgb(0,0,1, alpha=.5),xaxt = "n",yaxt="n",xlab="",ylab="", ylim=c(0,30))
axis(side=4)

enter image description here

Upvotes: 1

Related Questions