Number formatting axis labels in ggplot2?

前端 未结 3 626
后悔当初
后悔当初 2021-02-03 17:41

I\'m plotting a fairly simple chart using ggplot2 0.9.1.

x <- rnorm(100, mean=100, sd = 1) * 1000000
y <- rnorm(100, mean=100, sd = 1) * 1000000
df <- d         


        
3条回答
  •  深忆病人
    2021-02-03 18:04

    Here is an example of how to add commas and decimals to ggplot using scales::comma_format().

    Essentially allowing for a prettyNum() style of formatting.

    Seatbelts_df <- as.data.frame(Seatbelts)
    
    ggplot(data=Seatbelts_df, aes(x=Seatbelts_df$drivers, y=Seatbelts_df$DriversKilled, color=factor(Seatbelts_df$law))) +
      geom_jitter(alpha=0.5) +
      theme(plot.title=element_text(face="bold")) +
      labs(title="Amount of Drivers on Road vs Amount of deaths", subtitle = "Dataset from package datasets::Seatbelts", x ="Drivers on Road", y="Amount of Deaths", color="Seatbelt Law?") +
      scale_color_manual(labels = c("Yes", "No"), values = c("blue", "red")) +
      geom_vline(aes(xintercept=mean(Seatbelts_df$drivers)), color="black", linetype="dashed", size=1) +
      scale_x_continuous(
      labels = scales::comma_format(big.mark = ',',
                                     decimal.mark = '.'))
    

提交回复
热议问题