1

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.

1
  • 1
    Did you check if max(daily_sleep_diff[,7]) has values in hundreds? Commented Apr 1, 2024 at 13:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

You could also consider, for example, coord_cartesian(xlim = c(0, 50)) to "zoom in" on the smaller values. Or scale_x_log10() to spread out the smaller values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.