Set colors & title in an alluvial plot - r

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".

Related

How to put legends at the bottom of the heatmap,still not solved?

[ Reference: <Evolutionary origins of the SARS-CoV-2 sarbecovirus lineage responsible for the COVID-19 pandemic(extended fig.5),Using the code from https://github.com/TD-lab-Wu/SARSCoV2origins>]
[3 differences :
Question1:how to make the legend on the bottom of the picture by using the packege pheatmap?
Question2:how to make the slight margin between the cells?
Q3.How to draw the 4 horizontal lines for the 4 clusters?
]
Here is the code from the paper:
install.packages("tidyverse")
library(pheatmap)
library(viridis)
library(ggrepel)
library(ggpmisc)
library(rlang)
# RSCU Heatmap ------------------------------------------------------------
all_rscu = read.csv("https://raw.githubusercontent.com/plemey/SARSCoV2origins/master/codonUsage/all_rscu_codonBiasReanalysis.csv")
all_df <- as.data.frame(all_rscu[,2:26])
row.names(all_df) <- all_rscu$codon
pheatmap(all_df,
cluster_rows = F,
gaps_row = c(10, 14),
scale = 'none',
fontsize_row = 5,
fontsize_col = 10,
color = magma(50),
border_color = NA,
cutree_cols = 4
)
I check the pheatmap in the CRAN,so many functions,but i did not find one way to fix these problems,and this is the same question when i look for answer in the stackoverflow,https://stackoverflow.com/questions/63951276/how-to-put-key-values-and-legends-at-the-bottom-of-the-heatmap
Best Regards

How to zoom in on/extract a subsection of/extract colours from a heatmap.2 in R?

I'm creating heatmaps in R using heatmap.2 (I think it needs to be heatmap.2 because I'm using 1 dataset to generate the colours of the heatmap and a second dataset to overlay numerical data).
Here is a sample of my code so far. The actual data set is 30 columns and 1000 rows.
heatmap_all_data <-
data.frame(name = c("John", "Mark", "Luke", "Jack", "Will", "Jim", "Clive", "Steve"),
trait_1 = c(1, 2, 5, 8, 5, 3, 7, 8),
trait_2 = c(5, 7, 3, 4, 6, 3, 2, 1)) %>%
column_to_rownames(var="name")
heatmap_colour <- colorRampPalette(brewer.pal(11, "RdYlBu"))(1000)
heatmap.2(as.matrix(heatmap_all_data),
scale = "column",
key = FALSE,
dendrogram = "none",
Rowv = FALSE,
Colv = FALSE,
trace = "none",
col = rev(heatmap_colour),
labRow = row.names(heatmap_all_data))
Which generates the following heatmap: https://i.stack.imgur.com/lK8Sc.png
NOW, the problem is I only want a subsection of this data, e.g I want the following heatmap:
heatmap_part_data <-
data.frame(name = c("John", "Mark", "Luke"),
trait_1 = c(1, 2, 5),
trait_2 = c(5, 7, 3)) %>%
column_to_rownames(var="name")
heatmap_colour <- colorRampPalette(brewer.pal(11, "RdYlBu"))(1000)
heatmap.2(as.matrix(heatmap_part_data),
scale = "column",
key = FALSE,
dendrogram = "none",
Rowv = FALSE,
Colv = FALSE,
trace = "none",
col = rev(heatmap_colour),
labRow = row.names(heatmap_part_data))
https://i.stack.imgur.com/j33Ic.png
BUT, I want each cell to keep the same colours as the original. I.e. I want the colours in my subsetted heatmap to be relative to the total data and not just the subsetted data. (In the real example I want to show 10 out of 1000 entries).
So, I need to either "zoom in" and rescale the top section of the heatmap and then crop the image, extract the top section of the heatmap into a new object while maintaining the same colours, or extract information about the colours in the full heatmap and overwrite the default colours in the subsetted heatmap.
The goal is basically to output an image of the subsetted data heatmap with each colour in each cell the same as in the all_data heatmap.
I hope this is clear - please advise if you need any clarification!
Many thanks for taking the time to read and I hope someone can help.
Best,
Ryan
Found the solution!
So I switched from heatmap.2 to heatmaply - same functionality but with interactivity. With heatmaply you can drag an area over the heatmap and zoom into that area which gives the desired result but I wanted to consistently zoom to a specific area.
From this website (https://plotly.com/r/axes/) I found out about the Layout function of the wider plotly library (that heatmaply is part of).
So to the existing code you can add:
%>% layout(yaxis = list(range = c(10.5, 0.5)))
(Need to add 0.5 to centre the rows properly)
Et voila! The heatmap colours are generated relative to the wider dataset but only a subset is shown.

Placing two venn diagrams on one chart

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)

How to take a sample from a large plot to show in knitr-PDF

I often create large plots in RStudio which I save to PDF but would also like to partly show in the PDF knitr report.
Is there a way to create the full object then cut a piece (ideally top left corner) and include that second picture in the PDF report?
as example, a pheatmap code that produces a plot with 56 cols and 100's of rows. I would like to show only the left-top-most 10col and 10 rows but if I sample the input data, I obviously get another plot due to the clustering being done on different data. Also, I would love a solution applicable to any plot types (not only pheatmap).
drows <- "euclidean"
dcols <- "euclidean"
clustmet <- "complete"
col.pal <- c("lightgrey","blue")
main.title <- paste("Variant (freq>", minfreq, "%) in all samples", sep="")
hm.parameters.maj <- list(hm.maj.data,
color = col.pal,
fontsize = 10,
cellwidth = 14,
cellheight = 14,
scale = "none",
treeheight_row = 200,
kmeans_k = NA,
show_rownames = T,
show_colnames = T,
main = main.title,
clustering_method = clustmet,
cluster_rows = TRUE,
cluster_cols = FALSE,
clustering_distance_rows = drows,
clustering_distance_cols = dcols,
legend=FALSE)
# To draw the heatmap on screen (comment-out if you run the script from terminal)
do.call("pheatmap", hm.parameters.maj)
# To draw to file (you may want to adapt the info(header(vcf))sizes)
outfile <- paste("major-variants_heatmap_(freq>", minfreq, ")_", drows, ".pdf", sep="")
do.call("pheatmap", c(hm.parameters.maj, filename=outfile, width=24, height=35))
Thanks in advance
Stephane

Change color on single-series line chart using rCharts and Highcharts?

Is it possible to change the color of a line using rCharts and Highcharts so that the line color changes depending on a factor? I've done this with ggplot2 but would like to make an interactive version if possible. I've tried
h1 <- Highcharts$new()
h1$chart(type="line")
h1$series(data=mydf$myvalue, name="", groups = c("myfactor"))
h1$xAxis(tickInterval = 4, categories = mydf$myXaxis)
but that's not working, line stays the same color. Sample data
myvalue <- c(16, 18, 5, 14, 10)
myXaxis <- c(1,2,3,4,5)
myfactor <- c("old", "old", "old", "new", "new")
mydf <- data.frame(myvalue, myXaxis, myfactor)
Thanks for any suggestions.
I received a great suggestion on Twitter from #kyle_e_walker to use Dimple with rCharts instead of Highcharts http://bl.ocks.org/walkerke/ae98fc103dd9b9dcce0f

Resources