How do I save SVG plots from Databricks in R? - r

I have the following R code which I can run in R Studio to generate a CDH dendogram plot and save it locally as a SVG.
c1 <- hclust(as.dist(subs_matrix), method = "ward.D2") #ward.D2
#scale the branches of the tree, only changes the aesthetics the dendrogram for interpretation
min_height<-min(c1$height) - 0.01 #Ensure tree has some height at its lowest point
max_height<-max(c1$height)
c1$height <- (c1$height-min_height)/(max_height-min_height)
#plot the tree, cex is the label font size and hang = -1 roots the branches
plot(c1,cex = 0.6, hang = -1, width=20, height = 8)
#Outputs the tree image to a file
dev.print(svg, file = "C:/Users/Users/c3523186/Downloads/plot.svg", width = 20, height = 8)
dev.off()
When running in Databricks however, I don't know if/how I'm able to save the svg, either locally or in the Filestore. The plot produced by Databricks is shown as an image, which is too low quality / not scalable.
Can anyone help? Some research suggests it might have something to do with display_HTML(), but I have no idea how.

Related

R: Plot too large to fit into window

I made a phylogenetic tree plot with the igraph package.
The problem is, that my tree is too big to fit into the saved image.
I'm using Rstudio and usually saving my plots manually.
With smaller trees, I was able to enlarge the plot window of Rstudio to make the plot fit into the image, without the nodes overlapping each other.
Unfortunately, this isn't working with my latest plot.
I tried to save it with png(), pdf() and jpeg() with different width and height, but it still doesn't fit properly.
Either the nodes overlap each other or only a part of the plot is visible in the image.
The only solution I found so far was to decrease the label size. But in the end, you can't read anything.
Thank you in advance for your help.
Here is my code:
id <- c("Spirochaetota","Brachyspirae","Brevinematia","Leptospirae","Spirochaetia","sk6","Brachyspirales","Brachyspiraceae","Brachyspira",
"Brevinematales","bo1","Brevinemataceae","Brevinema","bf2","Leptospirales","Leptonemataceae","Leptonema","lg2","Leptospiraceae","Leptospira",
"LeptospiraA","LeptospiraB","lg3","lf1","Turneriellales","Turneriellaceae","Turneriella","tf1","Borreliales","Borreliaceae","Borrelia",
"Borreliella","Sphaerochaetales","Sphaerrochaetaceae","Sphaerochaeta","SphaerochaetaA","sg15","SpirochaetalesA","SpirochaetaceaeA",
"SpirochaetaA","sf1","SpirochaetalesC","Alkalispirochaetaceae","Alkalispirochaeta","Salinispiraceae","Salinispira","SpirochaetaD","sg1",
"sf6","SpirochaetalesD","sdf1","SpirochaetalesE","SpirochaetaceaeB","Oceanispirochaeta","SpirochaetaE","SpirochaetaF","SpirochaetaG","sg2",
"Treponematales","Treponemataceae","Treponema","TreponemaB","TreponemaC","TreponemaD","TreponemaF","tg7","TreponemataceaeB","TreponemaE",
"TreponemaG","TreponemaH","tg5","tf3","so11")
links <- data.frame(from = c("Spirochaetota","Spirochaetota","Spirochaetota","Spirochaetota","Spirochaetota","Brachyspirae",
"Brachyspirales","Brachyspiraceae","Brevinematia","Brevinematia","Brevinematales","Brevinemataceae",
"Brevinematales","Leptospirae","Leptospirales","Leptonemataceae","Leptonemataceae","Leptospirales",
"Leptospiraceae","Leptospiraceae","Leptospiraceae","Leptospiraceae","Leptospirales","Leptospirae",
"Turneriellales","Turneriellaceae","Turneriellales","Spirochaetia","Borreliales","Borreliaceae",
"Borreliaceae","Spirochaetia","Sphaerochaetales","Sphaerrochaetaceae","Sphaerrochaetaceae","Sphaerrochaetaceae",
"Spirochaetia","SpirochaetalesA","SpirochaetaceaeA","SpirochaetalesA","Spirochaetia","SpirochaetalesC",
"Alkalispirochaetaceae","SpirochaetalesC","Salinispiraceae","Salinispiraceae","Salinispiraceae","SpirochaetalesC",
"Spirochaetia","SpirochaetalesD","Spirochaetia","SpirochaetalesE","SpirochaetaceaeB","SpirochaetaceaeB",
"SpirochaetaceaeB","SpirochaetaceaeB","SpirochaetaceaeB","Spirochaetia","Treponematales","Treponemataceae",
"Treponemataceae","Treponemataceae","Treponemataceae","Treponemataceae","Treponemataceae","Treponematales",
"TreponemataceaeB","TreponemataceaeB","TreponemataceaeB","TreponemataceaeB","Treponematales","Spirochaetia"),
to = c("Brachyspirae","Brevinematia","Leptospirae","Spirochaetia","sk6","Brachyspirales",
"Brachyspiraceae","Brachyspira","Brevinematales","bo1","Brevinemataceae","Brevinema",
"bf2","Leptospirales","Leptonemataceae","Leptonema","lg2","Leptospiraceae",
"Leptospira","LeptospiraA","LeptospiraB","lg3","lf1","Turneriellales",
"Turneriellaceae","Turneriella","tf1","Borreliales","Borreliaceae","Borrelia",
"Borreliella","Sphaerochaetales","Sphaerrochaetaceae","Sphaerochaeta","SphaerochaetaA","sg15",
"SpirochaetalesA","SpirochaetaceaeA","SpirochaetaA","sf1","SpirochaetalesC","Alkalispirochaetaceae",
"Alkalispirochaeta","Salinispiraceae","Salinispira","SpirochaetaD","sg1","sf6",
"SpirochaetalesD","sdf1","SpirochaetalesE","SpirochaetaceaeB","Oceanispirochaeta","SpirochaetaE",
"SpirochaetaF","SpirochaetaG","sg2","Treponematales","Treponemataceae","Treponema",
"TreponemaB","TreponemaC","TreponemaD","TreponemaF","tg7","TreponemataceaeB",
"TreponemaE","TreponemaG","TreponemaH","tg5","tf3","so11"))
net <- graph_from_data_frame(d = links, vertices = id, directed = T)
lay = layout.reingold.tilford(net)
plot(net, vertex.shape = "none",
vertex.label.font = 3,
vertex.label.cex = 0.3,
edge.arrow.size = 0.3,
rescale = F,
ylim = c(0.7,3.4),xlim = c(-8,23.2), asp = 0,
layout = lay)
If everything works, it should look similar to this smaller tree. this one does not include the nodes of the code above:
This is my result of the big tree so far. It contains the nodes in the code above:
The only solution I came up with so far is to somehow fit the plot into the plot panel and save it as a SVG file. Afterwards, I edited it using inkscape:
You can plot the tree graph as a dendrogram.
Drawing it left to right allows to put all of the taxon labels in a readable column:
library(igraph)
net <- graph_from_data_frame(d = links, vertices = id, directed = F)
lay <- layout.reingold.tilford(net)
plot_dendrogram(cluster_fast_greedy(net))
There is also:
links %>%
graph_from_data_frame() %>%
as_adjacency_matrix() %>%
dist() %>%
hclust() %>%
as.dendrogram() %>%
plot()

