【问题标题】:Is there a way to create a for loop with char variables to create several plots?有没有办法用 char 变量创建一个 for 循环来创建多个绘图?
【发布时间】:2020-06-07 05:26:24
【问题描述】:

我对 R 很陌生,但我找不到解决问题的方法。我想问题很简单。我有一个包含 4 个变量的 df:日期、SKU_code、SKU_category 和 sales_amount。我想创建一个 for 循环来绘制 n 个数字,其中 n 等于 SKU_category 的数量。换句话说,这是我想在 for 循环中转换的代码。它有效,但我有超过 50 个类别,因此效率不高:

dfsales_red_cat <- dfsales %>% group_by(date, SKU_code, SKU_category) %>% summarize(y=sum(sales_amount))
dfsales_red_C01 <- dfsales_red_cat %>% filter(SKU_category =="C01")
dfsales_red_C01 <- dfsales_red_C01[,c(1,2,4)]
ggplot(dfsales_red_C01,aes(x=date,y=y,colour=SKU_code,group=SKU_code)) + theme(legend.position="none") + geom_line()+labs(title="C01", y='Sales',x='Year')

dfsales_red_C02 <- dfsales_red_cat %>% filter(SKU_category =="C02")
dfsales_red_C02 <- dfsales_red_C02[,c(1,2,4)]
ggplot(dfsales_red_C02,aes(x=date,y=y,colour=SKU_code,group=SKU_code)) + theme(legend.position="none") + geom_line()+labs(title="C02", y='Sales',x='Year')

...and so on...

我试过了,但是没用

dfsales_red_cat <- dfsales %>% group_by(date, SKU_code, SKU_category) %>% summarize(y=sum(sales_amount))

cat <- unique(dfsales_red_cat$SKU_category)

for (i in cat) {
    dfsales_red_i <- dfsales_red_cat %>% filter(SKU_category==i)
  dfsales_red_i <- dfsales_red_i[,c(1,2,4)]
  ggplot(dfsales_red_i,aes(x=date,y=y,colour=SKU_code,group=SKU_code)) + theme(legend.position="none") + geom_line()+
    labs(title=i, y='Sales',x='Year')
}

感谢您的帮助。

这是原始表 dfsales_red_cat(>10000 行)的一部分,格式为日期、字符、字符、数字:

    DATE SKU_code SKU_category sales_amount
1   2016-01-03  Z0003   C13 298380.0
2   2016-01-03  Z0005   C10 225433.6
3   2016-01-03  Z0006   C10 2246883.8
4   2016-01-03  Z0007   C10 653144.4
5   2016-01-03  Z0009   C15 170233.4

【问题讨论】:

  • 嗨,Davide,您能详细说明什么不起作用吗?我注意到循环的 ggplot 函数内部,你有 ggplot(dfsales_red_i,aes(x=week,...),不应该是日期吗?
  • 你的评论是对的。我将 ggplot(dfsales_red_i,aes(x=week,...) 更改为 ggplot(dfsales_red_i,aes(x=date,...),但它没有绘制任何内容。相反,如果我将每个代码运行为在第一个框中一切正常
  • 还是不行?错误是什么?如果 dfsales_red_cat 不是太大,你可以 dput(dfsales_red_cat) 并粘贴输出
  • 好的,谢谢您的建议。改变了原来的问题。我没有任何错误信息。它没有绘制任何东西。
  • 抱歉,这是您的全部数据吗?当每个子集中只有一个数据点时,您将如何绘制线?

标签: for-loop ggplot2 filter char


【解决方案1】:

您需要 print ,将其存储在列表中,然后 print 或只是 facet_wrap。首先获取类似于您的数据的内容:

sample_dates=seq(as.Date("2016-01-03"),as.Date("2016-12-03"),length.out=50)

df = expand.grid(
date = sample_dates,
SKU_code = c("Z0003","Z0005","Z0006"),
SKU_category = c("C13","C10")
)

df$date = as.Date(df$date)
df$sales_amount = runif(nrow(df))
cat <- unique(df$SKU_category)

只要print:

for (i in cat) {
  df_i <- subset(df,SKU_category==i)
  g = ggplot(df_i,aes(x=date,y=sales_amount,colour=SKU_code,group=SKU_code)) + 
  theme(legend.position="none") + geom_line()+
  labs(title=i, y='Sales',x='Year')
  print(g)
}

存储在列表中:

plts = lapply(cat,function(i){
g = ggplot(df_i,aes(x=date,y=sales_amount,colour=SKU_code,group=SKU_code)) + 
      theme(legend.position="none") + geom_line()+
      labs(title=i, y='Sales',x='Year')
return(g)
})
plts[[1]]

或者:

ggplot(df,aes(x=date,y=sales_amount,colour=SKU_code,group=SKU_code)) + 
theme(legend.position="none") + geom_line()+
labs(y='Sales',x='Year')+
facet_wrap(~SKU_category)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    相关资源
    最近更新 更多