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")
CodePudding user response:
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:
CodePudding user response:
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")
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.