【发布时间】:2020-05-17 11:09:05
【问题描述】:
我想绘制一个变量 x1 的直方图,通过另一个变量 x2 的值将数据细分为 4,并将 4 个直方图放在 1 个绘图区域中,每行 2 个直方图。
例如,
library(tidyverse)
ggplot(filter(mpg, cty < 15), aes(x = displ)) + geom_histogram(binwidth = 0.2)
ggplot(filter(mpg, cty == c(15,16,17,18)), aes(x = displ)) + geom_histogram(binwidth = 0.05)
ggplot(filter(mpg, cty == c(19,20,21,22)), aes(x = displ)) + geom_histogram(binwidth = 0.05)
ggplot(filter(mpg, cty > 23), aes(x = displ)) + geom_histogram(binwidth = 0.1)
感谢您的帮助!
【问题讨论】:
-
最好使用
%in%,即cty %in% c(15,16,17,18)。检查1:2 == c(0, 1, 3, 4, 5)