0

enter image description here

Hi, I am trying the plot with R, you can see the values of x Axis are factors. And how to put the values of $500, $1000,$2000,$5000,$10000 and $20000,$50000,and $100000 as the values of X-axis?

Thank you!

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically after `set.seed(1)`), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jun 20 '20 at 19:29
  • Looking at the plot, though, why are you ***explicitly*** converting what might be a continuous (or integral) variable named `AverageGDP` into a `factor`? I'd search for `as.factor(AverageGDP)` in your code and just use `AverageGDP` or, if necessary, `as.numeric(gsub("[^-.0-9]", "", AverageGDP))`. – r2evans Jun 20 '20 at 19:31
  • The values of the X-axis should be $500, $1000,$2000,$5000,$10,000, and $20,000 which gaps are totally different, but these values are required to be at equal distance on X-axis. So, I transferred these values to factors instead of numeric values. But, currently, I can not use scales_x_discrete to create the values anymore, as the values are no longer numeric, do you know how to create the values and labels for X-axis if the values are factors? Thank you! – codejourney Jun 20 '20 at 21:01
  • What you *show* on the x-axis is different than the type of value underneath. If you want more help, please make this question reproducible by giving sample data (or perhaps using `data("gapminder", package="gapminder")` here) and code used to generate this. Otherwise, base graphics as `axis` and `ggplot2` has `scale_x_continuous` for where to place `breaks=` and how to format them with `labels=`. – r2evans Jun 20 '20 at 21:15
  • `scale_x_discrete` works perfectly well with `factor`, see `ggplot(iris, aes(Species, Petal.Width)) + geom_point() + scale_x_discrete(labels = letters[1:3])`. If you really must use `factor`s (I'm not convinced it's necessary or even helpful), then you can definitely use `scale_x_discrete`. – r2evans Jun 20 '20 at 21:18
  • Hi Thanks for your prompt reply! I tried to use gg3+scale_x_discrete(breaks=c(500, 1000, 2000,5000,10000,20000),labels=c("$500", "$1000", "$2000","$5000","$10000","$20000")), but eventually, there is no any label on the X-axis. Is there any problems with my code? – codejourney Jun 20 '20 at 21:27
  • The original graph of world bank also used the factor for the plot, and I think the main reason is for greater clarity of visualization – codejourney Jun 20 '20 at 21:37
  • (1) Comments are horrible places for code, especially when it is relevant to the question. Please [edit] your question and place it there inside a [code block](https://stackoverflow.com/editing-help). (2) Please provide sample data or use a well-known dataset. (Please see suggestions on how to frame a question well to familiar answers: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jun 20 '20 at 22:07
  • Hi, I am still struggling with how to put the label for X-Axis. Could you advise which way I can get further help? I can not figure it out by myself. Thank you! – codejourney Jul 16 '20 at 21:04
  • I don't know how to help any further. Unless and until you can provide something reproducible, I don't think there is any way to help. – r2evans Jul 16 '20 at 21:47
  • So how to provide the sample. Where to provide? – codejourney Jul 16 '20 at 22:16
  • ??? ... *"how to provide"* and "*where to provide"* were both mentioned in [a previous comment](https://stackoverflow.com/questions/62490640/how-to-show-the-values-of-x-axis-in-r-if-x-value-is-the-factor?noredirect=1#comment110517172_62490640). – r2evans Jul 16 '20 at 22:38

0 Answers0