Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot如何用不同的颜色填充一个价格值_R_Ggplot2 - Fatal编程技术网

R ggplot如何用不同的颜色填充一个价格值

R ggplot如何用不同的颜色填充一个价格值,r,ggplot2,R,Ggplot2,我有这个密码 ggplot(xdata) + geom_histogram(aes(price),fill = 'orange',alpha = 0.85,binwidth = 15) + theme_minimal(base_size = 13) + xlab("Price") + ylab("Frequency") + ggtitle("The Distrub

我有这个密码

      ggplot(xdata) + 
           geom_histogram(aes(price),fill = 'orange',alpha = 0.85,binwidth = 15) + 
           theme_minimal(base_size = 13) + xlab("Price") + ylab("Frequency") + 
           ggtitle("The Distrubition of Price")+ xlim(0,5000)
结果是


我需要帮助将绘图中低于150美元的任何条形图转换为不同的颜色,需要帮助吗?

请使用
ggplot2::diamonds
数据集作为示例数据,这可以通过
ifelse
和使用
scale\u fill\u手册
实现,例如:

库(ggplot2)
ggplot(钻石)+
几何图形直方图(aes(价格,填充=ifelse(价格<1500,“低”、“高”)),alpha=0.85,binwidth=15+
刻度填充手动(数值=c(低=“钢蓝”,高=“橙色”))+
最小主题(基本大小=13)+xlab(“价格”)+ylab(“频率”)+
ggtitle(“价格分配”)+xlim(05000)
#>警告:已删除包含非有限值的14714行(stat_bin)。
#>警告:删除了包含缺失值的4行(几何图形栏)。

由(v0.3.0)于2020年12月31日创建
p <- ggplot(xdata)

p + 
  geom_histogram(aes(price, color = ifelse(price < 1500, "< $150", ">= $150")),
                 alpha = 0.85, binwidth = 15) + 
  scale_color_manual(values = c("< $150" = "green", ">= $150" = "orange")) + 
  theme_minimal(base_size = 13) + 
  xlab("Price") + 
  ylab("Frequency") + 
  ggtitle("The Distrubition of Price") + 
  xlim(0,5000)