1

I have water content data that starts at day 30, I would like the x axis at start at 30 when y=0.

I have tried this codes:

bp=barplot(height=ESR18R$Rainfall,names.arg=ESR18R$DAS,las=1,xaxt="n"
           ,ylim=c(0,80),col="grey",ylab="Daily rainfall (mm)")

axis(1, at = seq(30, 80, by = 10))

Unfortunately, this codes labels the x axis almost at the middle while I want it to start in the beginning of the x axis.

mt1022
  • 16,834
  • 5
  • 48
  • 71
Ann
  • 11
  • 2
  • Try with `scale_x_continuous(limits = c(30, 80))`. – novica Sep 09 '19 at 08:58
  • 1
    @novica That's not going to work. OP is using base R graphics, `scale_x_continuous` is for `ggplot`s. – Maurits Evers Sep 09 '19 at 09:31
  • @Ann Please include a small reproducible sample dataset (e.g. using `dput`). For more details, take a look at how to provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Maurits Evers Sep 09 '19 at 09:32

1 Answers1

0

You need to use the offset option of barplot. The offeset determines how much the bars should be shifted relative to the x axis. For a correct visualization of the height of the bars, height needs to be reduced of the same amount of offset.

# Generate data
set.seed(12345)
ESR18R <- data.frame(Rainfall=30+runif(6)*40,
                     DAS=letters[1:6])
print(ESR18R)
#   Rainfall DAS
# 1 58.83616   a
# 2 65.03093   b
# 3 60.43929   c
# 4 65.44498   d
# 5 48.25924   e
# 6 36.65487   f

bp <- barplot(height=ESR18R$Rainfall-30, xaxt="n", yaxt="n",
              ylim=c(30,80), col="grey", ylab="Daily rainfall (mm)", 
              offset=30)
axis(1, at=bp, labels=ESR18R$DAS)
axis(2, at=seq(30, 80, by=10), las=1)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • Thank you guys for your suggestions, @Marco, thanks, my y axis is the rainfall, that starts at 0-80, that works well. My problem is the X axis, because I started measurements at day 30, so I want the X to start from 30 and not 0. I am very new in r, trying to learn a lot of things – Ann Sep 10 '19 at 11:46
  • @Ann Please, could you share the output of `dput(ESR18R)` ? You can edit your post and add the output. – Marco Sandri Sep 10 '19 at 13:18