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

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))

Related

Combining forest plot and traffic light plot in meta-analysis using R

The software Revman produces a combination of forest plots and traffic light plots in meta-analyses (see e.g. doi: http://dx.doi.org/10.1136/bmjopen-2018-024444, Fig. 3). I am using the package meta fot meta-analysis and robvis for risk of bias. Here is some simple code using example data from the packages:
#creating a forest plot
library(meta)
m <- metacont(n.amlo, mean.amlo, sqrt(var.amlo),
n.plac, mean.plac, sqrt(var.plac),
data = amlodipine, studlab = study)
forest(m)
#creating a risk of bias traffic light plot
library(robvis)
rob_traffic_light(data_rob2, tool = "ROB2")
The important thing is that the rows (i.e. each single study) from the forest plot and the rows from the risk of bias plot are aligned. The problem with using packages such as gridExtra is that you have to play around with the alignment and size or the plots until it fits. My question is if there is a good workaround in R to get plots that are similar to the Revman-plots. The result should look like the figure below.
Here are some ideas to start building your plot.
library(meta)
data(amlodipine)
m <- metacont(n.amlo, mean.amlo, sqrt(var.amlo),
n.plac, mean.plac, sqrt(var.plac),
data = amlodipine, studlab = study)
library(ggplotify)
p1 <- as.ggplot(~forest(m), scale = 1, hjust = 0, vjust = 0)
library(robvis)
p2 <- rob_traffic_light(data_rob2, tool = "ROB2")
library(patchwork)
graphics.off()
dev.new(width=15,height=6)
wrap_plots(p1, p2, widths=c(9,2), heights=c(15,1))

Adding a Sample Space to a Venn Diagram in R

I am trying to insert of a simple Venn diagram in an jupyter notebook on R. I have been able to generate a simple 2-set diagram using the VennDiagram library. However I can't seem to figure out how to work the triple diagram in way that yields 2 intersecting sets that are in a subset of another larger set.
Maybe I'm using the wrong library?
Edit:
This is for an illustration, I just need to draw an example of a Venn Diagram. The data would be something like:
S=(1,2,3)
A=(1,2)
B=(2,3)
The latest development version of my r package eulerr is now able to take a list of sample spaces as input. It, however, produces euler diagrams (proportional Venn diagrams) (which is why your specifications won't result in two diagrams intersecting within another).
# devtoools::install_github("jolars/eulerr")
library(eulerr)
ll <- list(S = c(1, 2, 3), A = c(1, 2), B = c(2, 3))
fit <- euler(ll)
plot(fit)
If you want two intersecting circles within a third, try the following:
plot(euler(c(S = 5, "A&B&S" = 3, "A&S" = 1, "B&S" = 1)))

Rotate x-axis of r partykit tree plot

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")))

Combine two plots created with effects package in R

I have the following Problem. After running an ordered logit model, I want to R's effects package to visualize the results. This works fine and I did so for two independent variables, then I tried to combine the two plots. However, this does not seem to work. I provide a little replicable example here so you can see my problem for yourself:
library(car)
data(Chile)
mod <- polr(vote ~ age + log(income), data=Chile)
eff <- effect("log(income)", mod)
plot1 <- plot(eff, style="stacked",rug=F, key.args=list(space="right"))
eff2 <- effect("age", mod)
plot2 <- plot(eff2, style="stacked",rug=F, key.args=list(space="right"))
I can print these two plots now independently, but when I try to plot them together, the first plot is overwritten. I tried setting par(mfrow=c(2,1)), which didn't work. Next I tried the following:
print(plot1, position=c(0, .5, 1, 1), more=T)
print(plot2, position=c(0,0, 1, .5))
In this latter case, the positions of the two plots are just fine, but still the first plot vanishes once I add the second (or better, it is overwritten). Any suggestions how to prevent this behavior would be appreciated.
Reading down the long list of arguments to ?print.eff we see that there are some arguments for doing just this:
plot(eff, style="stacked",rug=F, key.args=list(space="right"),
row = 1,col = 1,nrow = 1,ncol = 2,more = TRUE)
plot(eff2, style="stacked",rug=F, key.args=list(space="right"),
row = 1,col = 2,nrow = 1,ncol = 2)
The reason par() didn't work is because this package is using lattice graphics, which are based on the grid system, which is incompatible with base graphics. Neither par() nor layout will have any effect on grid graphics.
This seems to work:
plot(eff,col=1,row=2,ncol=1,nrow=2,style="stacked",rug=F,
key.args=list(space="right"),more=T)
plot(eff2,col=1,row=1,ncol=1,nrow=2,style="stacked",rug=F,
key.args=list(space="right"))
edit: Too late...

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