Using VennDiagram package I'm generating two graphs in the following manner:
# First graph
VennDiagram::draw.pairwise.venn(
area1 = 100,
area2 = 70,
cross.area = 30,
category = c("A1", "B1"),
fill = c("#00204DFF", "#FFEA46FF")
) -> vg1
# Second graph
VennDiagram::draw.pairwise.venn(
area1 = 120,
area2 = 80,
cross.area = 10,
category = c("A2", "B2"),
fill = c("#000004FF", "#FCFFA4FF")
) -> vg2
When called via grid::grid.draw(vg1) and grid::grid.draw(vg2) the charts show as expected:
grid::grid.draw(vg1)
grid::grid.draw(vg2)
Question
How can I create one grid object where both plots are placed one under another?
Attempt
grdFrme <- grid::grid.frame(name = "gf")
grid::grid.pack("gf", vg1)
Error in packGrob(grid.get(gPath), grob, side, row, row.before,
row.after, : invalid 'grob'
Desired results
One solution could be to use awesome multipanelfigure package (fill the panels with base, 'lattice', 'ggplot2' and 'ComplexHeatmap' plots, grobs, and PNG, JPEG, SVG and TIFF images).
library(multipanelfigure)
figure <- multi_panel_figure(columns = 1, rows = 2)
figure %<>%
fill_panel(vg1) %<>%
fill_panel(vg2)
Related
I am trying to make a panel plots of 3 sankey diagram in R using networkD3 library. The Sankey diagrams are well generated and I have assigned three variables (p1, p2 and p3) to the plots. Now I want to combine them using ggarrange into one single plot using the code below;
ggarrange(p1, p2, p3, labels = c("A", "B", "C"), ncol = 1, nrow = 3)
But, I keep getting the following error.
Warning message:
In as_grob.default(plot) :
Cannot convert object of class sankeyNetworkhtmlwidget into a grob.
Apparently, ggarrange does not accept the format of my plots. How can I convert them into a format that ggarange will accept.
Here is the code that I used to generate the final Sankey Diagram for the first one (p1). The rest two (p2, p3) are generated using the same code.
nodes = data.frame("name" = c("Rocket 1", "Kerosene","Carbon IV Oxide",
"Aluminium IV Oxide","Sulphur IV Oxide"))
links = as.data.frame(matrix(c(0, 1, 488.4, 1, 2, 576.7, 1, 3, 24.4,
1, 4, 0.3), byrow = TRUE, ncol = 3))
p1 = names(links) = c("source", "target", "value")
p1 = sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize= 12, nodeWidth = 20)
I would really appreciate some help with this. Thank you.
I am building a large map where a number of observed variables map onto latent variables. When using DiagrammeR to generate the map, all the observed items (indicated by squares) appear in a single row. I was wondering if there was a way to sort them into multiple rows, say 5 or 10 in a row? The example below is based on actual data where there are 30+ items that map to one latent variable. The final map will have multiple sets of these, but right now, I am working on getting one to display correctly:
library(tidyverse)
library(DiagrammeR)
nodes <- create_node_df(
n=33,
nodes = 33,
label = make.unique(rep(letters, length.out = 33), sep=''),
tooltip = make.unique(rep(letters, length.out = 33), sep=''),
fontsize= 7,
shape = c("ellipse", "ellipse", rep("square", 31)),
fillcolor = "white",
width = 1,
height = 1,
)
# lines
edges <- create_edge_df(
from=c(1,rep(2,32)),
to = c(2,3,seq(3, 33, 1)),
)
create_graph(nodes_df = nodes,
edges_df = edges,
) %>%
render_graph(layout="tree")
This renders as:
However, what I am going for is more like:
I am trying to save a plot as png in R, but when I use Arabic letters in plot the saved plot doesn't have any good structure?
ds <- data.frame(labels = c("سامان", "احمد", "یوسف","A1", "B2", "C3"),values = c(10, 40, 60,50,40,10))
p=plot_ly(ds, labels = ds$labels, values = ds$values, type = "pie")
plotly::orca(p, file="p.png")
I have a simple data.frame, made into a simple alluvial map using the alluvial package. How can I edit the plot? My questions, in order of importance, are:
Change the color scheme so that flows coming from the same
"Admitted To" unit are the same color.
Add a title
Save this plot so I can later plot it into a grid with a few ggplots
Caveat: ggalluvial might be easier but unfortunately I can't install it at work, so the solution needs to use base r, ggplot, or the alluvial package.
library(alluvial)
df <- structure(list(Admitted.To =
c("UnitC", "UnitC", "UnitC", "UnitC", "UnitD", "UnitD",
"UnitD", "UnitD", "UnitE", "UnitE", "UnitE", "UnitF",
"UnitB", "UnitB", "UnitB", "UnitB", "UnitB", "UnitG",
"UnitH", "UnitA", "UnitA", "UnitA", "UnitA", "UnitA"),
Discharged.From = c("UnitC", "UnitD", "UnitE", "UnitA",
"UnitC", "UnitD", "UnitE", "UnitA",
"UnitD", "UnitE", "UnitA", "UnitF",
"UnitD", "UnitI", "UnitE", "UnitB",
"UnitA", "UnitG", "UnitH", "UnitC",
"UnitD", "UnitI", "UnitE", "UnitA"),
n = c(136, 2, 1, 2, 1, 162, 2, 3, 1, 213, 1, 3, 5, 1, 7,
22, 23, 1, 32, 10, 9, 39, 9, 607)),
.Names = c("Admitted.To", "Discharged.From", "n"),
row.names = c(NA, -24L),
class = c("tbl_df", "tbl", "data.frame"))
I've been using the color code below until I figure out how to map the colors to the "Admitted To" group
set.seed(8) # for nice colors
cols <- hsv(h = sample(1:8/10), s = sample(3:8)/8, v = sample(3:8)/8)
And my alluvial plot code:
alluvial(df[,1:2],
freq = 8,
blocks = T,
col = cols)
I've tried adding title = "SampleTitleHere" into my code but it just plots another column. I haven't found much documentation on this package.
Thanks for using the alluvial package. Addressing your questions one by one:
1 Change the color scheme so that flows coming from the same "Admitted.To" unit are the same color.
E.g. like that
pal <- RColorBrewer::brewer.pal(8, "Set1") # colors to use
alluvial(
f[,1:2],
freq = 8,
blocks = T,
col = k[ match(f$Admitted.To, unique(f$Admitted.To)) ]
)
Add a title
Perhaps we will add a title or main argument. Meanwhile use mtext() to add a "margin text" on top
pal <- RColorBrewer::brewer.pal(8, "Set1")
alluvial(
f[,1:2],
freq = 8,
blocks = T,
col = k[ match(f$Admitted.To, unique(f$Admitted.To)) ]
)
mtext("A title", 3, line=3, font=2)
If your title spans more than one line or you'd rather have larger margin or space between the title and the plot you can (a) make the margin larger by using a larger number of the third element of a vector you pass to mar argument; (b) mess with line argument to mtext() to adjust how far away from the plot the title should appear.
Save this plot so I can later plot it into a grid with a few ggplots
I dont have a quick answer for that. If you need to mix it with other ggplot-based figures using ggalluvial would be a better choice. See below how you might get it to work.
Caveat: ggalluvial might be easier but unfortunately I can't install it at work, so the solution needs to use base r, ggplot, or the alluvial package.
You should be able to install and use any R package (like ggalluvial) even if you are not an administrator of your system. You only need to install them somewhere you have a permission to write files. This can be even the folder you keep your analysis. See e.g. https://csg.sph.umich.edu/docs/R/localpackages.html or http://www.stat.osu.edu/computer-support/mathstatistics-packages/installing-r-libraries-locally-your-home-directory, or Google for "R user library tree".
I use the googleVis package to plot a map of France which I embedded in a shiny app. But the thinner division is by regions and i have to represents departments also. I actually use .shp files to plot and render each regions divided by departments, but I want to make my outputs more interactive and aesthetically pleasing.
Is there a way to force the magnifying glass when i hover over a region and to personalise the content of it (display a region map inside for example) ? Or to construct an equivalent.
Probaly not with R directly...
library(googleVis)
df <- data.frame(departement = c("Alpes-Maritimes", "Bouches-du-Rhone", "Rhone", "Savoie", "Haute-Savoie"), region = c(rep("Provence-Alpes-Cote d'Azur", 2), rep("Rhone-Alpes", 3)),
ISO_3166.2 = c(rep("FR-U", 2), rep("FR-V", 3)), x = rnorm(5))
dfAggreg <- aggregate(df$x, by = list(region = df$region, iso = df$ISO_3166.2), sum)
plot(gvisGeoChart(dfAggreg,
locationvar = "iso", colorvar = "x", hovervar = "region",
options = list(region = "FR", displayMode = "regions",
resolution = "provinces",
width = 500, height = 400,
colorAxis = "{colors:['#FFFFFF', '#0000FF']}"
)))