简体   繁体   中英

Label x-axis nearest to thousand in ggplot

I want to use scale_x_continuous and round my labels to nearest thousand. For example if my X axis are 1234567,2345566.... I want it to be 1235,2346.I have used sequencing to split my x-axis in deciles. When I am rounding it to thousands and then dividing by 1000 I will show it by example. Its making 1235000 and after dividing its becoming 1235 which I want but since sclae_x_continuous observes that all the numbers till 3million(which is now 3k) comes in the first decile its printing all the labels at the same place. The code I am using is:

ggplot(
  data = cummulative, 
  aes(
    x = seq(1,length(cummulative$Frequency)),
    y = Cum.Percent.,
    group = 1
  )
) + 
geom_line(colour="red", size=1) + 
theme_classic() + 
theme(axis.text.x = element_text(angle = 75, hjust = 1, size=8)) + 
labs(x="Number of Customer (in thousands)",y="Product Share (%)") +
ggtitle("Pareto Chart") +ylim(0,100.1) +
(scale_x_continuous(
  breaks = seq(
    0, 
    length(cummulative$Frequency), 
    length(cummulative$Frequency)/10
  )
))    

Please also find the image here: 在此处输入图片说明

Thanks a lot guys :)

Let ggplot handle the breaks instead of specifying them manually. You'll get a nice interval. Dividing X a priori by 1000 will get you very close to the solution.

Provide a reproducible example if you want a better solution.

ggplot(
  data = cummulative, 
  aes(
    x = seq_along(Frequency) / 1000,
    y = Cum.Percent.
  )
) + 
geom_line(colour="red", size=1) + 
theme_classic() + 
theme(axis.text.x = element_text(angle = 75, hjust = 1, size=8)) + 
labs(
  x = "Number of Customer (in thousands)",
  y = "Product Share (%)"
) +
ggtitle("Pareto Chart") +
ylim(0, 100.1) +
scale_x_continuous()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM