How to put multiple existing graphs in a same plot? - r

This links explains how to plot multiple graphs in a same overall plot.
Now I have three existing graphs, png1, png2, png3. I want a layout like below. How to achieve this?
Thank you very much for the answer, please remember to install the packages:
install.packages("png")
library(png)
install.packages("gridExtra")
library(gridExtra)
After using the gridExtra, I combined three graphs together. However, they had very low resolution. How can I make them at least the same resolution as the original ones?

You would use the par or layout function. See the examples here: https://www.rdocumentation.org/packages/graphics/versions/3.5.0/topics/layout
If you're interested in inserting image files into the plot, you'd use readPNG and rasterImage and/or the grid raster functions.
Example:
png1 = png::readPNG("png1.png")
png2 = png::readPNG("png2.png")
png3 = png::readPNG("png3.png")
images = list(png1, png2, png3)
grobs = lapply(images, grid::rasterGrob)
gridExtra::grid.arrange(grobs=grobs)

Related

Subplot in Julia using Gadfly

Subplots in Julia using Gadfly
Hi everyone,
I want to create subplot grid in Julia using Gadfly for two different functions/arrays.
For example:
1. Gadfly.plot(x=rand(20),y=rand(20))
Gadfly.plot(x=rand(10),y=rand(10))
The below link shows the example of subplot grid based on DataFrame using Geom.subplot_grid. But I can't find any example for functions/array.
http://gadflyjl.org/v0.6.4/lib/geoms/geom_subplot_grid.html
Can anyone help me out?
Thanks in advance
You can use Stacks to achieve what you want.
p1 = Gadfly.plot(x=rand(20),y=rand(20))
p2 = Gadfly.plot(x=rand(10),y=rand(10))
stack = hstack(p1, p2) # will arrange the plots horizontally
title(stack, "Horizontally Stacked Plots") # let's give a title
You can combine hstack and vstack to create arbitrary arrangements of plots. Please also take a look at gridstack which sometimes makes things easier. Please refer to the Stacks section in manual to learn more about Stacks.

creating multiple file types while plotting

