【问题标题】:Plot inside a loop using highcharter使用 highcharter 在循环内绘图
【发布时间】:2017-01-05 21:57:23
【问题描述】:

我正在为我的图表使用 highcharter。我有不同的课程,我需要为所有人绘制相同的图表。我正在尝试对课程进行 for 循环并在我的循环中绘制这些图表。 它没有在循环内绘制任何内容并且在循环外可以正常工作的问题。

for(i in unique(my_data$CourseID)){
  courseData<-subset(my_data, my_data$CourseID== i)

  #Course by Majer
  byMajer <- table(courseData$MajerName)
  #barplot(byMajer, main="Students by Majer", xlab="Majers") 

  byM <- aggregate(courseData$MajerName, by=list(courseData$MajerName), FUN=length)

  hc <- highchart(debug = TRUE) %>% 
  hc_title(text = courseData$CourseName[1]) %>% 
  hc_chart(type = "column") %>% 
  hc_xAxis(categories = byM$Group.1) %>% 
  hc_add_series(data = byM$x)

  #this is not working. it shows nothing.
  hc

  #but if I try to have this, it works, I will use below variables outside the loop
  assign(paste("hc",i,sep=""), hc)
}

#Below graphs showing in the output. I need to get rid of them and use the one inside the loop.
hc1; hc2; hc3; hc4; hc5; hc6; hc7; hc8; hc9; hc10; hc11; hc12; hc13; hc14; hc15; hc16; hc17; hc18

【问题讨论】:

    标签: r highcharts


    【解决方案1】:

    问题的答案在这里:https://stackoverflow.com/a/4716380/4046096

    简而言之:自动打印是循环关闭的,所以你需要明确地print 一些东西。

    print(hc)代替hc

    我没有你的数据,但这段代码可以正常工作:

    for(i in c(1,2,3,4,5)){
      hc <- highchart(debug = TRUE) %>% 
      hc_chart(type = "column") %>% 
      hc_add_series(data = list(1, i, i * 2, 5))
    
      print(hc)
    }
    

    【讨论】:

    • @shanyour 如果此答案回答了您的问题,您可以将其标记为已接受 - 如果没有,请提供有关未回答内容的更多信息。
    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    相关资源
    最近更新 更多