R不以编程方式保存图像
R not saving images programmatically
我正在使用 R+RStudio,我想为不同的文件保存一个 ggplot,但对于每个文件,一个不同的过滤器,所以我想要每个可用的子类型的图。在我看来这应该是一件非常简单的事情,我知道如何保存文件并且我以前做过。但我不知道为什么,当我遍历不同的子类型以保存这些图时(如果我一个一个地执行,我可以看到它们实际上是在绘制)不要在我的目标文件夹中绘制,只是一个白色文件。
所以我的代码是这样的:
shoton <- incidents %>% filter(type == 'shoton')
data <- unique(shoton$subtype1)
for(i in data){
fileName <- paste0("factorsImages/fouls/",i, ".png")
png(filename = fileName, bg="transparent")
shoton %>%
filter(subtype1 == i) %>%
ggplot(mapping = aes(x = lon, y = lat)) +
geom_jitter(mapping = aes(colour = subtype1), alpha = 0.3, size = 2, stroke = 0) +
scale_color_viridis(discrete = T) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
facet_wrap(~half) +
theme_minimal()
shoton
dev.off()
}
也许对于查看数据的外观很有用:
> head(shoton,10)
# A tibble: 10 x 13
id subtype2 subtype1 player1 player2 team lon lat elapsed elapsed_plus game_id half half_elapsed
<int> <fct> <fct> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <int> <dbl> <dbl>
1 378828 <NA> blocked_shot 24154 <NA> 10260 NA NA 3 0 1729 1 3
2 378866 <NA> header 24157 <NA> 10260 NA NA 7 0 1729 1 7
3 378922 <NA> shot 30829 <NA> 10260 NA NA 14 0 1729 1 14
4 378923 <NA> shot 30373 <NA> 10260 NA NA 14 0 1729 1 14
5 378951 <NA> distance 30373 <NA> 10260 NA NA 17 0 1729 1 17
6 379204 <NA> blocked_shot 24154 <NA> 10260 NA NA 43 0 1729 1 43
7 379363 <NA> distance 37799 <NA> 10261 NA NA 50 0 1729 2 50
8 379401 <NA> distance 24157 <NA> 10260 NA NA 58 0 1729 2 58
9 379406 <NA> blocked_shot 24157 <NA> 10260 NA NA 58 0 1729 2 58
10 379414 <NA> blocked_shot 30829 <NA> 10260 NA NA 60 0 1729 2 60
我很确定这一定很愚蠢,但老实说,我觉得我在这上面花了太多时间,我的研究对解决问题没有帮助。任何类型的反馈将不胜感激,
谢谢!
print
for
循环中的剧情:
library(dplyr)
library(ggplot2)
shoton <- incidents %>% filter(type == 'shoton')
data <- unique(shoton$subtype1)
for(i in data){
fileName <- paste0("factorsImages/fouls/",i, ".png")
png(filename = fileName, bg="transparent")
plot <- shoton %>%
filter(subtype1 == i) %>%
ggplot(mapping = aes(x = lon, y = lat)) +
geom_jitter(mapping = aes(colour = subtype1), alpha = 0.3, size = 2, stroke = 0) +
scale_color_viridis(discrete = T) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
facet_wrap(~half) +
theme_minimal()
print(plot)
dev.off()
}
我正在使用 R+RStudio,我想为不同的文件保存一个 ggplot,但对于每个文件,一个不同的过滤器,所以我想要每个可用的子类型的图。在我看来这应该是一件非常简单的事情,我知道如何保存文件并且我以前做过。但我不知道为什么,当我遍历不同的子类型以保存这些图时(如果我一个一个地执行,我可以看到它们实际上是在绘制)不要在我的目标文件夹中绘制,只是一个白色文件。
所以我的代码是这样的:
shoton <- incidents %>% filter(type == 'shoton')
data <- unique(shoton$subtype1)
for(i in data){
fileName <- paste0("factorsImages/fouls/",i, ".png")
png(filename = fileName, bg="transparent")
shoton %>%
filter(subtype1 == i) %>%
ggplot(mapping = aes(x = lon, y = lat)) +
geom_jitter(mapping = aes(colour = subtype1), alpha = 0.3, size = 2, stroke = 0) +
scale_color_viridis(discrete = T) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
facet_wrap(~half) +
theme_minimal()
shoton
dev.off()
}
也许对于查看数据的外观很有用:
> head(shoton,10)
# A tibble: 10 x 13
id subtype2 subtype1 player1 player2 team lon lat elapsed elapsed_plus game_id half half_elapsed
<int> <fct> <fct> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <int> <dbl> <dbl>
1 378828 <NA> blocked_shot 24154 <NA> 10260 NA NA 3 0 1729 1 3
2 378866 <NA> header 24157 <NA> 10260 NA NA 7 0 1729 1 7
3 378922 <NA> shot 30829 <NA> 10260 NA NA 14 0 1729 1 14
4 378923 <NA> shot 30373 <NA> 10260 NA NA 14 0 1729 1 14
5 378951 <NA> distance 30373 <NA> 10260 NA NA 17 0 1729 1 17
6 379204 <NA> blocked_shot 24154 <NA> 10260 NA NA 43 0 1729 1 43
7 379363 <NA> distance 37799 <NA> 10261 NA NA 50 0 1729 2 50
8 379401 <NA> distance 24157 <NA> 10260 NA NA 58 0 1729 2 58
9 379406 <NA> blocked_shot 24157 <NA> 10260 NA NA 58 0 1729 2 58
10 379414 <NA> blocked_shot 30829 <NA> 10260 NA NA 60 0 1729 2 60
我很确定这一定很愚蠢,但老实说,我觉得我在这上面花了太多时间,我的研究对解决问题没有帮助。任何类型的反馈将不胜感激,
谢谢!
print
for
循环中的剧情:
library(dplyr)
library(ggplot2)
shoton <- incidents %>% filter(type == 'shoton')
data <- unique(shoton$subtype1)
for(i in data){
fileName <- paste0("factorsImages/fouls/",i, ".png")
png(filename = fileName, bg="transparent")
plot <- shoton %>%
filter(subtype1 == i) %>%
ggplot(mapping = aes(x = lon, y = lat)) +
geom_jitter(mapping = aes(colour = subtype1), alpha = 0.3, size = 2, stroke = 0) +
scale_color_viridis(discrete = T) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
facet_wrap(~half) +
theme_minimal()
print(plot)
dev.off()
}