Force RStudio to plot in Viewer - r

My plots tab doesn't work - so I am trying to display the output of ggplot in the Viewer tab.
This has been accomplished for printing HTML tables: Force rstudio to use browser instead of viewer. Can this be done for plots?
library(ggplot2)
gg.plot <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
Is there a method to print to viewer?

I posted this as a comment, but couldn't get the formatting right, so I'm making it an answer. This code will create a new window to put your plot in.
library(ggplot2)
data(iris)
x11() # creates the new window
ggplot(data = iris, aes(Petal.Length)) + geom_histogram()
dev.off() # closes the window when you're done with it

Unfortunately, I don't think this is what the viewer pane was designed for. This article from Ian Pylvainen suggests that this pane is exclusively for web content. Unless you were attempting to visualise your plots in an HTML instance (produced from Markdown, Shiny, htmlwidgets, etc.), I would not recommend Rstudio's Viewer as a viable solution.
To continue with #Joseph Clark John's recommendation, using the variety of other devices R offers might be of use to you. Their recommendation to use x11() is specific to linux distributions so if using Windows or MacOs, you could produce another window using the windows() or quartz() commands, respectively (See documentation for all relevant devices).
This post touches on this:
Make R Studio plots only show up in new window

Alternatively, you could use plotly::ggplotly() to display a ggplot in the Viewer pane:
library(ggplot2)
library(plotly)
data(iris)
plotObj <- ggplot(data = iris, aes(Petal.Length)) + geom_histogram()
plotly::ggplotly(plotObj)

Related

R ggplot not displaying

I know this question has been asked a number of times, but the solutions for those answers did not work for me. I am using R version 4.0.0 in R studio. I have been able to use ggplot before I updated, so I am not sure if that is related to the issue or not.
I am trying with one of the ggplot examples:
library("ggplot2")
p1<- ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
show(p1)
I have also tried
print(p1)
Neither have shown the plot nor thrown an error. I am putting this directly into the console, so from what I've read, the print/show shouldn't be necessary, but it still does not show. P1 is created in my Global Environment and is a List of 9. Does anyone have any ideas??? Thanks!
Maybe you have redirected the output by opening a pdf() or jpeg() device that you have forgotten to close ?

Writing printer friendly pdfs with ggplot2 and many (>25000) dots or lines

I am creating a lot of facet plots with ggplot2 and writing those into a multiple page pdf file. I added some sample data as an example.
reps <- 1000
df <- data.frame(id=rep(letters[1:25], each=reps),
group=rep(LETTERS[1:25], each=reps),
x=runif(reps*25),
y=runif(reps*25))
pdf('test.pdf', width=11.69, height=8.27)
for(i in seq(4)) {
p <- ggplot(df, aes(x,y, group=group)) +
geom_line() + geom_point() +
facet_wrap(~ id)
print(p)
}
dev.off()
To view the PDF it is really nice, but most of the time these big files with vector graphics kill my print jobs.
Is there a way to handle this properly? For example, creating PNG images and saving them in multipage PDFs? Or, am I the only one having problems printing such files?
I am aware that the PNG file might be bigger, but I guess the printer can handle it much better than a vector graphic.
The only solution I found so far is the following: https://helpx.adobe.com/acrobat/kb/quick-fix-print-pdf-image.html
I think this might be the best way since the creation of PDF is the same, you just switch to "print as image" in the advance printing dialog in Acrobat Reader...

How to export arrangeGrob output via win.metafile()

I need to save plots from R as EMF format (windows metafile format) because this makes the chart look good on screen and paper in Microsoft Word. No other option (PNG, postscript etc) works well on both. The PNG device produces poor res plots. Tinkering with res parameters blows up the graph elements and I can't find anything that clearly explains how to mitigate this. Using postscript print output is pretty good. However, Word's EPS filters are busted so that I can't see the EPS file on screen. I need something that works well on screen and on paper. win.metafile is only thing that does both.
Here's the twist. I am using gridExtra to customise the layout of my plots. From what I gather, this means that I am writing multiple plots onto one device (which I then want to export to EMF). But I know that win.metafile only allows one plot per file. From ?win.metafile:
For win.metafile only one plot is allowed per file
So the following shouldn't work:
library(ggplot2)
library(gridExtra)
# g_legend pinched from Hadley:
# https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend <- function(a.gplot)
{
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
win.metafile(file='test.emf', width=6, height=4)
p <- ggplot(mtcars, aes(x=cyl, y=mpg, colour=factor(gear)))
pl <- p + geom_point()
legend <- g_legend(pl)
lwidth <- sum(legend$width)
pp <- arrangeGrob(pl + theme(legend.position="none"), legend)
pp
dev.off()
In fact I get the following error message:
Error in grid.newpage() : metafile 'test.emf' could not be created
Ok. So here's my question: how can I trick win.metafile to see only one plot from the arrangeGrob output? Can I stuff its output into something and get one plot out? It must be possible because if I use RStudio's export function, I get an excellent looking chart on screen and paper. But I want to codify this so I don't have to manually export the files.
I've scoured the web and haven't been able to find anything that addresses this. Help would be greatly appreciated!
I tried this just now using the devEMF package, and though it throws a warning it looks like the picture that you've created here.
You just need to install.packages('devEMF') and then:
require(devEMF)
emf('imPic.emf')
print(pp)
dev.off()

ggplot2 plot fill page in landscape (pdf)

I'm trying to render a plot to a PDF using the following approach:
pdf('~/Desktop/test.pdf', bg = "white", paper="USr")
p <- ggplot(df, aes(something)) + geom_bar();
print(p)
# I'm actually printing a bunch of graphs to the PDF
dev.off()
The "USr" in the PDF function is setting up the PDF to print in landscape mode. The plot is produced and is centered on the page but there is a large right/left margin and the plot isn't scaling out to take up the full 11" available to it.
I've tried some tweaks to the pdf(...) command and to the ggplot itself. Is there a solution this way or do I need to use a dedicated reporting/pdf package like sweave or knitr?
See this discussion; bottom-line is you probably want to use paper=special and set width and height explicitly.
Edit:
Here's a lazy trick to use ggsave with multiple pages,
library(ggplot2)
plots = replicate(8, qplot(1,1), simplify=FALSE)
library(gridExtra)
p <- do.call(marrangeGrob, c(plots,ncol=1,nrow=1))
ggsave("multipage.pdf", p, width=11, height=8.5)
(but otherwise, pdf(...) followed by a for loop is just fine and sometimes clearer)

R ggplot2 - no background or grid lines in plot with RGui

I'm having a strange problem with the output window in RGui (under Win XP). I should see a plot like the one below...
... when I run this script:
library(ggplot2)
x <- rnorm(100,0,1)
y <- rnorm(100,0,1)
z <- data.frame(x,y)
g <- ggplot(z, aes(x,y)) + geom_point() + theme_gray()
Instead, in the plot window it shows a white background and white grid lines, like below.
R Plot Window
When I export the plot to .png and I "preview" it in windows explorer - it doesn't show a background or grid lines.
Png in Windows
Same Png in Gimp
Same Png uploaded to image hosting
Any ideas about what's going on? How can I get the plot to display correctly in RGui?
Sounds like a problem with the device rather than R, try reinstalling GTK+. If that doesn't work try plotting jpegs instead of png's if you can.

Resources