【问题标题】:Using for loop to build 10 plots using ggplot() - R使用 for 循环使用 ggplot() - R 构建 10 个图
【发布时间】:2021-06-13 15:20:40
【问题描述】:

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

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 循环构建这些图?

【问题讨论】:

标签: r for-loop ggplot2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2019-02-23
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多