修复图表上杂乱的标题 [英] Fixing Cluttered Titles on Graphs

查看:12
本文介绍了修复图表上杂乱的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了以下25张网络图(为了简单起见,所有这些图都是副本-实际上,它们都是不同的):

library(tidyverse)
library(igraph)


set.seed(123)
n=15
data = data.frame(tibble(d = paste(1:n)))

relations = data.frame(tibble(
  from = sample(data$d),
  to = lead(from, default=from[1]),
))

data$name = c("new york", "chicago", "los angeles", "orlando", "houston", "seattle", "washington", "baltimore", "atlanta", "las vegas", "oakland", "phoenix", "kansas", "miami", "newark" )

graph = graph_from_data_frame(relations, directed=T, vertices = data) 

V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")

plot(graph, layout=layout.circle, edge.arrow.size = 0.2, main = "my_graph")

library(visNetwork)

    a = visIgraph(graph)  

m_1 = 1
m_2 = 23.6

 a = toVisNetworkData(graph) %>%
    c(., list(main = paste0("Trip ", m_1, " : "), submain = paste0 (m_2, "KM") )) %>%
    do.call(visNetwork, .) %>%
    visIgraphLayout(layout = "layout_in_circle") %>% 
    visEdges(arrows = 'to') 



y = x = w = v = u = t = s = r = q  = p = o = n = m = l = k = j = i = h = g = f = e = d = c = b = a

我想将它们平铺为5 x 5:因为它们是交互式的html绘图-我使用了以下命令:

library(manipulateWidget)
library(htmltools)

ff = combineWidgets(y , x , w , v , u , t , s , r , q  , p , o , n , m , l , k , j , i , h , g , f , e , d , c , b , a)

htmltools::save_html(html = ff, file = "widgets.html")

我了解了如何为每个单独的图表添加缩放选项:

 a = toVisNetworkData(graph) %>%
    c(., list(main = paste0("Trip ", m_1, " : "), submain = paste0 (m_2, "KM") )) %>%
    do.call(visNetwork, .) %>%
    visIgraphLayout(layout = "layout_in_circle") %>%  
    visInteraction(navigationButtons = TRUE) %>% 
    visEdges(arrows = 'to') 

y = x = w = v = u = t = s = r = q  = p = o = n = m = l = k = j = i = h = g = f = e = d = c = b = a

ff = combineWidgets(y , x , w , v , u , t , s , r , q  , p , o , n , m , l , k , j , i , h , g , f , e , d , c , b , a)

htmltools::save_html(html = ff, file = "widgets.html")

但现在,";Zoom";选项和";标题";已使";乱七八糟的所有图形!

我在想,最好将所有这些图表堆叠在一起,并将每个图表另存为组类型(&p;),然后根据需要隐藏/取消隐藏:

visNetwork(data, relations) %>% 
 visOptions(selectedBy = "group")
  • 我们是否可以将所有25个图表放在一页上,然后将其缩放到每个单独的图表中以便更好地查看(例如,屏幕角落中只有一组缩放/导航按钮可用于所有图表)?

  • 有没有办法防止标题与图表重叠?

  • 我们是否可以将所有25个图表放在一页上,然后通过选中选项菜单按钮隐藏单独的图表?(如本页上的最后一个示例:https://datastorm-open.github.io/visNetwork/options.html)

以下是我为这个问题想出的可能解决方案:

  • 选项1:(用于所有图表的单一缩放/导航选项,没有杂乱的标签)

  • 选项2:(将来,每个&Quot;Trip&Quot;将不同-&Quot;Trips&Quot;将包含相同的节点,但具有不同的边缘连接和不同的标题/字幕。)

我知道可以使用以下代码进行此样式的选择(选项2&q;):

nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
 group = sample(LETTERS[1:3], 15, replace = TRUE))

edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
 to = trunc(runif(15)*(15-1))+1)



visNetwork(nodes, edges) %>% 
    visOptions(selectedBy = "group")

但我不确定如何使上面的代码适用于预先存在的一组&visNetwork";图。例如,假设我已经有了";visNetwork";图形&a、b、c、d、e";-如何将它们堆叠在一起,并在它们之间进行重新洗牌,同时在上面的代码中选择菜单&q;?

有人能告诉我使用选项1和选项2解决此杂乱问题的方法吗?

