我经常有一个数据集,我需要从中制作多个图形,其中x值保持不变但y值发生变化。 例如,下面的df代码有1个因子变量,年份和3个度量。 我需要制作3个图,其中唯一改变的是y的值。
library(dplyr)
library(ggplot2)
years <- c(2012,2013,2014,2015)
count <- c(20,25,28,31)
spend <- c(300,320,310,341)
prop <- c(.7,.3,.5,.8)
df <- data.frame(years,count,spend,prop)
ggplot(df,aes(x = years, y = count)) +
geom_col()
ggplot(df,aes(x = years, y = spend)) +
geom_col()
ggplot(df,aes(x = years, y = prop)) +
geom_col()
这是一个非常简单的版本,我的实际图表更加精细。
到目前为止,我已经使用了一个循环来生成多个图形,我创建了一个函数,然后在循环中执行,我完成了简单的复制/粘贴。
还有其他更正式的做法吗?与dplyr
和ggplot
或其他任何东西?
谢谢