How can I re-scale a forest plot in RStudio?

I am trying to reproduce the plot from the example dataset in the robumeta package in R Studio. While the script works (as expected), I can only see a fraction of the graph. When I increase the size of the window, I get to see more but never the whole thing because apparently, my computer screen is too small and there is no option to scroll up or down. Is there a way to re-scale the forest plot or to print it to pdf (with several pages, if needed)?
Here's the code from package description file:
install.packages("robumeta")
library(robumeta)
data(oswald2013.ex1)
oswald_intercept <- robu(formula = effect.size ~ 1, data = oswald2013.ex1, studynum = Study, var.eff.size = var.eff.size, rho = 0.8, small = TRUE)
forest.robu(oswald_intercept, es.lab = "Crit.Cat", study.lab = "Study",
"Effect Size" = effect.size, # optional column
"Weight" = r.weights) # optional column
png(filename, height, width, pointsize)
#your plot code
dev.off()
in which you play around with the height, width, and pointsize parameters should allow you to save the graph to your desired view size. It won't save as a pdf, but rather a png, which should be ok if your goal is just to view the entire forest plot.

Creating multiple lines inside a nodule of a binary graph in the r package igraph

Package: igraph
Program: R-gui
System: Windows 8
Problem: How does someone put text, within a node, onto multiple lines instead of the default one line?
Background: Using reingold layout with a Binary Tree graph. When I print original tree the text for levels 5+ tends to mesh together into an incoherent mess. Right now I am putting the tree into a png file with, relatively, insane dimensions so it can be read easily. I am looking for a solution so I can move the text onto multiple lines to increase the font size and decrease overall dimensions.
Pertinent Code:
creating tree
library(igraph)
BinaryGraph<-graph.tree(n = 1)
filling tree, where NewName is variable containing all the gene locations at the specific node location
LeftHolder = NewName[j]
BinaryGraph <- BinaryGraph + vertices(LeftHolder)
BinaryGraph <- BinaryGraph + edge("Root" , LeftHolder)
plotting
V(BinaryGraph)$label.cex = 0.65
png(file = "f:/Current Work/PrunedTree.png",width= 60, height=40, units="in", res=300)
plot(BinaryGraph, vertex.size=10, edge.width=2 ,layout = layout.reingold.tilford(BinaryGraph, root = "Root"))
polygon(c(4.5,5.5,5.5,4.5),c(-60,-60,50,50),col="#0000ff22")
dev.off()

