BelleT
BelleT

Reputation: 33

R: Barplot - How to remove a bar

Hi there I am new to R and I have produced a barplot, with the code below, however for the data set "Control" I do not have data for the ungrazed rate, therefore there is a line where the bar should be on the graph (see image below) but no data! Is there a way for me to get rid of that? Do I need to edit the original table of data on my txt file?

    barplot(xtabs(ana$Infiltration ~ ana$Grazing + ana$Burn ),beside = TRUE, col = c( "tan4", "darkgreen"), xlab = "Burn Treatment", names = c( "Control", "Long Rotation", "Burned 1954", "Short Rotation" ) , ylab = "Mean Infiltration Rate (mm/h) " , legend = c( "Grazed", "Ungrazed"), args.legend = list(title = "Graze Treatment", x = "topright", cex = .7), ylim = c(0, 40000)  ) 

barplot

Upvotes: 1

Views: 3169

Answers (1)

akrun
akrun

Reputation: 887163

The option would be to replace the 0 values from the xtabs to missing values i.e. NA

tbl <- xtabs(Infiltration ~ Grazing + Burn, data = ana )
is.na(tbl) <- tbl == 0

barplot(tbl,beside = TRUE, 
  col = c( "tan4", "darkgreen"), xlab = "Burn Treatment",
   names = c( "Control", "Long Rotation", "Burned 1954", "Short Rotation" ) , 
   ylab = "Mean Infiltration Rate (mm/h) " , 
   legend = c( "Grazed", "Ungrazed"),
   args.legend = list(title = "Graze Treatment", x = "topright",
             cex = .7), ylim = c(0, 20)  ) 

-output

Below, we have the one without the NA replacement on the left and the right with NA replacement

enter image description here

data

set.seed(24)
ana <- data.frame(Infiltration = sample(1:5, 20, replace = TRUE),
               Grazing = sample(c("Grazed", "Ungrazed"), 20, replace = TRUE),
               Burn = sample(c("Control", "Long Rotation", "Burned 1954", "Short Rotation"),
                   20, replace = TRUE), stringsAsFactors = TRUE)

Upvotes: 1

Related Questions