2

I am super new to R coding and I'm trying to create some simple bar graphs for a presentation. I took two data sets and overlapped them using par(new=TRUE) but for some reason, the bars that I want to overlap are just slightly underneath the bars I want under. I have no idea how to fix this.

BG_all is my dataset, count and disease are my 2 variables that I want to overlap

Here is my code:

barplot(BG_all$Count,names.arg=1:12,xlab='Month',ylab='Total Catch',col = "skyblue3")
par(new=TRUE)

barplot(BG_all$Disease,ylim=c(0,1000),axes=FALSE,col="grey0")

title(main="Black Gill Disease in White Shrimp Trawl Survey Catch")

If you look at the link at the bottom of my post, you'll see what I'm talking about...the Disease variable bars are slightly below the blue Count variable bars.

Any idea of how to solve this problem or find an easier way to make this sort of plot? I've tried using ggplot based off of a tutorial and I've gotten error message after error message, so I thought laying one on top of another would be helpful

enter image description here

kgangadhar
  • 4,886
  • 5
  • 36
  • 54

1 Answers1

4

Your y axis value ranges are likely different. Try setting ylim to be the same for both plots.

The following exhibits the same problem you are seeing:

d1 <- 1.0 2.0 3.0 4.0 4.8
d2 <- 0.0 0.0 0.0 0.5 3.3
barplot(d1)
par(new=T)
barplot(d2, ylim=c(0,5), col='skyblue', axes=F)

Second barplot below the first

The following uses the same ylim for both plots:

d1 <- 1.0 2.0 3.0 4.0 4.8
d2 <- 0.0 0.0 0.0 0.5 3.3
barplot(d1, ylim=c(0,5))
par(new=T)
barplot(d2, ylim=c(0,5), col='skyblue', axes=F)

Bars aligned

Mike N.
  • 1,662
  • 1
  • 13
  • 19
  • Thanks, this is exactly what I needed. I tried doing that before I left for break and I was getting all sorts of weird error messages, but it worked today. R is fun. – Jillian Swinford Jan 03 '18 at 16:43