creating mirrored barplots with distinct scales in ggplot2 in R

前端 未结 1 1555
无人共我
无人共我 2021-01-17 01:31

I have a follow up to this nice question:

How do you create a bar plot for two variables mirrored across the x-axis in R?

these answers show how to make mirr

相关标签:
1条回答
  • 2021-01-17 02:21

    You can just change the label using scale_y_continuous():

    library(ggplot2)
    
    dat <- data.frame(
      group = rep(c("Above", "Below"), each=10),
      x = rep(1:10, 2),
      y = c(runif(20, 0, 100))
    )
    
    dat$y[dat$group=="Below"] <- -dat$y[dat$group=="Below"]
    
    ggplot(dat, aes(x=x, y=y, fill=group)) + 
      geom_bar(stat="identity", position="identity") + 
      scale_y_continuous(breaks=seq(-100,100,by=50),labels=abs(seq(-100,100,by=50)))
    

    If you don't like 50, you can always just change by.

    0 讨论(0)
提交回复
热议问题