谢谢!

推荐答案

虽然我的解决方案与您在Option 2中描述的不完全相同,但它很接近。我们使用combineWidgets()创建一个具有单列和行高的网格,其中一个图形覆盖了大部分屏幕高度。我们在每个窗口小部件实例之间加入一个链接,该链接在单击时向下滚动浏览器窗口以显示以下图形。

如果对您有效,请让我知道。应该可以根据浏览器窗口大小自动调整行大小。目前,这取决于浏览器窗口高度在1000px左右。

我略微修改了用于创建图形的代码,并将其包装在一个函数中。这使我们可以轻松地创建25个不同外观的图表。这样一来,测试生成的HTML文件会更有趣!函数定义后面是创建list个HTML对象的代码,然后我们将这些对象提供给combineWidgets()

library(visNetwork)
library(tidyverse)
library(igraph)
library(manipulateWidget)
library(htmltools)

create_trip_graph <-
  function(x, distance = NULL) {
    n <- 15
    data <- tibble(d = 1:n,
                   name =
                     c(
                       "new york",
                       "chicago",
                       "los angeles",
                       "orlando",
                       "houston",
                       "seattle",
                       "washington",
                       "baltimore",
                       "atlanta",
                       "las vegas",
                       "oakland",
                       "phoenix",
                       "kansas",
                       "miami",
                       "newark"
                     ))
    
    relations <-  tibble(from = sample(data$d),
                         to = lead(from, default = from[1]))    
    graph <-
      graph_from_data_frame(relations, directed = TRUE, vertices = data)
    
    V(graph)$color <-
      ifelse(data$d == relations$from[1], "red", "orange")
    
    if (is.null(distance))
      # This generates a random distance value if none is 
      # specified in the function call. Values are just for 
      # demonstration, no actual distances are calculated.
      distance <- sample(seq(19, 25, .1), 1)
    
    toVisNetworkData(graph) %>%
      c(., list(
        main = paste0("Trip ", x, " : "),
        submain = paste0(distance, "KM")
      )) %>%
      do.call(visNetwork, .) %>%
      visIgraphLayout(layout = "layout_in_circle") %>%
      visInteraction(navigationButtons = TRUE) %>%
      visEdges(arrows = 'to')
  }

comb_vgraphs <- lapply(1:25, function (x) list(
  create_trip_graph(x),
  htmltools::a("NEXT TRIP", 
               onclick = 'window.scrollBy(0,950)', 
               style = 'color:blue; text-decoration:underline;')))  %>%
  unlist(recursive = FALSE)


ff <-
  combineWidgets(
    list = comb_vgraphs,
    ncol = 1,
    height = 25 * 950,
    rowsize = c(24, 1)
  )

htmltools::save_html(html = ff, file = "widgets.html")
如果您希望每行有5个网络地图,代码就会变得有点复杂,而且还可能导致用户必须进行水平滚动才能看到所有内容的情况,而这是在创建HTML页面时通常希望避免的情况。以下是每行5幅地图解决方案的代码:

comb_vgraphs2 <- lapply(1:25, function(x) {
  a <- list(create_trip_graph(x))
  # We detect whenever we are creating the 5th, 10th, 15th etc. network map
  # and add the link after that one.
  if (x %% 5 == 0 & x < 25) a[[2]] <- htmltools::a("NEXT 5 TRIPS", 
                                          onclick = 'window.scrollBy(0,500)', 
                                          style = 'color:blue; text-decoration:underline;')
  a
}) %>%
  unlist(recursive = FALSE)

ff2 <-
  combineWidgets(
    list = comb_vgraphs2,
    ncol = 6, # We need six columns, 5 for the network maps 
              # and 1 for the link to scroll the page.
    height = 6 * 500,
    width = 1700
    #rowsize = c(24, 1)
  )

# We need to add some white space in for the scrolling by clicking the link to 
# still work for the last row.
ff2$widgets[[length(ff2$widgets) + 1]] <- htmltools::div(style = "height: 1000px;")

htmltools::save_html(html = ff2, file = "widgets2.html")
总的来说,我建议您使用combineWidgets()heightwidthncolnrow参数来获得满意的解决方案。我在构建它时的策略是,首先创建一个没有滚动链接的网格,然后在得到正确的网格后将其添加进去。

这篇关于修复图表上杂乱的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