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

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.

Related

How to decompress a large Grid?

I created a super large grid and now I cannot view the image.
Is there a way for me to save it as png or similar and use a magnifying glass to see each facet?
I have tried viewing it in the R itself, saving with ggsave, saving with cairo and exporting.
Here, I show you my script and the image
#install.packages("ggh4x")
library(ggh4x)
qPCR_data$Treatment <- factor(qPCR_data$Treatment, levels =c("NW","S","NS","Ec6","Ec7"))
qPCR_data$Wing <- factor(qPCR_data$Wing, levels =c("Wounded","Control"))
qPCR_grid <- ggplot(qPCR_data, aes(x=factor(SampleID), y=CtGene, colour=Replicate,group=Gene)) +
geom_point(aes(fill=Gene),position=position_dodge(0.3))+
facet_nested(TimePoint~Wing+Treatment)
ggsave("qPCR_grid2.png", plot=qPCR_grid)
qPCR_grid

`ggsave` jpg reversed black-white values with `theme_minimal()`

When I save a ggplot image with theme_minimal the black and white values are reversed in a photo negative like effect. This does not occur if I do not use a theme nor does it occur with theme_bw. It also does not occur when saving to .pdf or .png. I have tested and this occurs when running in RStudio, R GUI, or through the terminal. I'm running R version 4.0.2 on Mac OS 10.15.7.
I would greatly appreciate any insight into debugging this. The behavior has persisted for several weeks across multiple full system restarts.
library(ggplot2)
ggplot(diamonds, aes(x = cut, y = clarity)) +
geom_point() +
theme_minimal()
ggsave("test_minimal.jpg")
Seems like 'theme_minimal' defaults to black background for jpg files (pdf and png were fine and I used Windows 10). #stefan had proposed two ways to overcome this in the comments above. I did not see that and went searching again. So posting the full solution here:
library(ggplot2)
ggplot(diamonds, aes(x = cut, y = clarity)) +
geom_point() +
theme_minimal()
ggsave("test_minimal.jpg",bg="white")

Saving ggplots with greek alphabets in pdf

Here is a minimal example of the problem I face:
data.frame(Time=as.factor(c(0,5,10,15,20,25,30,35)),
Value=c(0,2,4,6,8,6,5,6))%>%
ggplot(aes(x=Time,y=Value))+
geom_point()+
ylab("\U0394 O.D")
ggsave("image.pdf", dpi = 1200)
I am able to generate a plot which renders the delta perfectly. However, the last command to save the plot as a PDF gives warnings. When you open the saved file, the y-axis is labelled as ..OD.
Try to use Cairo:
ggsave("image.pdf", dpi = 1200, device = cairo_pdf)
I'm probably too late for this discussion but I had better experience with latex2exp library. I had some issues saving multiple plots with ggsave and cowplot::save_plot I had to look for other solutions. With this method you can use any saving method, your file plot look fine.
Let's say you want to put pi as a label on your y axis.
p <- p + ylab(TeX("$\\pi$"))

Force RStudio to plot in Viewer

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)

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)

Resources