Programmatically create Venn diagrams at scale? - r

I would like to create a Venn Diagram like the following:
The challenge is to have the circles and intersections with the right proportions. That is:
Group 1 should be 15% of the universe, group 2 83%, and group 3 30%.
The intersection between G1 G2 and G3 should be 7% of the universe
etc
Is there a way of doing this? This is what I have right now:
library(VennDiagram)
grid.newpage()
draw.triple.venn(area1 = 15, area2 = 83, area3 = 30, n12 = 13, n23 = 28, n13 = 7,
n123 = 7, category = c("Group 1", "Group 2", "Group 3"), lty = "blank",
fill = c("skyblue", "pink1", "mediumorchid"))

This works a bit better, not sure if the overlap is completely correct though
library(venneuler)
venn <- venneuler(c(A=83, B=30, C=15, "A&B"=28, "B&C"=7, "A&C"=13, "A&B&C"=7))
plot(venn)

Related

Plotrix labels, adjusting the size/font/position?

I have been trying to create a risk radar using plotly and now plotrix, I have encountered limitiations with both (based on my need and also my skillset with R).
With plotly I had pretty much most of the things i wanted except the deal breaker was not being able to label the radial axis (Team A, Team B etc.)
My version of this using plotrix is almost where i need to be and just need some guidance to get me over the line?
I have 4 issues:
Can the title be moved to the left or right?
The radial? labels are bleeding into the chart circle making it hard
to read, can they be adjusted somehow?
Is it possible to change the font (size and/or colour) of the labels 0/30/60/90/180?
Is there anyway to add text to the plotted point, in my case i
wanted to have the RiskID as the labels
My chart looks like this:
library(plotrix)
# Build sample dataset
aRiskID <- c(1, 15, 23, 28, 35)
bRiskDays <- as.numeric(c(28, 15, 85, 153, 100))
cTheta <- as.integer(c(20, 80, 130, 240, 320))
dConsequence <- c("Major", "Major", "Minor", "Moderate", "Minor")
myRisks <- data.frame(RiskID = aRiskID, RiskDays = bRiskDays, Theta = cTheta, CurrentConsequence = dConsequence)
myLabels <- c("Team A", "Team B", "Team C", "Team D", "Team E", "Team F", "Team G", "Team H")
# Test different point colours
# initializing vector of colors as NA
colors_plot <- rep(NA,length(myRisks))
# set of conditions listed in the plot
colors_plot[myRisks$CurrentConsequence == "Major"] <- "black"
colors_plot[myRisks$CurrentConsequence == "Moderate"] <- "red"
colors_plot[myRisks$CurrentConsequence == "Minor"] <- "green"
# add more conditions as needed
# par(mar=c(2,5,5,5))
# plot the chart
radial.plot(myRisks$RiskDays,
myRisks$Theta,
start = pi/2,
clockwise = FALSE,
# start=pi/2,clockwise=TRUE,
show.grid.labels=1,
rp.type="s",
main="Risk Radar",
radial.lim=c(0,30,60,90,180),
radlab = TRUE,
point.symbols=17,
point.col=colors_plot,
cex = 2,
cex.axis = 0.25,
cex.lab = 0.25,
lwd=par("lwd"),mar=c(2,2,3,2),
# show.centroid=TRUE,
labels=myLabels)
I don't know where else to go with this and so any tips using plotrix or another charting package to achieve the end result would be great.
You should look at the functions radial.plot.labels and radial.grid
# plot the chart
radial.plot(myRisks$RiskDays,
myRisks$Theta,
start = pi/2,
clockwise = FALSE,
# start=pi/2,clockwise=TRUE,
show.grid.labels=1,
rp.type="s",
# main="Risk Radar",
radial.lim=c(0,30,60,90,180),
radial.labels = '',
radlab = TRUE,
point.symbols=17,
point.col=colors_plot,
cex = 2,
cex.axis = 0.25,
cex.lab = 0.25,
lwd=par("lwd"),mar=c(2,2,3,2),
# show.centroid=TRUE,
labels=NULL, label.pos = pi / 4 * 2:9)
# 1
mtext("Risk Radar", at = par('usr')[1], font = 2)
# 2
at <- c(0,30,60,90,180)
radial.plot.labels(max(at) + 35, pi / 4 * 2:9, labels = myLabels, radial.lim = at)
# 3
radial.plot.labels(at, pi / 2 * 3, labels = at, col = 1:5, cex = 1.5)
# 4
radial.plot.labels(myRisks$RiskDays, myRisks$Theta, start = pi/2,
clockwise = FALSE, labels = myRisks$RiskID)
If you really need perpendicular labels, you can use the radial.grid function or loop over the labels with separate rotations (srt). It's a real shame that srt isn't vectorized in text, it would make this a lot easier
th <- pi / 4 * 2:9
sapply(seq_along(th), function(ii) {
i <- ifelse((th[ii] > pi / 2) & (th[ii] < pi / 2 * 3), pi, 0)
radial.plot.labels(max(at) + 35, th[ii], labels = myLabels[ii],
radial.lim = at, srt = (th[ii] - i) * 180 / pi)
})
I accidentally made this lovely snowflake #accidental__aRt:
th <- pi / 4 * 2:9
sapply(th, function(x)
radial.plot.labels(max(at) + 35, pi / 4 * 2:9, labels = myLabels,
radial.lim = at, srt = x * 180 / pi))

