Plots not showing up in R html document - r

I've written some simple code to plot some data, but for some reason I can't get it to print out on the html document when I knit it?
library(datasets)
library(ggplot2)
ggplot(ChickWeight, aes(x = Time, y = weight)) + geom_point()
I expect to see an image in the output, but instead, I just see the html code for one...
'''r
library(datasets)
library(ggplot2)
ggplot(ChickWeight, aes(x = Time, y = weight)) + geom_point()
'''
<img src="hw5_files/figure-html/unnamed-chunk-3-1.png" width="672" />
Is there any reason why this would be happening? I'm using Ubuntu 18.04 and R 3.4.4
Edit:
My entire markdown file for this looks like this:
---
title: DS Homework
author: Aaron
date: 4/10/2020
output: html_document
---
1. Question 1:
a. Some part a
b. Some part b
'''{r}
library(datasets)
library(ggplot2)
ggplot(ChickWeight, aes(x = Time, y = weight)) + geom_point()
'''
2. Question 2:

ggplot works well in "interactive" mode however when you are sourcing your code you need to explicitly call print(). Thus either
library(datasets)
library(ggplot2)
print(ggplot(ChickWeight, aes(x = Time, y = weight)) + geom_point())
or
library(datasets)
library(ggplot2)
p <- ggplot(ChickWeight, aes(x = Time, y = weight)) + geom_point()
print(p)
Should work
Hope it helps

I've since figured out that in fact, it was actually, because I was indenting the code blocks, unindenting the code blocks fixed the issue
1. Question 1:
a. Some part a
b. Some part b
'''{r}
library(datasets)
library(ggplot2)
ggplot(ChickWeight, aes(x = Time, y = weight)) + geom_point()
'''
2. Question 2:
to
1. Question 1:
a. Some part a
b. Some part b
'''{r}
library(datasets)
library(ggplot2)
ggplot(ChickWeight, aes(x = Time, y = weight)) + geom_point()
'''
2. Question 2:

put print() before the ggplot or any other plotting line
e.g
from
surface3d(wt1, hp1,pred, alpha=.2)
to
print(surface3d(wt1, hp1,pred, alpha=.2))
Alternatively you can these in the code chunk if you are using rgl library
options(rgl.useNULL = TRUE)
setupKnitr(autoprint=TRUE)
E.g.
\\```{r}
library(rgl)
options(rgl.useNULL = TRUE)
setupKnitr(autoprint=TRUE)
\\```

Related

(R) Graphs in a loop and funktion are empty, even though they work when plottet standalone

So, I have a boxplot where i annotate the number of datapoint per plot and significance levels in letters above the plots. When plottet in a normal (?!?) workflow, they take about 1-2 seconds to plot in a X Window System Graphics (X11), the plot gets saved afterwards. When the plot-command is wrapped in a for-loop or called by a function, the X11-window stays empty and gets saved like that.
Here is a minimal example using mtcars, showcasing the same problem. Without context this example does not make sense.
library(ggplot2)
setwd("C:/")
output <- "C:/"
data <- mtcars
data$cyl <- as.factor(data$cyl)
#----normal plotting----
x11()
ggplot(data, aes(x = cyl, y = mpg))+
stat_boxplot(geom = "errorbar")+
geom_boxplot()
savePlot(paste0(output, "example_normal", ".tiff"), type = "tiff")
dev.off()
#----plotting throught a function----
my.plot <- function(x)
{
x11()
ggplot(x, aes(x = cyl, y = mpg))+
stat_boxplot(geom = "errorbar")+
geom_boxplot()
savePlot(paste0(output, "example_function", ".tiff"), type = "tiff")
dev.off()
}
my.plot(data)
Cheers
I had to post a print(ggplot(...)) around it to make it work in a for-loop.

Why is ggsave not saving my plot to my computer?

