DumpTruck33
DumpTruck33

Reputation: 3

How do I send my "abline" gridlines to appear behind my barplot data?

  1. I need the lines created with abline() to appear behind my barplot() bars. I need them running horizontally across the entire graph behind the bars at 10, 20, 30, and 40 on the Y axis. I have tried using the function panel.first() as well as calling abline() before/after creating the barplot. Neither seems to work.

This is my first attempt at creating these figures in R and I am assuming I have some problems with the formatting and organization. Would appreciate any advice/changes.

  1. On my Y axis I have ticks at 0, 10, 20, 30, and 40. Each are labeled with these values. I need to add tick marks at their midpoints (5, 15, 25, 35) and have them remain unlabeled and half the size of the major labeled ticks. I have tried using minor.tick, axes = FALSE and making changes with axis*() function. I also tried an additional rug function and a tick.ratio.

I have since solved the issue #1 and only need help on #2

EDIT: Here is the data I am using - CSV Data

Here is a copy of my code:

#NEED TO ADD TICK MARKS EVERY 5 ON YAXIS, SEND ABLINE TO BACKGROUND
#Create X and Y 
x <- table(fig402data.dat$well_ID)
y <- table(fig402data.dat$Avg_CO2_flow_scfm)
#Manually add 'flow' values
flow <- c(14,
            +           7.7,
            +           10.4,
            +           18.2,
            +           14.5,
            +           32,
            +           24.1,
            +           29.7,
            +           22,
            +           32.8,
            +           11.9,
            +           15,
            +           25)

abline(h=10, col= 'black')
abline(h=20, col= 'black')
abline(h=30, col= 'black')
abline(h=40, col= 'black')


#Create barplot
barplot(flow,x, abline(), space = 0, tck=1, tcl=-0.5, ylim=c(0,40), col = c("#E69F00","#E69F00", "#56B4E9", "#009E73","#009E73","#009E73","#009E73","#009E73","#009E73","#009E73","#009E73","#009E73","#009E73"), names.arg = fig402data.dat$well_ID, las=2, ylab = expression('Average Flow CO'[2] (scfm), cex.axis = 1, cex.names = 0.70))



barplot(flow,x,
        axes = F,
        #axTicks(1, axp = c(5,0,3)),
        space = 0,
        #tck = 1, tcl = -0.5,
        ylim = c(0,40),
        col = c("#E69F00","#E69F00", "#56B4E9", "#009E73","#009E73","#009E73","#009E73","#009E73","#009E73","#009E73","#009E73","#009E73","#009E73"),
        names.arg = fig402data.dat$well_ID,
        las = 2, 
        ylab = expression('Average Flow CO'[2] (scfm),
        cex.axis = 1, 
        cex.names = 0.70))
axis(side = 2, tick = c(5,15,25,35), line = -0.25, tck=-0.25, tcl = -0.5, at=c(seq(from=0,to=40,by=10)))
rug(x = 0:40 + 0, tick = c(5,15,25,35), ticksize = -0.025, side = 2)



#minor.tick(ny=10)
#axis(2, at = 0:10, labels = c("CL",1,2,3,4,5,6,7,8,9,"DL")) 


#USE
#axis(side = 2, line = -0.25, tck=-0.25, tcl = -0.5, at=c(seq(from=0,to=40,by=10)))
#axis(side = 2, line = -0.25, tck=-0.25, tcl = -0.5, at=c(seq(from=0,to=40,by=10)), tick =c(5,15,25,35))

#TRY
#tick.ratio=0.5
#?grid

#Add ablines
#abline(h=10, col= 'black')
#abline(h=20, col= 'black')
#abline(h=30, col= 'black')
#abline(h=40, col= 'black')

#Add Legend
legend("topright", legend=c("Phase 1", "Phase 2", "Phase 3"), fill=c("#E69F00","#56B4E9","#009E73"))





#END
#####################################################################################################################################################################################




#####TRY

#        panel.first = 
 #         abline(h=10, col= 'black', par("xpd"),
  #               abline(h=20, col= 'black'),
   #              abline(h=30, col= 'black'),
    #             abline(h=40, col= 'black'))

Sparging Well Average CO2 Flow in scfm

Upvotes: 0

Views: 476

Answers (1)

d.b
d.b

Reputation: 32548

I would recommend using ggplot. With barplot in base R, you'd first create an empty plot, add abline, and then actual barplot.

#Data
d = table(mtcars$cyl)
#Empty plot
b = barplot(d, col = NA, border = NA)
#Horizontal Lines
abline(h = 0:16, col = "grey")
#Actual barplot
barplot(d, add = TRUE)

Upvotes: 3

Related Questions