2

For the following barplot in R:

bp <- barplot(df$share.work, 
              names.arg=df
              space = c(0, diff(df$dist.km.work)),
              col="blue", 
              xlab="Kilometer", 
              ylab="Trip Frequency",
              ylim=c(0,.25), las=1 )

enter image description here

Q1: How can I add an x-axis without increasing the bars' width?

Q2: How can I make the title of the y-axis horizontal?

data:

  df<- structure(list(year = c(2016L, 2016L, 2016L, 2016L, 2016L, 2016L
    ), km = structure(1:6, .Label = c("km1", "km3", "km5", "km7", 
    "km9", "km15", "km20", "km25", "km30", "km35", "km42.5"), class = "factor"), 
        dist.km.work = c(0.5, 2, 4, 6, 8.5, 12.5), trips.work = c(14725L, 
        46730L, 58500L, 59710L, 87060L, 117535L), total.trips.work = c(476835L, 
        476835L, 476835L, 476835L, 476835L, 476835L), share.work = c(0.0308, 
        0.098, 0.12, 0.125, 
        0.18, 0.246)), row.names = c(NA, 
    -6L), class = c("tbl_df", "tbl", "data.frame"))
Hossein
  • 461
  • 3
  • 9
  • 17

1 Answers1

1

A1: You can use axis.lty=1 to add the x-axis.

A2: Comment out ylab="Trip Frequency". After barplot, use
mtext(2, text = "Trip Frequency", line = 2, las = 1). You can (horizontally) move the title by changing line and you can rotate the title by changing las.

So, the code becomes

bp <- barplot(df$share.work, 
              names.arg=df,
              space = c(0, diff(df$dist.km.work)),
              col="blue", 
              xlab="Kilometer", 
              #ylab="Trip Frequency",
              ylim=c(0,.25),
              las=1,
              axis.lty=1)

mtext(2, text = "Trip Frequency", line = 2, las = 1)
Alp Aribal
  • 370
  • 1
  • 11
  • Thanks @Alp Arıbal! Just for the second question, the `line=2` does not do the rotation. How can I rotate the axis title using `mtext`? – Hossein Nov 14 '18 at 21:37
  • You add `las = 1` in `mtext`. I also edited my answer. – Alp Aribal Nov 14 '18 at 23:09
  • You might also want to adjust the margins accordingly. See `mar` under `?par`. Before your plot, you can set this parameter by `par(mar=c(5,8,2,2))`. – Alp Aribal Nov 14 '18 at 23:17