![](/img/trans.png)
[英]How to plot two histograms of different variables in one GGPlot, with legend and colours
[英]How can I plot histograms of all variables but color by one
我想绘制数据框中所有变量的直方图,但是我想用变量之一对图进行着色(或填充)。 我怎样才能做到这一点?
此链接显示了如何绘制所有变量的直方图,但未显示如何对其进行着色。 相反,我可以为一个变量绘制直方图,并使用下面的代码对其进行着色,但是然后我必须一一绘制。
ggplot(mtcars, aes(mpg, color=factor(vs))) + geom_histogram()
如果您需要根据其中一列为所有facets
着色,这就是您要寻找的内容:
library(tidyverse)
ggplot(gather(mtcars, key, value, -c(vs)), aes(value, fill=factor(vs))) +
geom_histogram(bins = 10) +
facet_wrap(~key, scales = 'free_x')
如果您希望基于key
为每个facet
使用不同的颜色,则可以这样做:
library(tidyverse)
ggplot(gather(mtcars), aes(value, fill=key)) +
geom_histogram(bins = 10) +
facet_wrap(~key, scales = 'free_x')
由reprex软件包 (v0.3.0)创建于2019-06-18
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.