Hi I'm trying to save high quality (300 dpi) images using RStudio but no luck so far. I've looked around a lot on the internet but no answer seems to work. Even when I run the code below, no file shows up on my computer. Any help is appreciated!
install.packages("gapminder")
library(gapminder)
data("gapminder")
attach(gapminder)
plot(lifeExp ~ log(gdpPercap))
ggsave("filename.png",dpi = 300)
It works fine if you use ggplot() from ggplot2 instead of plot()
Packages and data
library(ggplot2)
library(gapminder)
data("gapminder")
attach(gapminder)
Solution
ggplot(gapminder,
aes(x = log(gdpPercap), y = lifeExp)) +
geom_point()
ggsave("filename.png",dpi = 300)
Here are some tweaks you came make to make it more similar to plot() appearance:
ggplot(gapminder,
aes(x = log(gdpPercap), y = lifeExp)) +
geom_point(shape = 1) +
theme_linedraw()
output from last code

ggplot2/knitr: geom_col shows breaks in PDF but not in RStudio/HTML

When including a plot with geom_col in an R Markdown report knitted to pdf, the stacked breaks between observations are made visible as gray lines:
```{r}
library(ggplot2)
ggplot(data = midwest) +
geom_col(mapping = aes(x = state, y = poptotal))
```
But when I run the exact same code directly in R Studio (or knit to HTML), the columns are shown as solid:
Is there something special to do to make the different observations not be shown in a pdf (e.g., to make the pdf-knitted plot look like the HTML-knitted one)?
Did you Try using geom_bar() instead of geom_col(), because geom_col() was created afterwards, its basically geom_bar() only
```{r}
library(ggplot2)
ggplot(data = midwest) +
geom_bar(stat="identity",mapping = aes(x = state, y = poptotal))
```
It might work, try it and let me know
You can also set fill and check what happens
geom_bar(stat="identity",mapping = aes(x = state, y = poptotal,fill="gray60"))

Export plotly to powerpoint in R

Is it possible to export plotly/ggplotly plot to Powerpoint and keep its interactivity? I mean by that for example to allow user to change colors of plot lines, thickness, etc.
Example code:
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- ggplot(data = d, aes(x = carat, y = price)) +
geom_point(aes(text = paste("Clarity:", clarity))) +
geom_smooth(aes(colour = cut, fill = cut)) + facet_wrap(~ cut)
gg <- ggplotly(p)
Btw, using webshot package to export plot as .png file is not a solution. And I should be able to code this solution and automate from R or any other programming language - online tools or any other applications where it should be clicked by user to get result do not solve the problem.

R - Changing ggplot plot size in jupyter

Using R in a jupyter notebook, first I set the plot size universally. Second, I would like to plot one single plot with a different size.
## load ggplot2 library
library("ggplot2")
## set universal plot size:
options(repr.plot.width=6, repr.plot.height=4)
## plot figure. This figure will be 6 X 4
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) + geom_point()
## plot another figure. This figure I would like to be 10X8
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) + geom_point() + HOW DO i CHANGE THE SIZE?
As you can see, I would like to change the second plot (and only the second plot) to be a 10X8. How do I do this?
Sorry for a potentially dumb question, as plot sizing is typically not an issue in Rstudio.
Here you go:
library(repr)
options(repr.plot.width=10, repr.plot.height=8)
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) +
geom_point()
I've found another solution which allows to set plot size even when you make plots inside function or a loop:
pl <- ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) + geom_point()
print(pl, vp=grid::viewport(width=unit(10, 'inch'), height=unit(8, 'inch')))
If options is the only mechanism available to change figure size, then you'd do something like this to set & restore the options to whatever they were:
saved <- options(repr.plot.width=10, repr.plot.height=8)
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) + geom_point()
options(saved)
A solution that doesn't involve using external packages is this.
fig <- function(width, heigth){
options(repr.plot.width = width, repr.plot.height = heigth)
}
Just put fig(10, 4), for example, in the cell that generates the plot, and the plot will be scaled accordingly
Source: https://www.kaggle.com/getting-started/105201

Resources