Adding a Sample Space to a Venn Diagram in R - 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)))

Related

Align plotly plots in R-Markdown

Can someone tell me if there is a way to align plotly plots in R-Markdown?. More specifically: Currently my plots are being placed one after another. I would like to have a grid-like format. I achieved this before, using the simple plot function, but that doesn't seem to work with plotly.
The following is an example of my prior code, that worked with the simple plot function. Can I make that work, assuming I were using plotly?
Thanks in advance.
{r comment = NA,fig.width=14, fig.height=14}
layout(matrix(c(1,2,3,4,5,6,7,8), 4, 2, byrow = TRUE))
m_alt <- miete[miete$baujahr <= 22,]
m_neu <- miete[miete$baujahr > 22,]
plot(table(m_alt$bezirk),main="",ylab="Frequency",xlab="Bezirk")
plot(table(m_neu$bezirk),main="",ylab="Frequency",xlab="Bezirk")
plot(table(m_alt$wohnflaeche),main="",ylab="Frequency",xlab="Wohnfläche")
plot(table(m_neu$wohnflaeche),main="",ylab="Frequency",xlab="Wohnfläche")
plot(table(m_alt$wohnlage,m_alt$zimmerzahl),main="Altbau: Wohnlage u. Zimmerzahl")
plot(table(m_neu$wohnlage,m_neu$zimmerzahl),main="Neubau: Wohnlage u. Zimmerzahl")
plot(table(miete$bezirk,miete$zimmerzahl),main="Bezirk u. Zimmerzahl")
plot(table(miete$warmwasser,miete$bezirk),main="Warmwasser u. Bezirk")

R Euler Plot Basic Questions

My question is about the inputs into library(eulerr) in r. I am trying to plot something I think would be simple but I am having trouble and it stems from my lack of knowledge of r and this library.
For this code :
fit1 <- euler(c("A" = 25, "B" = 5, "A&B" = 5))
plot(fit1)
I would think it would produce two circles with the following characteristics:
Circle A would be large
Circle B would be small
Circle B would be completely inside of A since they "share" all 5
However, circle B is not entirely inside of A and I am not sure why. More to the point I would like to get my intended output which would be a figure that satisfied the three bullet points above. Please help. Thanks.
To get more sense of the eulerr parameters you can check this shiny app. For the particular diagram you want to make you need to do:
fit1 <- euler(c("A" = 25, "B" = 0, "A&B" = 5))
plot(fit1)

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

Partially superposing data in lattice's xyplot()

Please, reproduce this code:
install.packages('lattice')
install.packages('zoo')
require(lattice)
require(zoo)
X <- matrix(runif(25 * 8), ncol = 8)
(Its purpose is just to load packages and to create a matrix with 8 columns).
Using zoo it is very easy to create such a plot:
plot.zoo(X, screen = c(1,1,2,2,3,3,4,4), col = c(1,2))
How can I make the same with lattice's xyplot() function?
This can be done via zoo:::xyplot.zoo: as reported in zoo package documentation, xyplot.zoo has xyplot methods for time series objects.
Then, for what concerns the above question, it is possible to use:
xyplot(as.zoo(X), screen = c(1,1,2,2,3,3,4,4), col = c(1,2))
to produce a trellis object like in lattice selecting the desired layout with the screen argument.

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