r - 使用带有 for 循环的 plot_ly 命令时出现问题
问题描述
我是 R 编程新手,在修改 R 脚本以在代码中引入“for”循环时遇到了困难。在搜索时,我发现这个问题已经被提出并使用 lapply 命令解决了,但我不知道如何解决我的问题!有效的代码(没有 for 命令):CPM.txt 文件包含 1 行
CPM <- read.table("FicConfig/CPM.txt" , header = TRUE, sep = "\t" , dec = ",", skip = 0)
options(viewer = NULL)
plot_ly() %>%
htmlwidgets::onRender(
"function(el, x) {
var gd = document.getElementById(el.id);
Plotly.downloadImage(gd, {format: 'png', width: 1000, height: 700, filename: 'AnomalieRR_EcartTmoy'});
}"
)%>%
add_trace(x =TAnn[ ,3], y = TAnn[ ,5], name = "Normale mensuelle de Tmax(en °C)",type = 'scatter', mode = 'markers', marker = list(size = T, symbol = 'circle', color = ~TAnn[ ,5], line = list(width= 2, color = cl))) %>%
add_annotations(text = TAnn[ ,1], x= TAnn[ ,3], y = TAnn[ ,5], font = list(color = "white", size = 14), showarrow = FALSE)%>%
layout(title = paste("Combinaison anomalie relative annuelle des précipitations et écart annuel à la normale de la température moyenne à ",CPM[1,1],sep =""),
xaxis = list(title = "Anomalie relative des précipitations annuelles(en %)", tickangle = 20 ,titlefont = list(color= "blue", size= 14 , family = 'Arial'), tickfont = list(color = "blue", size = 14)),
yaxis = list(title = "Ecart à la normale de la température moyenne annuelle(en°C)", titlefont = list(color= "red", size= 14 , family = 'Arial'),
tickfont = list(color = "red", size = 14) , showline = TRUE, linecolor = "red", linewidth = 2),
legend = list(x = 0.1, y = -0.3, font=list(size = 14,color= "black")),margin = list(
t = 70,
r = 70,
b = 70,
l = 70
))
不起作用的代码(使用 for 命令): # CPM.txt 文件包含几行
CPM <- read.table("FicConfig/CPM.txt" , header = TRUE, sep = "\t" , dec = ",", skip = 0)
for (i in 1: (nrow(CPM))) {
options(viewer = NULL)
plot_ly() %>%
htmlwidgets::onRender(
"function(el, x) {
var gd = document.getElementById(el.id);
Plotly.downloadImage(gd, {format: 'png', width: 1000, height: 700, filename: 'AnomalieRR_EcartTmoy'});
}"
)%>%
add_trace(x =TAnn[ ,3], y = TAnn[ ,5], name = "Normale mensuelle de Tmax(en °C)",type = 'scatter', mode = 'markers', marker = list(size = T, symbol = 'circle', color = ~TAnn[ ,5], line = list(width= 2, color = cl))) %>%
add_annotations(text = TAnn[ ,1], x= TAnn[ ,3], y = TAnn[ ,5], font = list(color = "white", size = 14), showarrow = FALSE)%>%
layout(title = paste("Combinaison anomalie relative annuelle des précipitations et écart annuel à la normale de la température moyenne à ",CPM[i,1],sep =""),
xaxis = list(title = "Anomalie relative des précipitations annuelles(en %)", tickangle = 20 ,titlefont = list(color= "blue", size= 14 , family = 'Arial'), tickfont = list(color = "blue", size = 14)),
yaxis = list(title = "Ecart à la normale de la température moyenne annuelle(en°C)", titlefont = list(color= "red", size= 14 , family = 'Arial'),
tickfont = list(color = "red", size = 14) , showline = TRUE, linecolor = "red", linewidth = 2),
legend = list(x = 0.1, y = -0.3, font=list(size = 14,color= "black")),margin = list(
t = 70,
r = 70,
b = 70,
l = 70
))
file.copy("C:/Users/pc/Downloads/Evolution.png", paste("C:/MONOGRAPHIE/EvolTmoy_",CPM[i,1],".png",sep="")
}
非常感谢您的任何帮助。
解决方案
推荐阅读
- html - 响应式重叠图像
- php - 不同网页中的动态评论系统
- php - errno: 150 “外键约束在迁移 laravel 6 中形成错误
- django - 如何使用基于类的视图呈现表单?
- angularjs - AngularJS:如何使用不同的控制器处理同一页面上的多个 http 请求
- php - FPDF。添加页面肖像仅适用于第一页
- excel - 尝试刷新连接时出现 1004 错误
- java - 如何按 LatLng ArrayList 中值的顺序对 LatLng、String 的 Map 进行排序
- laravel - Laravel - 如何在存在依赖字段时防止删除
- python - 根据第 2 列中存在的字符串更新第 1 列