Change direction of axis title and label in 3dplots in R?

I have a question that might be easy for a person who is expert in R plot. I need to draw 3D plot in R. My data is as follows:
df <- data.frame(a1 = c(489.4, 505.8, 525.8, 550.2, 576.6),
a2 = c(197.8, 301, 389.8, 502, 571.2),
b1 = c(546.8, 552.6, 558.4, 566.4, 575),
b2 = c(287.2, 305.8, 305.2, 334.4, 348.6), c1 = c(599.6, 611.4,
623.6, 658, 657.4), c2 = c(318.8, 423.2, 510.8, 662.4, 656),
d1 = c(616, 606.8, 600.2, 595.6, 595),
d2 = c(242.4, 292.8, 329.2, 378, 397.2),
e1 = c(582.4, 580, 579, 579, 579),
e2 = c(214, 255.4, 281.8, 303.8, 353.8))
colnames(df) <- rep(c("V1", "V2"), 5)
df.new <- rbind(df[, c(1, 2)],df[, c(3, 4)],df[, c(5, 6)],
df[, c(7, 8)],df[, c(9, 10)])
df.new$Group <- factor(rep(c("a","b","c","d","e"), each = 5))
df.new$Class <- rep(c(1:5), 5)
I am drawing a 3D Plot using scatterplot3d package.
x=df.new$Class
y=V1
z=V2
scatterplot3d(x,y,z, pch = 16, color=colors,main="3D V1 v.s V2",xlab =
"Class",ylab = "V1", zlab = "V2")
Now I want to do 2 modifications. One is to make the vertical title of those axis horizontal and the next is to put a label for values of x and for example put a label "first interval" for 1 in x values and so one and so forth. How an I do it?
Also, how can I make the points linear or plane instead of dots.
One is to make the vertical title of those axis horizontal
To do this you need to hide the current label, and use the text() function to add a rotated label in the correct spot; as described here Rotate y-axis label in scatterplot3d (adjust to angle of axis)
set.seed(42)
scatterplot3d(rnorm(20), rnorm(20), rnorm(20), ylab = "")
text(x = 5, y = -2.5, "Y-axis", srt = 45)
and for example put a label "first interval" for 1 in x values and so one and so forth. How an I do it?
From the documentation - https://cran.r-project.org/web/packages/scatterplot3d/scatterplot3d.pdf
Use the x.ticklabs attribute, for example:
xlabs <- c("first interval", "second", "third", "fourth", "this is 5")
scatterplot3d(x,y,z, pch =16,main="3D V1 v.s V2",xlab = "Class",ylab = "V1", zlab = "V2", x.ticklabs=xlabs)
Also, how can I make the points linear or plane instead of dots.
Scatterplot3d offers "lines" and "vertical lines", for example:
scatterplot3d(x,y,z , type="l", lwd=5, pch=" ")
#or
scatterplot3d(x,y,z , type="h", lwd=5, pch=" ")

Rstudio Venn Diagram with several numbers

I Have two dataset, where im counting the number of each sample within that dataset. And i want to combine the numbers of each sample for both dataset and then create a VennDiagram using R, with the main numbers of the largest dataset and in parenthesis the smaller dataset numbers.
The R code i'm currently using now
grid.newpage()
temp <- draw.triple.venn(area1 = 428,
area2 = 145,
area3 = 303,
n12 = 26,
n23 = 16,
n13 = 14,
n123 = 9,
category = c("text1", "text2", "text3"),
scaled=FALSE,
ext.text=FALSE)
grid.draw(temp)
pdf(file="figure1.pdf")
grid.draw(temp)
dev.off()
The result i get
What i would like to create
I have looked at the eulerr package and the Venn diagram package but i cant seem to figure out if there is a way to make this possible.
All help is much appreciated. Many thanks.
Not sure what the 'small' dataset looks like, but this does the trick
Here, you add traces to the grid with custom labels, and y-coordinates proportional to the y-coordinates of you 'large' dataset
library(VennDiagram)
temp <- draw.triple.venn(area1 = 428,
area2 = 145,
area3 = 303,
n12 = 26,
n23 = 16,
n13 = 14,
n123 = 9,
category = c("text1", "text2", "text3"),
scaled=FALSE,
ext.text=FALSE)
temp[[17]] <- temp[[13]]
temp[[17]]$y <- temp[[13]]$y * 0.8
temp[[17]]$label <- '(3)'
temp[[18]] <- temp[[9]]
temp[[18]]$y <- temp[[9]]$y * 0.95
temp[[18]]$label <- '(4)'
temp[[19]] <- temp[[7]]
temp[[19]]$y <- temp[[7]]$y * 0.95
temp[[19]]$label <- '(5)'
grid.draw(temp)

R Highcharter mosaïc / marimekko chart

