在R(github程序包)中向流图添加标题

时间:2019-04-23 15:37:38

标签: r github htmlwidgets stream-graph

我有类似“子示例”的数据

structure(list(monthyr = structure(c(17287, 17287, 17287, 17287, 
17287, 17287, 17287, 17287, 17287, 17287, 17287, 17287, 17287, 
17287, 17287, 17287, 17287, 17287), class = "Date"), Location = c("TAI", 
"NAM", "LUI", "HEE", "BRA", "KEI", "TAI", "NAM", "LUI", "HEE", 
"BRA", "KEI", "TAI", "NAM", "LUI", "HEE", "BRA", "KEI"), ID = structure(c(-719050, 
-718685, -718320, -717954, -717589, -717224, -719050, -718685, 
-718320, -717954, -717589, -717224, -719050, -718685, -718320, 
-717954, -717589, -717224), class = "Date"), Type = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L), .Label = c("Industrial.compound", "Pesticide", "Pharmaceutical"
), class = "factor"), value = c(0.885230769230769, 1.9203125, 
3.26635, 0.950052631578947, 2.87133333333333, 3.194775, 0.0823076923076923, 
0.190833333333333, 0.467142857142857, 0.58173125, 0.1375, 0.3777475, 
0.0215555555555556, 0.240236363636364, 0.340790909090909, 0.150083333333333, 
0.103333333333333, 0.2665)), row.names = c(29L, 65L, 107L, 149L, 
194L, 237L, 279L, 315L, 357L, 399L, 444L, 487L, 529L, 565L, 607L, 
649L, 694L, 737L), class = "data.frame")
> 

我想使用https://github.com/hrbrmstr/streamgraph中的streamgraph

所以我做到了:

sg<-streamgraph(subexample,"Type","value", "ID" )%>%
  sg_legend(show=TRUE, label="Parameter: ")%>%
  sg_axis_x(1, "ID")%>% sg_axis_y(0)

我得到了不错的流图,但是当我添加sg_title

sg<-streamgraph(subexample,"Type","value", "ID" )%>%
      sg_legend(show=TRUE, label="Parameter: ")%>%
      sg_axis_x(1, "ID")%>% sg_axis_y(0)%>%sg_title("Title example") 

我在控制台中得到了大量代码,而不是带有标题的流图。

我试图这样分离它:

sg<-streamgraph(subexample,"Type","value", "ID" )%>%
  sg_legend(show=TRUE, label="Parameter: ")%>%
  sg_axis_x(1, "ID")%>% sg_axis_y(0)

sg_title(sg, "Title example")

但没有结果。

您知道要获得带有标题的流图需要做什么吗?

1 个答案:

答案 0 :(得分:0)

我找到了这个解决方案。

library(streamgraph)
library(dplyr)
library(shiny)

ui = shinyUI(fluidPage(
  h3("Title Example", style="text-align:center"),
  streamgraphOutput('sg1')
))

server = function(input, output) {

  sgexample<-streamgraph(subexample, "Type","value", "ID" )%>%
    sg_legend(show=TRUE, label="Parameter: ")%>%
    sg_axis_x(1, "ID")%>% sg_axis_y(0)

  output$sg1 <- renderStreamgraph(sgexample)

}

shinyApp(ui = ui, server = server)