rplotvisualizationplotrix

Add units to Y axes


I'm using plotrix.twoord to compare two plots in a single graph using this code:

library(plotrix)

mempool<-read.csv("mempool-size.csv")
mempool$date <- as.Date(mempool$date)
fees<-read.csv("transaction-fees.csv")
fees$date <- as.Date(fees$date)

twoord.plot(mempool$date,mempool$value,fees$date,fees$value,type="b",
            ylab="Unconfirmed transactions (B)",rylab="Fees paid per day (BTC)",
            main="Fees and mempool size since 2017",
            )

This prints a graph like this:

Plot

This looks almost right but the left axis looks a bit odd. I would like it to show the unit (B) after the values - preferably with metric prefixes instead of scientific notation (20MB instead of 2e07B).

I'm not sure if plotrix.twoord provides an option like this, so I tried to use ggplot instead:

ggplot() + 
  geom_line(data=fees, aes(x=date, y=value), color='black') + 
  geom_line(data=mempool, aes(x=date, y=value), color='red')

But now the fees line is just flat because it doesn't have it's own axis.

You can find the data here if you want to try it yourself: http://www.sharecsv.com/s/07076796191d9e414e5b50840d9ed7ad/mempool-size.csv http://www.sharecsv.com/s/72a183dd3ab0df781eacbfee81999548/transaction-fees.csv


Solution

  • Plotrix does not seem to support self provided tick mark labels, there are a few ways around this.

    1) The easiest way is to divide the left axis values by 10^6

    mempool$value <- mempool$value / 1000000
    twoord.plot(mempool$date,mempool$value,fees$date,fees$value,type="b",
                ylab="Unconfirmed transactions (MB)",rylab="Fees paid per day (BTC)",
                main="Fees and mempool size since 2017",
                )
    

    2) You could use base R without plotrix, e.g.

    plot(mempool$date,mempool$value, type='b',ylab="Unconfirmed transactions", yaxt='n',xlab='Date')
    axis(2, c('0MB', '20MB', '40MB', '60MB', '80MB', '100MB', '120MB', '140MB'), at=seq(0, 140*10^6, length.out=8))
    par(new=TRUE)
    plot(fees$date,fees$value,type="b",col="red",xaxt="n",yaxt="n",xlab="", ylab='')
    axis(4)
    mtext("Fees paid",side=4,line=3)
    

    3) You could hack the twoord.plot function to remove one of the axes, and then just add it afterwards similar to option 2) above. For details of this, see Handling axis with dates in twoord.plot (remove axis 3)