本文介绍了直方图条件填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想制作一个柱状图,其中填充颜色根据垃圾箱的低端而变化。我不想与第二个geom_histogram(),但不成功。 最简单的方法是在条件中添加另一列并更新 aes 包含填充组。 cust $ high_rev 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(标签=逗号)+ xlab(客户收入)+ ylab(客户数量)+ ggtitle(客户价值分布) 如果您对某些特定颜色设置了心脏,您可以使用 scale_fill_manual 函数。这是一个有趣的明亮颜色的例子。 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(客户收入)+ ylab(客户数量)+ ggtitle(Customer Value ) I would like to make a histogram where the fill color changes depending on the low end of the bin. I do not want a manual fill. This answer seems promising, but I could not transition it successfully to a histogram and two-value (not gradient) color scheme. I believe the solution may be some ifelse logic within geom_histogram(fill= ) but I don't know how to access the bin starting value.For example, in the below histogram, I would like to color revenue bins over $100,000 red to show the high-revenue customers.library(ggplot2)library(scales)n <- 10000cust <- data.frame(cust_id=1:n,cust_rev <- rexp(n,.00001))# I want to use a log scale for my tick marks and bin breakspowers <- 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")Also, I attempted a workaround with a 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 解决方案 It would be easiest to just add another column with the condition and update the 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")If you have your heart set on some specific colors you can use the scale_fill_manual function. Here is an example with some fun bright 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") 这篇关于直方图条件填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 03:17
查看更多