Histogram fill color

I would like to make a histogram where the fill color changes depending on the lower end of the hopper. I do not want to fill in the manual . This answer seems promising, but I could not successfully translate it into a histogram and a two-digit (non-gradient) color scheme. I believe the solution may be some ifelse logic in geom_histogram(fill= ) , but I don't know how to access the starting bin value.

For example, in the histogram below, I would like to color banknotes worth more than $ 100,000 to show high-income customers.

 library(ggplot2) library(scales) n <- 10000 cust <- data.frame(cust_id=1:n,cust_rev <- rexp(n,.00001)) # I want to use a log scale for my tick marks and bin breaks powers <- function(base,exp) sapply(1:exp, function(exp) base^exp ) ggplot(cust, aes(cust_rev)) + geom_histogram(color="black",fill="light blue", binwidth=1/3) + scale_x_log10(labels=comma, breaks=powers(10,8)) + scale_y_continuous(labels=comma) + xlab("Customer Revenue") + ylab("Number of Customers") + ggtitle("Distribution of Customer Value") 

enter image description here

In addition, I tried to make a workaround with the second geom_histogram (), but was unsuccessful.

 ggplot(cust, aes(x=cust_rev)) + geom_histogram(color="black",fill="light blue", binwidth=1/3) + geom_histogram(data=subset(cust,cust_rev>100000), color="black",fill="red", binwidth=1/3) + scale_x_log10(labels=comma, breaks=powers(10,8)) + scale_y_continuous(labels=comma) + xlab("Customer Revenue ($)") + ylab("Number of Customers") + ggtitle("Distribution of Customer Value") # Error in data.frame(x = c(45291.1377418786, 52770.7004919648, 15748.975193128, # : arguments imply differing number of rows: 10000, 3568 
+6
source share
2 answers

It would be easier to just add another conditional column and update aes to include the fill group.

 cust$high_rev <- as.factor((cust[,2]>100000)*1) ggplot(cust, aes(cust_rev, fill=high_rev)) + geom_histogram(color="black", binwidth=1/3) + scale_x_log10(labels=comma, breaks=powers(10,8)) + scale_y_continuous(labels=comma) + xlab("Customer Revenue") + ylab("Number of Customers") + ggtitle("Distribution of Customer Value") 

enter image description here

If your heart is set to specific colors, you can use the scale_fill_manual function. Here is an example with some bright vibrant colors.

 ggplot(cust, aes(cust_rev, fill=high_rev)) + geom_histogram(color="black", binwidth=1/3) + scale_x_log10(labels=comma, breaks=powers(10,8)) + scale_y_continuous(labels=comma) + scale_fill_manual(values = c("green", "purple")) + xlab("Customer Revenue") + ylab("Number of Customers") + ggtitle("Distribution of Customer Value") 

enter image description here

+11
source

How about this?

 ggplot(cust, aes(cust_rev)) + geom_histogram(aes(fill=cust_rev > 100000),binwidth=1/3) + scale_x_log10() 

or equivalent

 ggplot(cust, aes(x=cust_rev,fill=cust_rev > 100000)) + geom_histogram(binwidth=1/3) + scale_x_log10() 
+3
source

Source: https://habr.com/ru/post/981981/


All Articles