我有一些分层数据,例如,
> library(dplyr)
> df <- data_frame(id = 1:6, parent_id = c(NA, 1, 1, 2, 2, 5))
> df
Source: local data frame [6 x 2]
id parent_id
(int) (dbl)
1 1 NA
2 2 1
3 3 1
4 4 2
5 5 2
6 6 5
Run Code Online (Sandbox Code Playgroud)
我想通过圆形包装图在"自上而下"的视图中绘制树:http: //bl.ocks.org/mbostock/4063530
以上链接适用于d3库.是否有一个等价物允许我在ggplot2中制作这样的情节?
(我希望这个情节在一个闪亮的应用程序,它确实支持d3,但我之前没有使用过d3并且不确定学习曲线.如果d3是明显的选择,我会尝试让它工作.谢谢.)
pic*_*ick 12
有两个步骤:(1)聚合数据,然后(2)转换为json.之后,所有的javascript都写在该示例页面中,因此您只需插入生成的json数据即可.
由于聚合数据应具有与树图类似的结构,因此我们可以使用该treemap包进行聚合(也可以使用具有连续聚合的循环).然后,d3treeR(来自github)用于将树形图数据转换为嵌套列表,jsonlite并将列表转换为json.
我使用了一些示例数据GNI2010,在发现d3treeR包.您可以在plunker上查看所有源文件.
library(treemap)
library(d3treeR) # devtools::install_github("timelyportfolio/d3treeR")
library(data.tree)
library(jsonlite)
## Get treemap data using package treemap
## Using example data GNI2010 from d3treeR package
data(GNI2010)
## aggregate by these: continent, iso3,
## size by population, and color by GNI
indexList <- c('continent', 'iso3')
treedat <- treemap(GNI2010, index=indexList, vSize='population', vColor='GNI',
type="value", fun.aggregate = "sum",
palette = 'RdYlBu')
treedat <- treedat$tm # pull out the data
## Use d3treeR to convert to nested list structure
## Call the root node 'flare' so we can just plug it into the example
res <- d3treeR:::convert_treemap(treedat, rootname="flare")
## Convert to JSON using jsonlite::toJSON
json <- toJSON(res, auto_unbox = TRUE)
## Save the json to a directory with the example index.html
writeLines(json, "d3circle/flare.json")
Run Code Online (Sandbox Code Playgroud)
我还将示例中的源代码行替换index.html为
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
要创建闪亮的绑定应该可以使用htmlwidgets和遵循一些示例(d3treeR源有一些).请注意,某些东西不起作用,比如着色.存储在这里的json实际上包含了很多关于节点的信息(使用它聚合的所有数据treemap),您可以在图中使用它们.
| 归档时间: |
|
| 查看次数: |
2924 次 |
| 最近记录: |