R bar plot: how to set specified number in y axis but do not change the distance between numbers in R

Viewed 28

I want to customized the y axis to a certain number, but the distance in the y axis should not be changed accordingly like below figure. I tried different way, but failed. Basically, when I use the axis(2, yseries), the y range will be changed.

The core I tried is like this:

yseries <- c(10,
             20,
             40,
             80,
             160,
             320,
             640,
             1280)

gm <- c(760, 640)
barplot(gm, xaxt = "n", yaxt = "n")
# Y-axis
axis(2, at = yseries)

You can see the y axis is different from the attached figure (where the y ranges from 10 to 1280, but the distance in the y axis is not changed in this figure. I also want to make this kind of figure). Can you help me to figure out?

Thanks,

This is the figure I want to replicate it in y axis

1 Answers

You can set the range of the y axis in the plot command, and use a log scale to get equal distances between multiples as follows:

barplot(gm, xaxt = "n", yaxt = "n", log="y", ylim = range(yseries))
axis(2, at = yseries,las=1)

enter image description here

As requested in the comments - a ggplot2 solution. This is a little tricky because using bar plots with log-scales isn't very natural. The 'zero' disappears on the log-scale, so trying to set the limits within the scale function makes the columns disappear. I use coord_cartesian to set the limits instead (data is different to previous plot):

ggplot(df, aes(x, y)) + 
     geom_col() + 
     scale_y_log10(breaks=yseries)+
     coord_cartesian(ylim=range(yseries))

enter image description here

Related