I use VennDiagram to make a venn diagram with the following example code:
library(VennDiagram)
venn.diagram(list(shams_90d = 1:3, shams_90d_4h = 2:4, sham3__shams_90d = 3:5,
sham3_90d__shams = 5:7, sham3_90d__shams_4h = 6:9),
fill = c("red", "green", "blue", "yellow", "purple"),
alpha = c(0.5, 0.5,0.5, 0.5, 0.5), cex = 1,cat.fontface = 2,
lty =1, filename = "trial2.emf");
Which gives this figure:
The names on the left and right of the figure are cut off, and a little bit of the name at the bottom as well. I tried changing width, but that makes the venn diagram itself get wider, and the names still get cut off.
How can I make the VennDiagram so that it includes the full names, either by adding more whitespace on the left and right of the diagram, or by pushing the names more towards the venn diagram?
You can justify the label text with cat.just. The package reference manual gives info. on how to pass the parameters.
For your example i used trial and error for the justification values.
# Plot
v <- venn.diagram(list(shams_90d = 1:3, shams_90d_4h = 2:4, sham3__shams_90d = 3:5,
sham3_90d__shams = 5:7, sham3_90d__shams_4h = 6:9),
fill = c("red", "green", "blue", "yellow", "purple"),
alpha = c(0.5, 0.5,0.5, 0.5, 0.5), cex = 1,cat.fontface = 2,
lty =1, filename=NULL, cat.cex=0.8,
cat.just=list(c(0.6,1) , c(0,0) , c(0,0) , c(1,1) , c(1,0)))
grid.newpage()
grid.draw(v)
Another option (if a bit of a quick hack) would be to remove the cat.just argument and set a smaller grid::viewport. You may need to tweak the width of your graphics window / output device (ie pdf(..., width=...)):
# Plot
v <- venn.diagram(list(shams_90d = 1:3, shams_90d_4h = 2:4, sham3__shams_90d = 3:5,
sham3_90d__shams = 5:7, sham3_90d__shams_4h = 6:9),
fill = c("red", "green", "blue", "yellow", "purple"),
alpha = c(0.5, 0.5,0.5, 0.5, 0.5), cex = 1,cat.fontface = 2,
lty =1, filename=NULL, cat.cex=0.8)
grid.newpage()
pushViewport(viewport(width=unit(0.8, "npc"), height = unit(0.8, "npc")))
grid.draw(v)
Related
I'm using par(mfrow) to generate a multi-panel plot of three separate graph output objects. Sample code below represents a very simplified version of the objects I have.
How can I save these plots as a single object with ggsave? I've tried naming the par(mfrow) as an object and plotting it, but that doesn't seem to work.
Any advice on alternative ways of generating/saving a multi-panel plot is also welcome! Please let me know if I can clarify the question or example. Thanks!
par(mfrow = c(1,3), mar = c(10, 5, 5, 3), xpd = TRUE)
hist(x = rnorm(100), col = "skyblue", main = "X")
hist(x = rnorm(50), col = "green", main = "Y")
legend("bottom", c("Blue", "Green", "Purple"),
title = "Sample Data", horiz = TRUE, inset = c(0, -0.4),
col = c("skyblue", "green", "purple"), pch = rep(15,2),
bty = "n", pt.cex = 1.5, text.col = "black")
hist(x = rnorm(75), col = "purple", main = "Z")
I would suggest using svglite over svg if you want to edit the graph using Inkscape or a similar program since you will not be able to edit the text (change the text, font, or size) in a file produced by svg. Here is an example with a few edits to your original code:
library(svglite)
svglite("MyPlots.svg", width=8, height=6)
par(mfrow = c(1,3), mar = c(10, 5, 5, 3), xpd = TRUE, mgp=c(1.75, .75, 0))
hist(x = rnorm(100), col = "skyblue", main = "X")
hist(x = rnorm(50), col = "green", main = "Y")
legend("bottom", c("Blue", "Green", "Purple"),
title = "Sample Data", horiz = TRUE, inset = c(0, -0.2),
col = c("skyblue", "green", "purple"), pch = rep(15,2),
bty = "n", pt.cex = 1.5, text.col = "black")
hist(x = rnorm(75), col = "purple", main = "Z")
dev.off()
I want a dot plot for mydf below
mydf <- data.frame(city=c(rep(c("Rome","NY","LA"),3)),
old=c(11,23,13,24,12,13.5,15,17,22),
new=c(12,22,13.5,25,14,15,12,17,14),
method=c("a","a","a","b","b","b","c","c","c"))
my_cols <- c("red", "blue", " dark green")
grps <- as.factor(mydf$method)
dotchart(mydf$old, labels = mydf$city,
groups = grps, gcolor = my_cols,
color = my_cols[grps],
cex = 0.6, pch = 19, xlab = "value")
meth1<- mydf[mydf$method=="a",]
meth2<- mydf[mydf$method=="b",]
meth3<- mydf[mydf$method=="c",]
points(meth1$new, 1:nrow(meth1), col = "orange", pch = 16, cex = 0.6)
points(meth2$new, 1:nrow(meth2), col = "light blue", pch = 16, cex = 0.6)
points(meth2$new, 1:nrow(meth2), col = "green", pch = 16, cex = 0.6)
before adding points(), I get plot below, which is what I want as a basis.
but when I add points, all of them appear in the bottom part of the plot. I want the "new" values corresponding to each method appear in its own part of the plot and a segment line connects the old and the new values accordingly.
how can I do that in my code? thanks for any help with this.
The y-axis in the dotchart, under the hood, is really just integers. So you have to provide the correct y-axis values to points():
points(meth1$new, 11:13, col = "orange", pch = 16, cex = 0.6)
points(meth2$new, 6:8, col = "light blue", pch = 16, cex = 0.6)
points(meth2$new, 1:3, col = "green", pch = 16, cex = 0.6)
I reached those values by just eyeballing the plot and counting up. Note that not all these points will appear if they are outside the range of the original dotchart call. You can adjust that by setting xlim = c(11,25) or something appropriate in the dotchart call.
I have the data below:
dc <- c("CACNA1C", "CACNA1D", "KCNN4", "CACNA1F", "CACNA1D", "CACNA1C", "GNRHR", "CD80", "CD86", "ITGA2B" )
tc <- c("CACNA1C", "CACNA1D", "CACNA1C", "CACNA1D", "CACNA1F", "KCNN4", "APP", "GNRHR", "CD86", "CD80" )
and I want to create a Venn diagram using the VennDiagram package.
library(VennDiagram)
Vt <- venn.diagram(
x = list(
DC_Dataset=dc,
TC_Dataset=tc
),
#filename = "1B-double_Venn.tiff",
main = "Targets",
main.cex = 3,
filename = NULL,
lwd = 4,
fill = c("cornflowerblue", "green"),
alpha = 0.75,
label.col = "black",
cex = 2,
fontfamily = "sansserif",
fontface = "bold",
cat.col = c("black", "black"),
cat.cex = 1.5,
cat.fontfamily = "sansserif",
cat.fontface = "bold",
cat.dist = c(0.03, 0.03),
cat.pos = c(-20, 14)
)
grid.newpage()
grid.draw(Vt)
I have 3 issues:
The shapes are ovals rather than circles.Can this be changed or is the default shape?
It would be nice to locate these labels more centrally within the bodies of the 2 shapes. With cat.pos = c(-20, 14) I can move the 2 labels left or right but not upside-down.
I don’t like that the unique part of the TC dataset, with 1 unique value, is the same color as the overlap region.Ideally, in the updated script, if e.g. purple and green are selected as the 2 regions’ colors, then a third color could be used as the overlap color.
Answer to any of these questions would be really helpful.
It all depends on the scaling of your window/output picture. Setting length to width ratio to 1:1 gives a circle.
cat.pos isn't about left and right:
Vector giving the position (in degrees) of each category name along
the circle, with 0 at 12 o'clock
Also, cat.dist is useful for this:
Vector giving the distance (in npc units) of each category name from
the edge of the circle (can be negative)
The colors are not the same. They are really similar, though, due to high value of alpha and blue being "close" to green. It doesn't seem to be possible to specify the third color, but it kind of makes sense that the resulting color is a mix of the other two.
That said, you could do something like this:
Vt <- venn.diagram(
x = list(
DC_Dataset=dc,
TC_Dataset=tc
),
main = "Targets",
main.cex = 3,
filename = NULL,
lwd = 4,
fill = c("red", "blue"), # Modified
alpha = 0.4, # Modified
label.col = "black",
cex = 2,
fontfamily = "sansserif",
fontface = "bold",
cat.col = c("black", "black"),
cat.cex = 1.5,
cat.fontfamily = "sansserif",
cat.fontface = "bold",
cat.dist = c(-0.1, -0.1), # Modified
cat.pos = c(-90, 90) # Modified
)
grid.newpage()
grid.draw(Vt)
I am creating 2 overlapping histograms on a single plot. The diagram needs to be clear in black and white, so I am using a combination of colours and textures to help distinguish between the histograms.
The problem is with the legend, where I can either get the colours to show up, or the textures, but not both. This code produces a legend with empty boxes:
dat <- rnorm(100)
dat2<-rnorm(100,1)
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), fill = c("grey", NA), density = c(0, 20))
I don't have the reputation to post images, but a link to the output is below:
https://drive.google.com/uc?id=0B4VH8cX4Cf7bNjhVQnUwWWVaMEk
I can get the colour alone to display correctly:
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), fill = c("grey", NA))
https://drive.google.com/uc?id=0B4VH8cX4Cf7bcXJvakZxN0x5c1U
And the textures alone also works:
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), density = c(0, 20))
(I've can only post 2 links, also because I don't have enough reputation)
It just won't work together. What am I doing wrong?
Thanks!
Figured it out. The fill argument in legend() also applies to the shading lines, so when I specified that fill for the second legend item was NA, it meant the shading lines were invisible.
The following code produces the result I want:
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), fill = c("grey", "black"), density = c(1000, 20))
So I've created a barplot for some data I have with the following code:
plot <- barplot(data,
beside = TRUE,
col = c("red", "blue", "green"),
space = c(0, 0.4),
width = 0.2,
xlim = c(0, 2),
ylim = c(0, 1.1),
legend = c("KNN", "MF1", "MF2"),
args.legend = list(x = 2.7, y = 1.2),
yaxt = 'n',
xpd = TRUE,
srt = 90
)
text(x = plot,
y = data + 0.05,
labels = as.character(round(data, digits = 2)),
srt = 90,
xpd = TRUE
)
I think the plot looks neat, but... It does not fit the region (I think at least). Are there any ways to keep the width of the bars in picture 1 and still show all 8 groups? My solution so far is to reduce the width of the bars as shown in picture 2.
Picture 1
Picture 2