How can I change the Y-axis figures into percentages in a barplot?

前端 未结 4 1571
清歌不尽
清歌不尽 2020-11-28 21:50

How can we change y axis to percent like the figure? I can change y axis range but I can\'t make it to percent. \"enter

4条回答
  •  囚心锁ツ
    2020-11-28 22:08

    In principle, you can pass any reformatting function to the labels parameter:

    + scale_y_continuous(labels = function(x) paste0(x*100, "%")) # Multiply by 100 & add %  
    

    Or

    + scale_y_continuous(labels = function(x) paste0(x, "%")) # Add percent sign 
    

    Reproducible example:

    library(ggplot2)
    df = data.frame(x=seq(0,1,0.1), y=seq(0,1,0.1))
    
    ggplot(df, aes(x,y)) + 
      geom_point() +
      scale_y_continuous(labels = function(x) paste0(x*100, "%"))
    

提交回复
热议问题