user3378320
user3378320

Reputation: 47

how to define x axis at 1 instead of 0

I have a ggplot with 7 Million values on x axis. I am trying to use scale from 1 to 7M with break at every 1 million. When I use the following code my scale never starts at 1.It will be always from 0. My first value has a index of 1. I have tried using xlim, scale_x_continuous, coord_cartesian and expand_limits. All gives x axis scale starting at 0.

Please give me suggestion.

scales::demo_continuous(c(1, 7000000), breaks = scales::breaks_width(1000000), labels=scales::label_number_si()) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))

enter image description here

Upvotes: 3

Views: 774

Answers (1)

denis
denis

Reputation: 5673

scale_x_continuous does the trick of you precise the breaks, the labels and the limits:

df <- data.frame( x = sample(1:7000000,1000) )
df$y <- df$x


ggplot(df,aes(x,y))+
  geom_point()+
  scale_x_continuous(breaks = seq(1e6,7e6,1e6),
                     labels = paste0(1:7,"M") ,
                     limits = c(1e6,7e6))

enter image description here

Upvotes: 1

Related Questions