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

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.

Related

How do I save SVG plots from Databricks in 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.

Interactive plot: Manipulate contents of a ggplot2 plot with a sliding bar

Edit: Thank you to Javier for his suggestion. I forgot to mention that I would like to incorporate this interactive plot into a report / dashboard, so something that works with a HTML document from RMarkdown would be ideal, but a dashboard solution would also be fine.
Consider the following plots; the red line represents the actual data, while the green line plots predictions generated by a model:
The predictions of two different models are displayed; one trained over the first 100 hours, and the other over the first 216 hours. Predictions are then generated for the unseen data-points, then plotted.
What I would like to do, is train n models, eg. one every 12 hours in an expanding window fashion. After having done this, I would like to present the results in an interactive fashion where the user can click/slide something to move the vertical line back and forth, thereby changing which model's predictions are displayed. The point would be to intuitively show the effect of different training lengths.
I'm new to shiny and interactive plots in R; can this be done without too much trouble?
You can with the manipulate package for quick interactive plots. Shiny requires more fine-tuning and it is more time-consuming.
Here is a reproducible example for you to test out:
This creates the slider bar:
library(manipulate)
manipulate(plot(1:x), x = slider(1, 100))
Put your code here for the creation of the interactive plot:
manipulate(
plot(cars, xlim = c(0, x.max), type = type, ann = label),
x.max = slider(10, 25, step=5, initial = 25),
type = picker("Points" = "p", "Line" = "l", "Step" = "s"),
label = checkbox(TRUE, "Draw Labels"))
Check out the CRAN manipulate package for more information:
https://cran.r-project.org/web/packages/manipulate/index.html
I was able to do this with the example at the bottom of this link.
library(shiny)
sliderInput("n", "Training length:", 100, min=24, max= 11*24)
renderPlot({
plotPredictCurve(data= df, trainLength= input$n)
})

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()

heatmap in R how to resize columns labels?

I have a data.matrix that is approximately 4000 rows and 100 columns. I am doing a heatmap of the data like:
data<-heatmap(data_matrix,Rowv=NA,Colv=NA,col=cm.colors(256),scale="column",margins=c(5,10))
But the problem that I got is that the labels that appear in the column are too grouped, so it is impossible to visualize them correctly. How I can resize the heatmap so I can see the values of the labels of the column? I tried to print it in pdf, but it only appears a black stripe.
Thanks
I am including a figure of the heatmap, the portion that I want to see are the labels that are in the right part, but they are too close together.
First of all it's better to put your output directly to a PDF file - you may use other image formats but PDF is the best because it is a vector output and you can zoom as much as you want:
pdf("Your-file.pdf", paper="a4", width=8, height=8)
Then it's better to use pheatmap( = pretty heatmap) package. It makes really better heatmaps with a color key besides your heatmap. Finally although the pheatmap() function tries to reduce the label size while you have many rows, but it fails for really large number of rows. So I use the code below for really high - but not too high - number of rows:
library(pheatmap)
library(gplots)
if (nrow(table) > 100) stop("Too many rows for heatmap, who can read?!")
fontsize_row = 10 - nrow(table) / 15
pheatmap(table, col=greenred(256), main="My Heatmap", cluster_cols=F,
fontsize_row=fontsize_row, border_color=NA)
You may change fontsize_col for the column labels. You have many interesting options like display_numbers to have the values inside the cells of your heatmap. Just read ?pheatmap.
This is an example generated by the default parameters of pheatmap() command:
Finally note that too many rows are easy to read on a display, but useless for print.
In Rstudio you can easily resize the graphic window, same holds for Rgui. Alternatively, if you save the plot to file you can use a bigger size for your graphics, e.g. bigger width and height when calling pdf or png.
You can use cexRow = and cexCol =.
You can get more information into ??heatmap.2
# Row/Column Labeling
margins = c(5, 5),
ColSideColors,
RowSideColors,
cexRow = 0.2 + 1/log10(nr),
cexCol = 0.2 + 1/log10(nc),
labRow = NULL,
labCol = NULL,
srtRow = NULL,
srtCol = NULL,
adjRow = c(0,NA),
adjCol = c(NA,0),
offsetRow = 0.5,
offsetCol = 0.5,
colRow = NULL,
colCol = NULL
If you use pheatmap (https://www.rdocumentation.org/packages/COMPASS/versions/1.10.2/topics/pheatmap) you can spread out those labels by adjusting the cellheight parameter.
If you are doing this in R notebook, even though the entire heat map will not display in your output window when you run the code, when you save the heat map to your computer using the filename parameter, pheatmap will automatically calculate the optimal size for the output file so that your entire heatmap will be displayed in your output file. If this size is not to your liking you can adjust using width and height parameters, but it is unlikely you will want to do this.

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