我想遍历六个相似的数据框并使用ggplot打印出六个图。
当我分别运行绘图时,我的代码有效,但是我无法使用for循环让ggplot运行六次。我的六个数据帧是gg_pets中列出的六个动物。
cat <- data.frame(Breed = c("American Shorthair","Ragdoll","Persian","Sphynx","Maine Coon"),
Longevity = c("17","19","15","17","20"))
dog <- data.frame(Breed = c("Havanese","Bulldog","Beagle","Chihuahua","Poodle"),
Longevity = c("20","11","12","15","16"))
#etc for types of birds, fish, snakes, and ferrets
#the following works
plot <- ggplot(data = cat, aes(x = Breed, y = Longevity, fill = Breed)) +
geom_bar(stat = "identity", position = position_dodge()) +
xlab("Breed") +
ylab("Longevity") +
ggtitle("cat") +
geom_text(aes(label = Longevity), vjust = -0.3, color = "black", size = 3.5) +
theme(axis.line = element_line(color = "black"), axis.text = element_text(color = "black"),
legend.position = "none", plot.title = element_text(hjust = .5),
panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
panel.border = element_rect(color = "black", fill = NA, size = 0.8),
panel.background = element_rect(fill = NA), text = element_text(size=10))
print(plot)
gg_pets <- c("cat","dog","bird","fish","snake","ferret")
#the following does not work
for (i in 1:length(gg_pets)){
plot <- ggplot(data = [i], aes(x = Breed, y = Longevity, fill = Breed)) +
geom_bar(stat = "identity", position = position_dodge()) +
xlab("Breed") +
ylab("Longevity") +
ggtitle([i]) +
geom_text(aes(label = Longevity), vjust = -0.3, color = "black", size = 3.5) +
theme(axis.line = element_line(color = "black"), axis.text = element_text(color = "black"),
legend.position = "none", plot.title = element_text(hjust = .5),
panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
panel.border = element_rect(color = "black", fill = NA, size = 0.8),
panel.background = element_rect(fill = NA), text = element_text(size=10))
print(plot)
}
答案 0 :(得分:2)
在您的示例中,gg_pets只是字符串的向量。您需要连接数据帧,以便在for循环中对其进行迭代。您可以使用列表进行操作。如下。您可以将项目名称用作标题。
...
gg_pets <- list(cat=cat, dog=dog)
#the following does not work
for (i in 1:length(gg_pets)) {
plot <- ggplot(data = gg_pets[[i]], aes(x = Breed, y = Longevity, fill = Breed)) +
geom_bar(stat = "identity", position = position_dodge()) +
xlab("Breed") +
ylab("Longevity") +
ggtitle(names(gg_pets)[i]) +
geom_text(aes(label = Longevity), vjust = -0.3, color = "black", size = 3.5) +
theme(axis.line = element_line(color = "black"), axis.text = element_text(color = "black"),
legend.position = "none", plot.title = element_text(hjust = .5),
panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
panel.border = element_rect(color = "black", fill = NA, size = 0.8),
panel.background = element_rect(fill = NA), text = element_text(size=10))
print(plot)
}
答案 1 :(得分:0)
我认为您需要
ggplot(data = get(i), ...)
,但是将数据集放在命名列表(my_data_list <- list(cat=cat, dog=dog, ...)
甚至my_data_list <- mget(gg_pets)
中)并使用data = my_data_list[[i]]
...
答案 2 :(得分:0)
其他答案中提到的list
方法的另一种选择是首先将所有内容都放在同一data.frame
中,因为显然所有表都具有相同的变量;然后再添加一个变量,指示观察值对应于哪种宠物。如下:
pets_df <- bind_rows(cat %>% add_column(pet = 'cat'),
dog %>% add_column(pet = 'dog'))
> pets_df
Breed Longevity pet
1 American Shorthair 17 cat
2 Ragdoll 19 cat
3 Persian 15 cat
4 Sphynx 17 cat
5 Maine Coon 20 cat
6 Havanese 20 dog
7 Bulldog 11 dog
8 Beagle 12 dog
9 Chihuahua 15 dog
10 Poodle 16 dog
然后,在循环的每次迭代中,您只需要过滤一个data.frame
。例如:
for (i in 1:length(gg_pets)) {
plot <- ggplot(data = filter(pets_df, pet == gg_pets[i]),
aes(x = Breed, y = Longevity, fill = Breed)) +
geom_bar(stat = "identity", position = position_dodge()) +
xlab("Breed") +
ylab("Longevity") +
ggtitle(gg_pets[i]) +
geom_text(aes(label = Longevity), vjust = -0.3, color = "black", size = 3.5) +
theme(axis.line = element_line(color = "black"), axis.text = element_text(color = "black"),
legend.position = "none", plot.title = element_text(hjust = .5),
panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
panel.border = element_rect(color = "black", fill = NA, size = 0.8),
panel.background = element_rect(fill = NA), text = element_text(size=10))
print(plot)
}