creating tree diagram for showing case count using R
The tree diagram could be drawn using the "diagram" package. It is a generic package for drawing flow diagrams etc. See
library(diagram)
demo("flowchart")
Create tree diagram
Using the package ape
, and assuming your data is olddat
:
library(ape)
newdata <- as.phylo(x=~Division/Subgroup/Class/Brand,data=olddat)
plot.phylo(x=newdata,show.tip.label=TRUE,show.node.label=TRUE,no.margin=TRUE)
You'll need to play with your various plot options, but I think this will get you going in the right direction. I'll see about updating the answer if I can get the plot to look okay.
How can I visualize hierarchical data?
Take a look at Rgraphviz
, which can allow you to visualize graphs (including trees), using different schemes for your nodes.
How to build a dendrogram from a directory tree?
Here's a possible approach to get what you originally asked for which is a system like tree. This will give a data.tree
object that's pretty flexible and could be made to plot like you might want but it's not entirely clear to me what you want:
path <- c(
"root/a/some/file.R",
"root/a/another/file.R",
"root/a/another/cool/file.R",
"root/b/some/data.csv",
"root/b/more/data.csv"
)
library(data.tree); library(plyr)
x <- lapply(strsplit(path, "/"), function(z) as.data.frame(t(z)))
x <- rbind.fill(x)
x$pathString <- apply(x, 1, function(x) paste(trimws(na.omit(x)), collapse="/"))
(mytree <- data.tree::as.Node(x))
1 root
2 ¦--a
3 ¦ ¦--some
4 ¦ ¦ °--file.R
5 ¦ °--another
6 ¦ ¦--file.R
7 ¦ °--cool
8 ¦ °--file.R
9 °--b
10 ¦--some
11 ¦ °--data.csv
12 °--more
13 °--data.csv
plot(mytree)
You can get the parts you want (I think) but it'll require you to do the leg work and figure out conversion between data types in data.tree
: https://cran.r-project.org/web/packages/data.tree/vignettes/data.tree.html#tree-conversion
I use this approach in my pathr package's tree
function when use.data.tree = TRUE
https://github.com/trinker/pathr#tree
EDIT Per@Luke's comment below...data.tree::as.Node
takes a path directly:
(mytree <- data.tree::as.Node(data.frame(pathString = path)))
levelName
1 root2
2 ¦--a
3 ¦ ¦--some
4 ¦ ¦ °--file.R
5 ¦ °--another
6 ¦ ¦--file.R
7 ¦ °--cool
8 ¦ °--file.R
9 °--b
10 ¦--some
11 ¦ °--data.csv
12 °--more
13 °--data.csv
Related Topics
How to Remove the Legend Title in Ggplot2
Print Number as Reduced Fraction in R
Passing Arguments to Iterated Function Through Apply
Time Difference in Years with Lubridate
Dplyr - Mean for Multiple Columns
How to Increase Size of the Points in Ggplot2, Similar to Cex in Base Plots
Convert Data from Many Rows to Many Columns
Insert Portions of a Markdown Document Inside Another Markdown Document Using Knitr
Really Fast Word Ngram Vectorization in R
Dplyr 'Rename' Standard Evaluation Function Not Working as Expected
Creating Tree Diagram for Showing Case Count Using R
R: Unexpected Results from P.Adjust (Fdr)
Raster Package Taking All Hard Drive
Using Dplyr for Frequency Counts of Interactions, Must Include Zero Counts
Is There Any Other Package Other Than "Sentiment" to Do Sentiment Analysis in R