r - ggplot - 我可以绘制每个方面的所有数据,同时分割另一个变量吗?
问题描述
我正在尝试创建一个按帐户类型和年份划分的帐户图表,但我希望所有数据都在每个方面,但如果不是我想要集中的部分,则只是在后台。
使用下面的虚拟数据,如果我只想按帐户类型随着时间的推移进行帐户,以显示我希望它显示的内容。
df1 <- data.frame(acc_type=rep(c("cash", "credit"), each=10),
year = as.factor(rep(2019, each = 20)),
x_date=seq(10),
accounts=runif(20, 1000, 10000))
df1$acc_type2 <- df1$acc_type
ggplot(df1, aes(x_date,accounts, group=acc_type)) +
geom_line(data=df1[,3:5], aes(x=x_date, y=accounts, group=acc_type2), colour="grey") +
geom_line() +
facet_wrap(~ acc_type, nrow = 2)
然而,当我介绍年份时,数据有点搞笑
df2 <- data.frame(acc_type=rep(c("cash", "credit"), each=10),
year = as.factor(rep(2020, each = 20)),
x_date=seq(10),
accounts=runif(20, 1000, 10000))
df2$acc_type2 <- df2$acc_type
df3 <- rbind(df1, df2)
df3$year2 <- df3$year
ggplot(df3, aes(x_date,accounts, group=acc_type,colour= year)) +
geom_line(data=df3[,-(1:2)], aes(x=x_date, y=accounts, group=acc_type2, colour = year2), colour="grey") +
geom_line() +
facet_wrap(~ acc_type, nrow = 2)
然后数据看起来没有被正确分组,任何关于如何正确分组并将对背景不那么重要的数据的帮助将不胜感激。
解决方案
推荐阅读
- algorithm - 计算半铰接卡车的位移坐标
- sql-server - 基于“案例”语句隐藏行
- c# - 为什么关闭标签显示在视图中?
- google-colaboratory - 如何在谷歌 Colab 中渲染 OpenAI 健身房?
- javascript - 找不到 Javascript 错误
- mongodb - MongoDB中的复合索引效率不清楚
- c++ - 如果构造函数不采用任何输入参数,则使用显式构造函数?
- python - hybrid_property 在 if/else 中引发 TypeError
- postgresql - Postgres 应用程序:配置设置
- javascript - 如何在列表和项目之间导航?