ggplot2中直方图的颜色

时间:2015-07-24 19:32:02

标签: r ggplot2 histogram

我正在以下列方式绘制数据框的直方图,但我想将其颜色设置为蓝色/其他任何颜色。但是当我放color = "blue"时,它不会改变颜色并在侧面添加额外的组。

> str(neg.all.frame)
'data.frame':   455634 obs. of  2 variables:
 $ probability: num  0.645 0.536 0.365 0.523 0.587 ...
 $ group      : chr  "Unknown/Negative" "Unknown/Negative" "Unknown/Negative" "Unknown/Negative" ...

ggplot(neg.all.frame, aes(colour = "blue", probability, fill = group)) + geom_histogram(alpha = 0.2) +
  xlab("Probability of Being Interested in Fashion") + ylab("Number of People")

enter image description here

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您正在传递字符串"blue"作为美学映射。您需要做的是在fill=blue中使用geom_histogram作为参数。试试这个:

ggplot(neg.all.frame, aes(probability, fill = group)) +    
 geom_histogram(alpha = 0.2, fill = "blue") + 
 xlab("Probability of Being Interested in Fashion") + 
 ylab("Number of People")

在图形语法中,美学映射将数据映射到可视化的美学特征。你要做的不是映射,而是静态的颜色变化。