i tried to create a marimekko chart in R Highcharter, following this example :
http://jsfiddle.net/highcharts/h2np93k1/
I cannot seem to get the sortIndex of the treemap to work, my code is as follows:
parentid <- c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
sortIndex <- c(0, 1, 2, 3, 4, 0, 1, 2, 3, 4)
child <- c("Alpha", "Alpha", "Alpha", "Alpha", "Alpha", "Beta", "Beta", "Beta", "Beta", "Beta")
childid <- c(100, 100, 100, 100, 100, 200, 200, 200, 200, 200)
colorid <- c(100, 100, 100, 100, 100, 200, 200, 200, 200, 200)
parent <- c("Parent 1", "Parent 2", "Parent 3", "Parent 4", "Parent 5", "Parent 1", "Parent 2", "Parent 3", "Parent 4", "Parent 5")
value <- c(10, 60, 70, 20, 90, 50, 30, 10, 90, 10)
data <- data.frame(parentid, sortIndex, child, childid, colorid, parent, value)
hctreemap2(data, group_vars=c("parentid", "childid"),
size_var="value",
color_var="colorid",
layoutAlgorithm='stripes',
alternateStartingDirection = T,
stacking="percent",
levelIsConstant = F,
sortIndex=sortIndex,
levels = list(
list(level=1, dataLabels = list(enabled=T, align='left', verticalAlign='top'), borderWidth=3),
list(level=2, dataLabels = list(enabled=T))))
does anyone have any ideas?
I realize this question was several years ago, and Highcharter has changed since it was written.
This answer may not have worked in 2018, but it does work now.
At some point hctreemap and hctreemap2 were deprecated. The instructions from Highcharter are to use data_to_hierarchical to prepare the data and then use either hchart() or highchart() to create the treemap. However, this method will strip the sortIndex, so I don't think that's the route you would want to go.
Instead, I prepared the data by formatting it as it is in the JS link you provided and then graphed it.
The data:
# collect hc colors (like they did in the example)
colrs <- getOption("highcharter.color_palette")
# two data frames, one for each level
# id isn't as important until you go beyond 2 levels
pars <- data.frame(id = unique(data$parent),sortIndex = unique(data$sortIndex))
kids <- data.frame(
name = data$child, parent = data$parent, sortIndex = data$sortIndex,
value = data$value, color = data$color/100) %>% mutate(color = colrs[color])
# this assumes data is already sorted by sort order*
newData <- list() # for storing the data as hierarchical
invisible(map(1:nrow(pars),
function(j) {
p <- pars[j, ]$id # collect par id to find children
k <- kids[kids$parent == p, ] # isolate applicable children
pl <- list_parse(pars[j, ]) # make row a list
kl <- list_parse(k) # make each child row a list
newData <<- append(newData, pl) # add parent
newData <<- append(newData, kl) # add that parent's children
}))
Now the data is ready for plotting.
hchart(newData, type ="treemap", layoutAlgorithm = "stripes",
alternateStartingDirection = T)

Redrawing venn diagram

What would be the easiest way to redraw a venn diagram using R ? I do not have a data which was used to generate venn diagram but the rest of diagrams were drawn in R... I would like to keep the same structure so it means I have to somehow redraw it in R.
Do you have any idea what would be the easiest way to do it ?
That's a code which I have been using for other venn diagrams.
v1 <- venn.diagram(list(1=a, 2=b, 3=c, 4=d), filename=NULL, fill=rainbow(4), cex.prop=NULL, cex=1.5)
png("TEST.png", width=7, height=7, units='in', res=150)
grid.newpage()
grid.draw(v1)
dev.off()
You can make a call to the draw.venn.* functions and input overlap areas directly. It will be tedious but i think it is your only option. you'll notice the order of groups is different, there maybe a way to control that but I'm not sure what that is at the moment.
a <- c(2411, 12433,939,238, 1575,2483,2923,540)
b <- c(1575, 2483,2923, 540, 1255, 1330, 615, 622)
c <- c(1247, 1330, 2483, 12433, 150, 615, 2923, 939)
d <- c(150,615,2923,939, 1245, 622, 540, 238)
draw.quad.venn(area1 = sum(a), area2 = sum(b), area3 = sum(c), area4 = sum(d),
n12 = sum(a[5:8]), n13 = sum(a[c(2,3,6,7)]), n14 = sum(a[c(3,4,7,8)]),
n23 = sum(b[c(2,3,6,7)]), n24 = sum(b[c(3,4,7,8)]),
n34 = sum(c[5:8]),
n123 = sum(a[6:7]),
n134 = sum(a[c(3,7)]),
n124 = sum(a[7:8]),
n234 = sum(b[c(3,7)]),
n1234 = 2923, category = c("A","B","C","D"),
fill = colorspace::rainbow_hcl(4),
col = colorspace::rainbow_hcl(4)[c(1,3,4,2)], lwd = rep(1, 4))
have you tried the Venn Diagram package
https://cran.r-project.org/web/packages/VennDiagram/VennDiagram.pdf
and see this too
Venn Diagrams with R?

Resources