Rotate x-axis of r partykit tree plot - r

I've built a decision tree using ctree in R and visualize the tree using the ctree model in the party package.
I am very happy with the results and the overall visualization. However, I cannot interpret the 'confusion' in every leaf node, since the x-axis labels either overlap or are missing!
Currently I use the following command:
plot(fitCtree, main="Title", gp = gpar(fontsize = 2))
I've searched quite a lot to find the (simple?) answer... to no avail.
Can you help me out?
Cheers,
Arend

One option that is easily available in node_barplot(), the panel function employed here, is to rotate the axis labels rather than rotating the entire plot (as suggested in https://stackoverflow.com/a/12000533/4752675 mentioned by #G5W). For example you can set rot = 45, just = c("right", "top") to obtain a rotation by 45 degrees with top-right justification of the labels.
Depending on the length of the labels, it might be necessary to increase the lower margin of the plot to allow for enough space. One can do this either with pushing a separate viewport - or via the convenience argument margins that I just added to the development version of partykit on R-Forge.
As an illustration:
install.packages("partykit", repos = "http://R-Forge.R-project.org")
library("partykit")
ct <- ctree(Species ~ ., data = iris)
plot(ct, margins = c(3, 0, 0, 0),
tp_args = list(rot = 45, just = c("right", "top")))

Related

R: Drawing markov model with diagram package (making diagram changes)

I have the following code that draws a transition probability graph using the package heemod (for the matrix) and the package diagram (for drawing). The following code generates such a graph with data that I have generated:
library('heemod')
library('diagram')
mat_dim <- define_transition(
state_names = c('State_A', 'State_B', 'State_C'),
.18, .73, .09,
.12, .10, .78,
.58, .08, .33);
plot(mat_dim)
This creates the following plot:
My questions all originate from my poor understanding of the diagram package and I can't figure out these seemingly simple adaptations...
How can I move the arrow at State_B around (e.g.90 degrees to one side) so that it does not overlap with other arrows? Is there a simple way to move the states closer together?
See ?plotmat.
argument curve, a matrix, to control the curvatures of the "non-self" transitions
arguments self.shiftx and self.shifty to control the positions of the self-transitions
argument self.arrpos to control the positions of the self-arrows
This is really not easy. Here is what I obtained by a lot of trial-errors.
curves <- matrix(nrow = 3, ncol = 3, 0.05)
plot(mat_dim,
curve=curves,
self.shiftx = c(0.1,-0.1,0),
self.shifty = c(-0.1,-0.1,0.15),
self.arrpos = c(1,2.1,1))

3D pipe/tube plots in R - creating plots of tree roots

I'm trying to create 3D plots of simulated tree roots in R. Here is an example of a root system growing over time:
This is essentially a 3D network of cylinders, where the cylinder diameter (and, optionally, color) represents the size of the root. The available data includes:
x, y, z of the root centroid
direction of "parent" root (e.g. +x, -x, +y, -y, +z, -z), although this information could be captured in several different ways, including by calculating the x, y, z of the parent directly prior to plotting.
size of root
Example 3D data is here, but here is my first attempt at it in just 2D using ggplot2::geom_spoke:
dat <- data.frame(x = c(0,1,-1,0,1,-1),
y = c(-1,-1,-1,-2,-2,-2),
biomass = c(3,1.5,1.5,1,1,1),
parent.dir = c("+y","-x","+x","+y","+y","+y"))
dat$parent.dir <- as.numeric(as.character(factor(dat$parent.dir,
levels = c("-x", "+x", "-y", "+y"),
labels = c(pi, 0, pi*3/2, pi/2))))
ggplot(dat, aes(x = x, y = y)) +
geom_point(x = 0, y = 0, size = 20) +
geom_spoke(radius = 1,
aes(angle = parent.dir,
size = biomass)) +
coord_equal()
I prefer a solution based in the ggplot2 framework, but I realize that there are not a ton of 3D options for ggplot2. One interesting approach could be to creatively utilize the concept of network graphs via the ggraph and tidygraph packages. While those packages only operate in 2D as far as I know, their developer has also had some interesting related ideas in 3D that could also be applied.
The rgl library in seems to be the go-to for 3D plots in R, but an rgl solution just seems so much more complex and lacks the other benefits of ggplot2, such as faceting by year as in the example, easily adjusting scales, etc.
Example data is here:
I don't understand the format of your data so I'm sure this isn't the display you want, but it shows how to draw a bunch of cylinders in rgl:
root <- read.csv("~/temp/root.csv")
segments <- data.frame(row.names = unique(root$parent.direction),
x = c(-1,0,1,0,0),
y = c(0,1,0,0,-1),
z = c(0,0,0,0.2,0))
library(rgl)
open3d()
for (i in seq_len(nrow(root))) {
rbind(root[i,2:4],
root[i,2:4] - segments[root$parent.direction[i],]) %>%
cylinder3d(radius = root$size[i]^0.3, closed = -2, sides = 20) %>%
shade3d(col = "green")
}
decorate3d()
This gives the following display (rotatable in the original):
You can pass each cylinder through addNormals if you want it to look smooth, or use sides = <some big number> in the cylinder3d to make them look rounder.

Control plot layout in foodweb plot from mvbutils R-package

I would like to visualize how functions in my own R package depend on each other. For this purpose I use the foodweb() function from the mvbutils package.
I can get the right functional dependencies out without a problem but the plot looks a bit messy, with lines crossing each other and function names not aligned vertically or horizontally.
Is there a way to control the layout of the plot similar to the way this works in the igraph package?
Example
dirPath <- "~/dev/stackoverflow/46910042"
setwd(dirPath)
## Download example Package
urlPackage <- "https://github.com/kbroman/qtlcharts/archive/master.zip"
download.file(urlPackage, destfile = "master.zip")
unzip("./master.zip", exdir = dirPath, overwrite = TRUE)
## Install or load mcbutils
if (!require(mvbutils)) install.packages("mvbutils")
thefiles = list.files(path = "./qtlcharts-master/R/", full.names = TRUE)
thefiles
## Now we load all the package files into memory, so we can have
## foodweb generate a map of the package functions.
sapply(thefiles, source)
## Generate plot
par(mar = rep(0.1, 4))
foodweb(border = TRUE, boxcolor = "pink", lwd = 1.5, cex = 0.8)
Plot Output:
Michael,
One option is to look behind the curtains of foodweb. The mvbutils::foodweb function returns an object of (S3) class foodweb. This has three components:
funmat a matrix of 0s and 1s showing what (row) calls what (column). The dimnames are the function names.
x shows the x-axis location of the centre of each function’s name in the display, in par("usr") units
level shows the y-axis location of the centre of each function’s name in the display, in par("usr") units.
thus one approach we can take is to call foodweb but tell it not to create a plot rather return a foodweb object. This then allows us to manipulate the data directory or via graphics::plot() externally of the defaults provided by the mvbutils::foodweb() function.
Why? Well, to do what you suggest my sense is three options exist:
You can either play with mvbutils::foodweb() parameters.
Work with data structure returned with another plotting package.
Use graphics::par() and graphics::plot to manipulate the plot size and attributes of the foodweb structure returned.
It would be great to know your preference. Excluding, that my sense was to provide a base example:
Plot Package Example
In the case of using graphics::plot, you need to go look at how you manipulate graphics:par. par() allows you to set or query graphical parameters. For example, if we want to clean up the function plot you might choose to modify the grahics::par() fin parameter to increase the figure region dimensions, (width, height), in inches. A simple example but my sense it helps map out and demonstrate the options available to you.
## Generate plot
if (!require(qtlcharts)) install.packages("qtlcharts")
## Here we specify `asNamespace` to get the package internals
fw <- foodweb( where = asNamespace( "qtlcharts"),
plotting = FALSE,
)
#Display foodweb structure
str(fw)
# Expand plot figure region dimensions...
par(fin = c(9.9,7))
# Plot fw strucuture
plot(fw,
border = TRUE,
expand.xbox = 1,
boxcolor = "pink", lwd = 1.5, cex = 0.8)
Plot Output example
Note that the function names are not spaced out. Note I cut the top and bottom white of plot here. In this case, you can play with the par constraints such as margin to get the plot you want.
Pruning your plot
Another option within the constraints of mvbutils::foodweb is to use the prune and rprune option to simplify your plots. These are super poweful and useful especially the regular expression version.
if (!require(qtlcharts)) install.packages("qtlcharts")
fw <- foodweb( where = asNamespace( "qtlcharts"),
plotting = FALSE)
str(fw)
par(fin = c(9.9,7))
plot(fw,
border = TRUE,
expand.xbox = 1,
boxcolor = "pink", lwd = 1.5, cex = 0.8)
fw <- foodweb( where = asNamespace( "qtlcharts"),
rprune = "convert_", ## search on `convert_` to negate use `~convert_`
plotting = FALSE)
str(fw)
par(fin = c(9.9,7))
plot(fw,
border = TRUE,
expand.xbox = 1,
boxcolor = "pink", lwd = 1.5, cex = 0.8)
Hoping the above information points you in the right direction.
T.
Because of the fact that there are many data, connections etc, the plot is squeezed in order to fit in the screen, hence it becomes messy.
What I would suggest is to save it in a PDF or PNG with big enough width and Height and then you can zoom in. This will save you a lot of time.
E.G.
## Generate plot
pdf( "mygraph.pdf", width = 50, height = 80 )
par(mar = rep(0.1, 4))
foodweb(border = TRUE, boxcolor = "pink", lwd = 1.5, cex = 0.8)
dev.off()
In addition, you can play with the plot options of foodweb.
Hope it helps.

manipulate text edge labels ctree

Got a ctree with four labels, but the categories are long text therefore just the first is shown.
category;presence;ratio;tested;located
palindromic_recursion;1;0;0;0
conceptual_comprehension;0;1;0;0
infoxication_syndrome;0;0;1;0
foreign_words_abuse;0;0;0;1
palindromic_recursion;1;0;0;0
conceptual_comprehension;0;1;0;0
infoxication_syndrome;0;0;1;0
foreign_words_abuse;0;0;0;1
concepts.ctree <- ctree(category ~., data)
plot(concepts.ctree)
is there any way or parameter for manipultating (rotate) text, edge label names and this way force them to be all shown in plot?
My real data is much bigger but this sample is ok to test it if you do not use zoom tool.
Regards
There wasn't an option for this up to now. But I just tweaked the development version of partykit on R-Forge to support this feature. Currently, the package is re-building but hopefully you can soon say install.packages("partykit", repos = "http://R-Forge.R-project.org") - or if you don't want to wait that long, simply check out the SVN and re-build yourself.
In the new version, you can pass the rot and just arguments to grid.text() to control rotation and justification of the x-axis labels.
Read the data:
data <- read.csv2(textConnection(
"category;presence;ratio;tested;located
palindromic_recursion;1;0;0;0
conceptual_comprehension;0;1;0;0
infoxication_syndrome;0;0;1;0
foreign_words_abuse;0;0;0;1
palindromic_recursion;1;0;0;0
conceptual_comprehension;0;1;0;0
infoxication_syndrome;0;0;1;0
foreign_words_abuse;0;0;0;1"
))
Fit the tree (using the partykit implementation of ctree()):
library("partykit")
concepts.ctree <- ctree(category ~ ., data = data)
For visualization create a viewport with sufficiently large margins on the x-axis first. Then, add the tree to the existing viewport page and set the rotation/justification arguments for the barplot.
pushViewport(plotViewport(margins = c(6, 0, 0, 0)))
plot(concepts.ctree, tp_args = list(rot = 45, just = c("right", "top")),
newpage = FALSE)

Plotting directed multigraphs in R

I've never used any graph plotting package in R, I'm familiar with basic plotting commands and with ggplot2 package. What I've found (but not tried out yet) are Rgraphviz, network and igraph packages. So I'd like to ask you, which package has simplest learning curve and satisfies following requirements:
Has simple layout engines (spring layout, random, ...)
Tries to draw multiple edges between two vertices so that they would not overlap. As a bonus it would be nice to being able to adjust this.
Can draw loops.
Vertex and edge labels, vertex and edge size and color are adjustable.
(No need for any of the graph algorithms like link analysis, shortest path, max flow etc, but nice, if present)
The igraph package seems to fulfill your requirements, with the tkplot() function helping adjusting the final layout if needed.
Here is an example of use:
s <- cbind(A=sample(letters[1:4], 100, replace=TRUE),
B=sample(letters[1:2], 100, replace=TRUE))
s.tab <- table(s[,1], s[,2])
library(igraph)
s.g <- graph.incidence(s.tab, weighted=T)
plot(s.g, layout=layout.circle,
vertex.label=c(letters[1:4],letters[2:1]),
vertex.color=c(rep("red",4),rep("blue",2)),
edge.width=c(s.tab)/3, vertex.size=20,
vertex.label.cex=3, vertex.label.color="white")
With the interactive display (there's a possibility of using rgl for 3D display), it looks like (I have slightly moved one vertex afterwards):
tkplot(s.g, layout=layout.circle, vertex.color=c(rep("red",4),rep("blue",2)))
Finally, you can even export you graph into most common format, like dot for graphviz.
The multigraph R package can be useful as well. For the above example bmgraph plots such graph:
library("multigraph")
bmgraph(s.tab, layout = "circ", pch = 16:16, pos = 0, vcol = 6:7, lwd = 3, cex = 9)
And for a directed version:
bmgraph(s.tab, "circ", pch = 16:16, pos = 0, vcol = 6:7, lwd = 3, cex = 9, directed = TRUE)

Resources