rggplot2bar-chartx-axis

X-axis in ggplot2 is showing numeric values in hundreds instead of tens


I am creating a bar chart in ggplot2, but my x-axis is showing the amount of minutes in hundreds instead of tens. This is my code.

ggplot(data=daily_sleep_diff, aes(x=TimeToFallAsleep))+geom_bar()

This is a sample of my dataset. sample dataset

And this is the chart it shows. Notice the numeric values on the x-axis. bar chart

I'm confused as to why this is occurring, as I'd think it would just show the numeric values within the column of TimeToFallAsleep, but I am a noob in R, so there's still a lot for me to learn.


Solution

  • I suspect you have a few large values in your dataset and the x axis breaks are being simplified per the first example.

    You could adjust the breaks per the second example using scale_x_continuous().

    library(tidyverse)
    
    tibble(TimeToFallAsleep = c(37, 32, 20, 34, 19, 300)) |> 
      ggplot(aes(TimeToFallAsleep)) +
      geom_bar()
    

    
    tibble(TimeToFallAsleep = c(37, 32, 20, 34, 19, 300)) |> 
      ggplot(aes(TimeToFallAsleep)) +
      geom_bar() +
      scale_x_continuous(breaks = seq(20, 300, 20))
    

    Created on 2024-04-01 with reprex v2.1.0