0

I would like to rename the categories of "income" from 1,2,3,4,5 to the real values of the income in the plot. I tried this code but it does not work. Can somebody please explain me why?

ggplot(data=subset(trips_renamed,income!="99")) + 
    geom_bar(mapping = aes(x = income,fill="income"))+
    scale_x_discrete(labels=c("<=4000","4001-8000","8001-12000","12001- 
    16000",">16000",position="bottom"))+
    labs(y= "Total number of trips", x="Income Classes")+
    theme(legend.position = "none")
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 3
    Welcome to SO, MarcoManinetti! It's hard to test this without sample data, please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info for suggestions. Possibilities: (1) [edit] your question and paste the output from `dput(x)`, where `x` is just enough data to get the point across, no unused columns please; (2) use a public dataset, such as `mtcars`, `iris`, or `ggplot2::diamonds`. Also, while [tag:ggplot2] is apparent here, in general please be explicit with non-base packages you're using. Thanks! – r2evans Oct 08 '21 at 17:16

2 Answers2

1

It would be much easier to find and test an answer if you provided a minimal reproducible example. However, below is shown how to change the scale for a similar plot as in your question. Since the values for x are numeric we need to use the (somewhat counterintuitive) scale_x_continuous to change the labels on the fly

library(ggplot2)
ggplot(data=mtcars) + 
  geom_bar(aes(x = gear))+
  scale_x_continuous(breaks = 3:5,  labels=c("<4", "4-4.9",">4"))

Returns:

enter image description here

dario
  • 6,415
  • 2
  • 12
  • 26
  • Thank you really much! Sorry for my ignorance, but what do you mean by minimal reproducible example? – Marco Maninetti Oct 09 '21 at 17:17
  • @MarcoManinetti r2evans gave you a helpful comment with some important links about minimal reproducible example and other info on how to ask questions here. It is always better to read comments others give you, since they took their time to help you/give you useful tips. Not reading them could be considered kind of rude – dario Oct 10 '21 at 15:49
1

It seems your issue has to do with trips_renamed$income being a class "integer" or "numeric". As such, scale_x_discrete() should be replaced with scale_x_continuous(). You can either use scale_x_continuous() or convert to a discrete value (factor), then use scale_x_discrete(). Here are two examples using the following dummy dataset.

set.seed(8675309)
df <- data.frame(income=sample(1:5, 1000, replace=T))

Option 1 : Relabel your continuous axis

If class(trips_renamed$income) is "numeric" or "integer", then you will need to use scale_x_continuous(). Relabeling requires you to specify both breaks= and labels= arguments, and they have to be the same length. This should work:

ggplot(df, aes(x=income)) + geom_bar() +
  scale_x_continuous(breaks=1:5, labels=c("<=4000","4001-8000","8001-12000","12001- 
    16000",">16000"),position="bottom")

enter image description here

Option 2 : Convert to Factor and use Discrete Scale

The other option is to convert to a factor first, then use scale_x_discrete(). Here, you don't need the breaks= argument (the levels of the factor are used):

df$income <- factor(df$income)
ggplot(df, aes(x=income)) + geom_bar() +
  scale_x_discrete(labels=c("<=4000","4001-8000","8001-12000","12001- 
    16000",">16000"),position="bottom")

You get the same plot as above.

Option 2a: Factor and define labels together

If you want to get really crafty, you can define the labels the same time as the factor and they will be used for the axis labels instead of the name of the levels:

df2 <- df

df2$income <- factor(df2$income, labels=c("<=4000","4001-8000","8001-12000","12001- 
    16000",">16000"))

ggplot(df2, aes(x=income)) + geom_bar()

This together should give you a good idea of how ggplot2 works when choosing how to label the axes.

chemdork123
  • 12,369
  • 2
  • 16
  • 32