Output Stem and Leaf Plot to Image

I'm trying to output a Stem and Leaf plot in R as an image. I'm not sure if there's a nice library which can accomplish this but below is some of the code I've tried.
jpeg(filename="stem.jpeg",width=480,height=480, units="px",pointsize=12)
plot.new()
tmp <- capture.output(stem(men, scale = 1, width = 40))
text( 0,1, paste(tmp, collapse='\n'), adj=c(0,1), family='mono' )
dev.off()
This above code resulted in the data being saved, but it looks very blurry and the plot gets cut off pretty badly. When adding a histogram to an image, R seems to do a good job to scale everything to fit in the size of the image.
jpeg(filename="stem.jpeg",width=480,height=480,
units="px",pointsize=12)
stem(men, scale = 1, width = 40)
dev.off()
This created the image but had no content within it.
Any ideas? Thanks!
That's because stem and leaf plots produce text not images. You can save the text as follows using the sink command: http://stat.ethz.ch/R-manual/R-devel/library/base/html/sink.html
sink(file=“Stem.txt”)
stem(men, scale = 1, width = 40)
sink(file=NULL)
unlink("stem.txt")
To export a stemplot as graphics, you can use a vector graphics format, such
as .eps, .pdf, or .emf. For example, a windows metafile:
win.metafile("stem.wmf", pointsize = 10)
plot.new()
tmp <- capture.output(stem(mtcars$mpg))
text(0,1,paste(tmp,collapse='\n'),family='mono',adj=c(0,1))
dev.off()

Producing a data.frame for presence/absence for each raster cell in R

G'day,
I have a data set I am trying to run a boosted regression tree model on with code:
pa.brt.m <- gbm.step(data=data, gbm.x = 5:15, gbm.y = 2,
family = "bernoulli", tree.complexity = 5,
learning.rate = 0.01, bag.fraction = 0.5)
As it goes about it's business, it returns the error:
Error in plot.new() : figure margins too large
I am wondering what is going on and how I can fix this? This error seems to stop the function and I don't get the rest of the output I would expect. Can anyone provide some insight into what is going on?
This happens when the plotting area
is too small to put the margins
(margins have an absolute dimension:
they will not shrink),
especially when you want many small
plots.
If you had issued a command such as par(mfrow=c(100,100)),
you need to undo it, e.g., by closing the plot window.
This is the most probable cause of the problem.
You can also try to enlarge ("maximize") the plot window.
You can also try to change the margins, e.g. with
par(mar=c(0,0,0,0),oma=c(0,0,0,0)).
You can also try to divert the plot to a file,
with sufficiently large dimensions.
pdf("a.pdf", width=100, height=100)
...
dev.off()

Resources