Running Plotly charts in Slidify or R markdown - r

I am trying to build R Markdown presentation document using plotly charts , but its not showing us charts in the browser . Code below :
========================================================
```{r,echo=false}
library(plotly)
p <- ggplot(data = diamonds, aes(x = cut, fill = clarity)) +
geom_bar(position = "dodge")
ggplotly(p)
```

Related

Display ggplotly chart without toolbar

I am trying to hide the plotly toolbar when displaying a reactive ggplot chart within a Shiny app. Currently my code looks like this:
renderPlotly( ggplot(users_activated(), aes(x = month, y = n)) +
geom_bar(stat = "identity"))
I have tried config(displayModeBar = F) to no avail.
You need to convert your ggplot object to a plotly object before you can render it as one, or use plotly functions on it. ggplotly does that. Below, is an example using mpg dataset.
ggplotly(ggplot(mpg, aes(class, displ, color = manufacturer)) +
geom_point()
) %>%
config(displayModeBar = F)

side by side display of chart and table in Rmarkdown

Is there a way to display a table and a chart in Rmarkdown? I am trying export the such report to Word and excel not to pdf.
Word is tough. I would recommend pdf or html, but if you have to use word then you can combine the plot and table into a single image using gridExtra
library(gridExtra)
library(ggplot2)
data(iris)
myTable = tableGrob(iris[1:10,])
myPlot = ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
grid.arrange(myTable, myPlot, nrow = 1)

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

R: Align Text Ouput Next to Plot Output in html knitr File

I'd like to align some text output next to a plot output from plotly in an html knitr file. For example, I'd like the output of the functions below to appear side by side in the document.
---
title: 'untitled'
output: html_document
---
```{r}
pacman::p_load(ggplot2, plotly, grid, gridExtra)
p1 <- ggplot(df, aes(y = var1, x =
var2, color = p_c)) + geom_point(aes(text = subjID)) +
geom_smooth(method = "lm")
ggplotly(p1, tooltip = "text")
cor.test(var1, var2)
```
I've fooled around with Grobs (i.e. grobText) and the grid/gridExtra packages - but those don't seem compatible with plotly. For example:
```{r}
pacman::p_load(ggplot2, plotly, grid, gridExtra)
p1 <- ggplot(df, aes(y = var1, x =
var2, color = p_c)) + geom_point(aes(text = subjID)) +
geom_smooth(method = "lm")
p1 <- ggplotly(p1, tooltip = "text")
text1 <- cor.test(var1, var2)
text1 <- textGrob(text1)
grid.arrange(p1, text1, ncol = 2)
```
This returns the error: Error in gList(list(x = list(data = list(list(x = c(-28, 1689, 1122, 284, : only 'grobs' allowed in "gList"
I've also considered flexdashboard, but I don't want the output to appear in a separate html page from the rest of the report.
Any thoughts are appreciated, especially if there's a way to get the row formatting of flexdashboard to be inserted into the middle of a standard html rmarkdown file.

Unwanted stripes in ggplot2 bar chart when knitted to pdf

When knitting a simple bar chart to pdf, I get some unwanted stripes in my bars (see right side of the attached screenshot).
---
title: "Don't Panic"
author: "Ford Perfect"
output: pdf_document
---
```{r, include = F}
library(ggplot2)
```
# Introduction
```{r, echo = F, fig.cap = "My plot"}
ggplot(mpg) + geom_bar(aes(x = as.factor(cyl), y = hwy), stat="identity")
```
These stripes do not appear when I create the plot directly in R-Studio (see left side of the attached screenshot).
I found a way to remove these stripes by aggregating the data before:
ggplot(aggregate(hwy~cyl,mpg,"sum")) +
geom_bar(aes(x = as.factor(cyl), y = hwy), stat="identity")
So I understand that these stripes should be coming from stacking all the other groups in the datasets. This theory seems plausible, since I get two stripes when I aggregate the dataset also by years (2 uniques in mpg dataset).
ggplot(aggregate(hwy~cyl*year,mpg,"sum")) +
geom_bar(aes(x = as.factor(cyl), y = hwy), stat="identity")
But I thought that ggplot2 is automatically doing the aggregation for me when I set stat to identity? Actually it does work directly in R-Studio. So maybe the problem has more to do with knitr?
I do believe that I did not had this same issue in the past. So maybe something changed with an update? Actually all my colleagues (6 other mac and windows computers) have the exact same problem.
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
ggplot2: 2.2.1
knitr: 1.15.1
As noted in a comment, the file type for the graphic can influence the way the graphic renders in the output pdf.
Using the provided chunk:
```{r, echo = F, fig.cap = "My plot"}
ggplot(mpg) + geom_bar(aes(x = as.factor(cyl), y = hwy), stat="identity")
```
The default dev is pdf and the resulting graphic in the pdf is as shown in the question post:
If you are explicit on the dev to use, as in the chunk below, then a png will be generated and the image in the resulting pdf is as wanted.
```{r, echo = F, fig.cap = "My plot", dev = "png"}
ggplot(mpg) + geom_bar(aes(x = as.factor(cyl), y = hwy), stat="identity")
```

Resources