p = ggplot(mpg, aes(class, hwy))
p + geom_boxplot(aes(colour = drv))
看起来像这样:
我想制作一个非常相似的情节,但是(yearmon
格式化的)日期中示例中的class
变量,以及drv
所在的因子变量例。
以下是一些示例数据:
df_box = data_frame(
Date = sample(
as.yearmon(seq.Date(from = as.Date("2013-01-01"), to = as.Date("2016-08-01"), by = "month")),
size = 10000,
replace = TRUE
),
Source = sample(c("Inside", "Outside"), size = 10000, replace = TRUE),
Value = rnorm(10000)
)
我尝试了很多不同的东西:
在日期变量周围放置as.factor
,然后我不再具有x轴的间隔很好的日期刻度:
df_box %>%
ggplot(aes(
x = as.factor(Date),
y = Value,
# group = Date,
color = Source
)) +
geom_boxplot(outlier.shape = NA) +
theme_bw() +
xlab("Month Year") +
theme(
axis.text.x = element_text(hjust = 1, angle = 50)
)
另一方面,如果我使用Date
作为建议here的其他group
变量,则添加color
不再会产生任何其他影响:< / p>
df_box %>%
ggplot(aes(
x = Date,
y = Value,
group = Date,
color = Source
)) +
geom_boxplot() +
theme_bw()
关于如何在保持yearmon
刻度x轴的同时实现#1输出的任何想法?