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.
Related
I am trying to make a map using R but I have some problems. The datas I have are shapefiles. I used ggplot to visualise them with this code :
ggplot(farmscot)+geom_sf()
ggplot(scotland)+geom_sf()
ggplot(river)+geom_sf()
and it gives me different graphs like this:
but I need them to be superimpose to make the map. How can I do please?
When I try to add them together R says I can't add ggplots together
Would really appreciate some help and advice, Thank you in advance
I am implementing a R shiny with a plot panel implemented by library(ggplot2). If there are 12 plots, the layout looks great. Please check below.
12-plot layout
However, if I increase the plot number to 70, then each plot looks being compressed (pls see below). Is that possible I can keep the size of each plot fixed? Thank you so much!
enter image description here
Is there another way to approach this? For instance, can you group your data by two categorical variables and use on for colouring and the other for facetting? In that way, you may be able to reduce the number of facets, and stick with the larger facet size, while still conveying all relevant information? 70 facet plots is a lot!
Is this more of a QC thing? For QC, I tend to break it into groups by condition as Paul was suggesting. The reason is that within a condition, things should be really similar. Outside a condition, all bets are off. When I do this for genomics data, I tend to use “pairs” customized to my liking.
What don’t you like about the 70 sample display? Simply the change in aspect ratio? IMO, these are the things I don’t like about ggplot. You can make these plots using base R and then place them on a page manually using par or layout. For that matter, you can do the same with ggplot and use ggarrange or a different manual layout function to place the plots. All wrapped in a for or apply of course.
The other things I like to do when I have a LOT of QCs to look through is create a movie. I can use the forward/back buttons and go through a lot quickly. I like the idea of having this in a dashboard, nice one!
you could also try coord_fixed(ratio= ), not sure if that will work with faceting or not
Finally, I have made a movie-like visualization for those 70 plots using the plot_ly function in R package "plotly".
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)
I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)
I want to create a new figure with interactive R-plots (with tooltips).
To show how I wish to have my plots I give an example using the mtcars data. I am using the package scatterD3 to get tooltips:
library(scatterD3)
data(mtcars)
tooltips <- paste('This is an incredible <strong>',rownames(mtcars),'</strong><br />with',mtcars$cyl,'cylinders !')
scatterD3(x=mtcars$wt,y=mtcars$mpg,tooltip_text=tooltips)
Now I don't want only one plot of this type in one figure. Usually it is possible to use par(mfrow=(i,j)) to create a figure with more than one plot. But it seems not to work for my interactive plots. Is there a way to do this?
Have you given a look to the plotly R package?
It has also a function to render the ggplots in ggplotly
An idea could be to create a multiplot with ggplot2 and than render it with ggplotly.
I've not yet tried it, so I don't know if it's really possible.