首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用for循环使用ggplot() -R构建10个图

使用for循环使用ggplot() -R构建10个图
EN

Stack Overflow用户
提问于 2021-03-16 18:41:09
回答 1查看 35关注 0票数 0

我正在尝试使用ggplot()构建10个不同的图来显示消费选择(有机的、非有机的)。我想使用循环来构建这些图,而不是一个接一个地构建,以下是我尝试逐个构建时的原始代码:

代码语言:javascript
复制
a <- with(data, table(Banana_Choice))
p1 <- ggplot(as.data.frame(a), aes(factor(Banana_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = a), vjust= -0.3) + xlab("Banana") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p1

b <- with(data, table(Apple_Choice))
p2 <- ggplot(as.data.frame(b), aes(factor(Apple_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = b), vjust= -0.3) + xlab("Apple") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p2  

c <- with(data, table(Tomato_Choice))
p3 <- ggplot(as.data.frame(c), aes(factor(Tomato_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = c), vjust= -0.3) + xlab("Tomato") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p3 

d <- with(data, table(Cucumber_Choice))
p4 <- ggplot(as.data.frame(d), aes(factor(Cucumber_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = d), vjust= -0.3) + xlab("Cucumber") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p4 

e <- with(data, table(Broccoli_Choice))
p5 <- ggplot(as.data.frame(e), aes(factor(Broccoli_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = e), vjust= -0.3) + xlab("Broccoli") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p5

f <- with(data, table(Milk_Choice))
p6 <- ggplot(as.data.frame(f), aes(factor(Milk_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = f), vjust= -0.3) + xlab("Milk") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p6 

g <- with(data, table(Cheese_Choice))
p7 <- ggplot(as.data.frame(g), aes(factor(Cheese_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = g), vjust= -0.3) + xlab("Cheese") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p7 

h <- with(data, table(Wine_Choice))
p8 <- ggplot(as.data.frame(h), aes(factor(Wine_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = h), vjust= -0.3) + xlab("Wine") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p8

i <- with(data, table(MilkChoco_Choice))
p9 <- ggplot(as.data.frame(i), aes(factor(MilkChoco_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = i), vjust= -0.3) + xlab("Milk Chocolate") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p9


j <- with(data, table(DarkChoco_Choice))
p10 <- ggplot(as.data.frame(j), aes(factor(DarkChoco_Choice), Freq)) +     
  geom_col(position = 'dodge') + geom_bar(fill = "#69b4a2", stat = "identity") + theme_gray(base_size = 14)+
  geom_text(aes(label = j), vjust= -0.3) + xlab("Dark Chocolate") + ylab("Frequency (count)")+
  theme(axis.ticks = element_blank(),
        text=element_text(size=11))+ylim(0,100)
p10

如你所见,这是非常慢的。如何使用for循环构建这些图?

EN

回答 1

Stack Overflow用户

发布于 2021-03-16 18:55:43

您可以尝试使用tidyr::pivot_longer()将数据转换为更长的格式,然后使用facet_wrap()一次性生成所有绘图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66653575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档