I would like to produce a series of plots in both high-resolution and low-resolution versions, or stated differently using two different file types (.png and .eps). I'd like to know the best/least repetetive way to do this. I am using the gplot function in sna, and the plot has a custom legend outside the plot area. I wrote a function something like this:
library(sna)
plotfun <- function(net){
png("test.png",width=800)
p <- gplot(net)
par(xpd=T)
legend(max(p[,1])+1,max(p[,2]),legend=letters[1:10],title="custom legend")
dev.off()
seteps()
postscript(test.eps)
#repeat all the plotting commands, which are much longer in real life
dev.off()
}
#try it with some random data
plotfun(rgraph(10))
This is perfectly functional but seems inefficient and clumsy. The more general version of this question is: if for any reason I want to create a plot (including extra layers like my custom legend), store it as an object, and then plot it later, is there a way to do this? Incidentally, this question didn't seem sna specific to me at first, but in trying to reproduce the problem using a similar function with plot, I couldn't get the legend to appear correctly, so this solution to the outside-the-plot-area legend doesn't seem general.
I would recommend generate graphs only in Postscript/PDF from R and then generate bitmaps (e.g. PNG) from the Postscript/PDF using e.g. ImageMagick with -density parameter (http://www.imagemagick.org/script/command-line-options.php#density) set appropriately to get desired resolution. For example
convert -density 100 -quality 100 picture.pdf picture.png
assuming picture.pdf is 7in-by-7in (R defaults) will give you a 700x700 png picture.
With this approach you will not have to worry that the picture comes out formatted differently depending which R device (pdf() vs png()) is used.

plot igraph in a big area

Just wondering if it is possible to increase the size of the plot so that the nodes and edges can be more scattered over the plot.
Original plot:
What are expected:
I tried many parameters in the layout function such as area, niter, and so on, but all of them do not work. By the way, I am using 'igraph' package in R.
If you are referring to the actual size of the produced output (pdf, png, etc), you can configure it with the width and height parameters. Check this link for png,bpm, etc, and this link for PDF format.
A MWE is something like this:
png("mygraph.png", heigh=400, width=600)
#functions to plot your graph
dev.off()
If you are referring to the size of the graphic produced by the layout function, as #MrFlick referred, you should check the parameters of the particular layout you are using.
Hope it helps you.
In your second graph, it's obviously the graph can be divided into several clusters (or sections). If I understood you correctly, you want to have a layout that separates your clusters more visibly.
Then you can draw this by calculating a two-level layout:
First, calculate the layout of the graph in order to find a place for each cluster.
Second, calculate the layout in each cluster according to first step and plot nodes in the corresponding place.

Change plot size of pairs plot in R

I have this pairs plot
I want to make this plot bigger, but I don't know how.
I've tried
window.options(width = 800, height = 800)
But nothing changes.
Why?
That thing's huge. I would send it to a pdf.
> pdf(file = "yourPlots.pdf")
> plot(...) # your plot
> dev.off() # important!
Also, there is an answer to the window sizing issue in this post.
If your goal is to explore the pairwise relationships between your variables, you could consider using the shiny interface from the pairsD3 R package, which provides a way to interact with (potentially large) scatter plot matrices by selecting a few variables at a time.
An example with the iris data set:
install.packages("pairsD3")
require("pairsD3")
shinypairs(iris)
More reference here
I had the same problem with the pairs() function. Unfortunately, I couldn't find a direct answer to your question.
However, something that could help you is to plot a selected number of variables only. For this, you can either subset the default plot. Refer to this answer I received on a different question.
Alternatively, you can use the pairs2 function which I came across through this post.
To make the plot bigger, write it to a file. I found that a PDF file works well for this. If you use "?pdf", you will see that it comes with height and width options. For something this big, I suggest 6000 (pixels) for both the height and width. For example:
pdf("pairs.pdf", height=6000, width=6000)
pairs(my_data, cex=0.05)
dev.off()
The "cex=0.05" is to handle a second issue here: The points in the array of scatter plots are way too big. This will make them small enough to show the arrangements in the embedded scatter plots.
The labels not fitting into the diagonal boxes is resolved by the increased plot size. It could also be handled by changing the font size.

How to add a picture as datapoints in a map in R

I am using maps package in R to draw a simple geographic map and then put my data points in it.
My question is that whether there is any way in R to represent data points with a picture of interest, for example, the animal I am working on in my example.
This is just to give a better representation of the distribution of my data points relative to each other for my reader.
You can also use grid package. The grid.raster can be used to put some pictures.
Since maps is graphic base package , you need to gridBase to combine the grid/base graphics.
Here an example:
library(maps)
map('usa',boundary=T,fill=T,col='grey')
library(gridBase)
library(grid)
library(png)
vps <- baseViewports()
pushViewport(vps$figure,vps$plot)
camel <- readPNG("camel.png") ## some animal picture
grid.rect(gp = gpar(fill=NA))
x <- c(-110,-100,-70)
y <- c(30,40,40)
grid.raster(image=camel,x=x,y=y,width=5, ## it is vectorized
interpolate=FALSE,default.units = 'native')
upViewport(1)
PS: I am not sure that there are camels in USA...
rasterImage is one way, albeit somewhat laborious. Once you've got the images of interest formatted as raster objects, you can then place them at designated locations (and frame sizes) inside your plot region.
In addition to the rasterImage function mentioned by #CarlWitthoft there is also the combination of my.symbols and ms.image from the TeachingDemos package for adding images to a plot (base graphics). The rasterImage approach gives the most control, but my.symbols is more like the regular plotting functions in that you say plot the images centered at these coordinates (and set other options to specify size etc.)

Resources