Fill function in ggplot2 package not working when constructing a histogram

I'm following the Edureka! tutorial on using ggplot2. The csv file that I'm currently using is called House. The problem that I encountered tonight is creating a histogram that involves using the variable "air_cond" inside the fill function. Here's my code:
ggplot(house, aes(x =price, fill = aircond))+geom_histogram()

What it's suppose to do is to create a histogram that is made up of colors for the variable "air_cond". Instead, it just gave me a histogram made up gray bars. I don't know if it's a problem with my computer or the package itself. Any help is appreciated!

Does this work for you?

house <- data.frame(price = c(rnorm(100,10000,1000), rnorm(100,13000, 1200)),
                    aircond = c(rep("AC", 100), rep("NotAC", 100)))  
ggplot(house, aes(x =price, fill = aircond))+geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2020-03-18 by the reprex package (v0.3.0)

It will be helpful if you can post the structure of your data frame i.e. house, using the str(), head() or glimpse() functions.

@FJCC's solution highlighted in response to your questions should work in your case.

Okay, I found the problem now. It appears that the structure of aircond is in binary numbers. Like 0 represents no air condition and 1 means there is air condition. (e.g., 0=no; 1=yes). The problem now is to convert the binary numbers back to characters. How can I change the numbers back to characters? Again, any help is appreciated as I'm slowly learning R on my own!

To convert the variable from binary to character class you can do something like this.

house$aircond <- as.character(house$aircond) # This will convert the boolean variables i.e. 0 and 1 into character variables.

I hope this addresses what you are looking for.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.