How can I rename the xlabel for this histogram?
Jaffet Leon
I'm making a histogram for a database about household income and education level and everything is fine, but when I try to rename the x labels, I simply can't:
cces$faminc_new1 = recode(cces$faminc_new,
"1" = "Less than $10,000",
"2" = "$10000 - $19,999",
"3" = "$20000 - $29,999",
"4" = "$30000 - $39,999",
"5" = "$40000 - $49,999",
"6" = "$50000 - $59,999",
"7" = "$60000 - $69,999",
"8" = "$70000 - $79,999",
"9" = "$80000 - $99,999",
"10"= "$100000 - $119,999",
"11"= "$120000 - $149,999",
"12"= "$150000 - $199,999",
"13"= "$200000 - $249,999",
"14"= "$250000 - $349,999",
"15"= "$350000 - $499,999",
"16"= "$500000 or more")
cces$educ1 = recode(cces$educ,
"1" = "1 No HS",
"2" = "2 HS graduate",
"3" = "3 Some college",
"4" = "4 2-year",
"5" = "5 4-year",
"6" = "6 Post-grad")
But I can't rename the x tag for this history:
cces %>% ggplot(aes(x= faminc_new, colour = educ1, fill = educ1)) +
geom_histogram() + scale_x_discrete(labels = c("1" = "Less than $10,000",
"2" = "20k",
"3" = "30k",
"4" = "40k",
"5" = "50",
"6" = "60",
"7" = "70k",
"8" = "80k",
"9" = "100k",
"10"= "120k",
"11"= "150k",
"12"= "200k",
"13"= "250k",
"14"= "350k",
"15"= "500k",
"16"= ">500k"))
faminc_new and educ are continuous, while faminc_new1 and educ1 are discrete.
How can I place the labels from faminc_new1 onto the x-labels of the histogram?
Su Ren
It appears that the expected result is a bar graph. So one option is to use geom_bar
.
When using the histogram, there is an automatic break. So adding new tags manually won't work. If you want to stick with geom_histogram
it, you must specify an interrupt.
Do the following after the introduction break.
mtcars %>% ggplot(aes(x= cyl, y= gear, colour = gear)) +
geom_bar(stat = "identity") +
scale_x_continuous(labels = c("40k", "60k", "80k"), breaks = c(4, 6, 8))
mtcars %>% ggplot(aes(x = cyl, y = gear, colour = gear, fill = gear)) +
geom_histogram(stat = "identity") +
scale_x_continuous(labels = c("40k", "60k", "80k"), breaks = c(4, 6, 8))