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?
Related
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)
I have been struggling to plot a 3D chart in R for a while. I think I am very close to what I want. I have asked a question before. What I need to know now is only how to convert the scatterplots with dots to linear. I mean if only I can connect the points it is great for me. What I have now looks like below:
I need to connect the points which has a better view in 3D. I need a separate line for each color and I want to add legend to the chart.
I have defined my data as:
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)
x=df.new$Class
y=V1
z=V2
Below is my code:
library(scatterplot3d) #colors
colors <- c("#999999", "#E69F00", "#56B4E9","#1B9E77", "#D95F02")
colors <- colors[as.numeric(df.new$Group)]#Others
xlabs <- c("[7,9]", "[10,12]", "[16,18]", "[19,21]", "[22,24]")
scatterplot3d(x,y,z, pch = 16, color=colors,main="Title",xlab
="Intervals",ylab = "", zlab = "Total time", x.ticklabs=xlabs)
text(8, 2.4, "c",cex = 1)
text(9, 2, "c",cex = 1)
I really appreciate if someone can help me about this issue that I have been struggling. I know there is a type=1 but it makes just one unified plot.
Try this:
sd<-scatterplot3d(x,y,z, pch = rep(16:12, each=5), color=colors,main="Title",xlab
="Intervals",ylab = "", zlab = "Total time", x.ticklabs=xlabs)
sd$points3d(x[1:5],y[1:5],z[1:5], col="purple", type="l")
sd$points3d(x[6:10],y[6:10],z[6:10], col="orange", type="l")
sd$points3d(x[11:15],y[11:15],z[11:15], col="blue", type="l")
sd$points3d(x[16:20],y[16:20],z[16:20], col="green", type="l")
sd$points3d(x[21:25],y[21:25],z[21:25], col="red", type="l")
legend("right", legend = levels(df.new$Group), col= levels(as.factor(colors)),pch = rep(16:12, each=1))
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=" ")
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)
I have this dataset:
sample <- structure(list(A = c(1415.6, 1345.3, 1321.7, 1234.5, 1567.8,
1476.6, 1610.1, 1422.6, 1209.1, 1249.3, 1377.5, 1525.7, 1683.7,
1500.1, 1565.3, 1737.4, 1321, 1477.8, 1642, 1608.1, 1427.8, 1608.2,
1404.4, 1688.3, 1795.4), B = c(98, 457, 756, 971, 1148, 4260,
16307, 42614, 69787, 76301, 80491, 82267, 83975, 85310, 86322,
94492, 98798, 102514, 126045.986, 160848.998, 183607.7625, 212747.9255,
249117.2874, 306092.91, 339609.8663), C = c(1.2397, 1.5526, -0.1829,
-0.3298, -0.1945, 2.8669, 1.3536, 0.781, 0.0324, -1.4283, -0.4413,
-0.8583, -0.039, -0.2464, -0.277, 2.0885, -0.6405, -0.1474, 1.8457,
0.3913, -0.4248, 0.2472, 0.2216, 0.4489, -0.5306)), .Names = c("A",
"B", "C"), class = "data.frame", row.names = c(NA, -25L))
and I want to change the plot region colour in vis.gam function (invert the grey colour - the higher the number in the plot contours the darker the colour of the plot region and vice versa):
library(mgcv)
m0 <-gam(C ~ A + B, data = sample)
vis.gam(m0, plot.type="contour", color="gray")
I would like to just invert the colour palette. If not possible, change it manually.
I've tried somethoing like this (I choosed names only by chance)
vis.gam(m0,plot.type="contour", col=c("#FFFFFF", "#F7F7F7", "666666"))
vis.gam(m0,plot.type="contour", col=("grey25", "grey26", "grey27"))
but that is not working.
Unfortunately the definition of vis.gam doesn't allow what you want. Fortunately, it is fairly easy to modify this function to do what you want:
# first get the definition of vis.gam
newDef <- deparse(vis.gam)
# change the line defining the direction of the grey gradient
newDef[grep("gray\\(seq\\(",newDef)] <- " pal <- gray(seq(0.9, 0.1, length = nCol))"
# then define a new function with this new definition
vis.gam2 <- eval(parse(text=newDef))
Now using vis.gam2 will do what you want:
vis.gam2(m0, plot.type="contour", color="gray")