Rstudio Venn Diagram with several numbers - r

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)

Related

Adjust the relative positions of Venn diagram using R Venn.diagram package

I would like to draw a Venn Diagram using the R Venn.digram package. Here is my code.
library(VennDiagram)
myCol2 <- brewer.pal(3, "Pastel2")
list1 <- c(1:179)
list2 <- c(171:224)
list3 <- c(1:17, 171, 172, 225:230)
venn.diagram(
x = list(list1, list2, list3),
category.names = c("list1", "list2", "list3"),
filename = 'three comparison.png',
output=TRUE,
resolution = 600,
cex = 1.8, # size of numbers in the cycles
# sub.fontfamily = "serif",
fontfamily ="Arial",
main.fontfamily="serif",
cat.cex = 1.2, # size of category names
fill = myCol2
)
Here is my Venn diagram output.
Is there a way to rotate two cycles corresponding to list2 and list3 to make the plot look like the following? - Basically, I don't want three centers on the same line.
Thanks Dominik Rafacz for introducing the ggvenn package. It works.
library(ggvenn)
a <- list(list1 = c(1:179),
list2 = c(171:224),
list3 = c(1:17, 171, 172, 225:230))
ggvenn(a, c("list1", "list2", "list3"),show_percentage = F)

Soil profiles with coloured volume fractions with "aqp" in R

I am trying to plot a soil profile in R using the package aqp: algorithms for quantitative pedology. The profile should represent matrix colour, plus mottling colour and percentage. For that purpose, I am using the function addVolumeFraction, which works well to some extent: it plots points on the profile that correspond to the right mottling percentage for each horizon, but it doesn't assign the corresponding colours. Here an example:
#Variables for the soil profile
id <- rep(1, 4)
hor <- c("H1", "H2", "H3", "H4")
tops <- c(0,15,35,60)
bottoms <- c(15, 35, 60, 95)
mx_Hex <- c("#695F59FF", "#A59181FF", "#9E9388FF", "#A59181FF")
mot_Hex <- c("#EEB422","#EEB422", "#CD4F39", "#CD4F39")
mot_perc <- c(5, 10, 40, 8)
#Soil profile df
soildf <- data.frame(id,hor,tops,bottoms, mx_Hex, mot_Hex, mot_perc)
soildf$mx_Hex <- as.character(mx_Hex) #the class "SoilProfileCollection" needs colors as characters
soildf$mot_Hex <- as.character(mot_Hex)
# Transform df to "SoilProfileCollection"
depths(soildf) <- id ~ tops + bottoms
#Plot
plot(soildf, name = "hor", color = "mx_Hex", divide.hz = TRUE)
addVolumeFraction(soildf, "mot_perc",pch = 19, cex.min = 0.4, cex.max = 0.5, col = soildf$mot_Hex)
Soil profile plot
As you can see on the plot, the mottles' colours are mixed along the profile. I would like to have mottles of a given colour for their corresponding horizon instead. Can anybody help me to solve this problem?
Thanks!!
This works as expected in the current version of aqp available on CRAN (v1.19 released in January 2020).
I modified your example below to use alternating black and white mottles in each horizon.
library(aqp)
#Variables for the soil profile
id <- rep(1, 4)
hor <- c("H1", "H2", "H3", "H4")
tops <- c(0,15,35,60)
bottoms <- c(15, 35, 60, 95)
mx_Hex <- c("#695F59FF", "#A59181FF", "#9E9388FF", "#A59181FF")
# change mottle colors to something obviously different in each horizon
mot_Hex <- c("#FFFFFF", "#000000", "#FFFFFF","#000000")
mot_perc <- c(5, 10, 40, 8)
#Soil profile df
soildf <- data.frame(id, hor, tops, bottoms, mx_Hex, mot_Hex, mot_perc)
#the class "SoilProfileCollection" needs colors as characters
soildf$mx_Hex <- as.character(mx_Hex)
soildf$mot_Hex <- as.character(mot_Hex)
# Transform df to "SoilProfileCollection"
depths(soildf) <- id ~ tops + bottoms
#Plot
plot(soildf,
name = "hor",
color = "mx_Hex",
divide.hz = TRUE)
addVolumeFraction(
soildf,
"mot_perc",
pch = 19,
cex.min = 0.4,
cex.max = 0.5,
col = soildf$mot_Hex
)
alternating mottles

Plotting subset os subset in R

I have a data.frame like following:
files
Total 1000
Subset1 587
Subset2 123
I would like to represent the above data frame in a such way that of 123 files is a subset of 587 which itself subset of 1000. When I use pie or bar graphs, it is misleading.
My sincere apologies if my question is very amateurish. Kindly guide me how can represent the above data in R plots.
Perhaps something like this:
df = data.frame(files=c(1000,587,123),row.names = c('total','subset1','subset2'))
library(VennDiagram)
draw.triple.venn(area1 = df$files[1], area2 = df$files[2], area3 = df$files[3],
n12 = 587, n23 = 123, n13 = 123, n123 = 123,
category = c("Total", "Subset1", "Subset2"),
lty = "blank", fill = c("skyblue", "pink1", "mediumorchid"),
cat.pos = 0,cat.dist = c(-0.02,-0.05,-0.02))
Result:
You could do it as follows:
df$files[1] <- df$files[1] - sum(df$files[-1])
pie(df$files, df$sets)
The result:
Data:
df <- read.table(text=" sets files
Total 1000
Subset1 587
Subset2 123 ", header=TRUE)

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?

Programmatically create Venn diagrams at scale?

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)

Resources