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)
Related
ggiraph package is suitable for generating interactive graphics.
I was trying to execute some examples found online concerning package ggiraph and I was not able to find anywhere how to export the interactive graph generated, maybe in format svg (or other).
Below the code:
library(ggplot2)
library(ggiraph)
library(patchwork)
library(cowplot)
mtcars <- mtcars
mtcars$tooltip <- rownames(mtcars)
gg1 <- ggplot(mtcars) +
geom_point_interactive(aes(x = drat, y = wt, color = qsec,
tooltip = tooltip, data_id = tooltip ), size = 4)
gg2 <- ggplot(mtcars) +
geom_point_interactive(aes(x = qsec, y = disp, color = mpg,
tooltip = tooltip, data_id = tooltip ), size = 4)
Then I tried to export it:
oggetto <- girafe( ggobj = plot_grid(gg1, gg2), width_svg = 8, height_svg = 4)
library(svglite)
ggsave(file="test.svg", plot=oggetto, width=10, height=8)
getting the following error:
Error in UseMethod("grid.draw") :
no applicable method for 'grid.draw' applied to an object of class "c('girafe', 'htmlwidget')"
Does somebody knows how to export the images?
Thank you in advance for every eventual reply!
Giacomo
You can save the interactive plot containing the svg in a HTML file using htmltools::save_html(oggetto, "out.html")
out.html contains JavaScript e.g. ggiraphjs.min.js required to run the interactivity. Plain SVG files can display tooltips using the title attribute, but the orange point color on hover on both plots simultaneously seems to require JavaScript.
I have created some awesome graphs that I want to export to my Word document. Yea, should write in Markdown but... you know... someday!
However, how do I resize the graphs to the right dimensions while labels stay "within" the perimeter? See the following examples (code is at the end of the document).
I want to insert the following graph into my word document:
Looks great! Not when I insert it into the document:
Labels are two tiny, and I would love to stretch it vertically, so the width is greater than the height. So I managed to produce this:
And this is were I am stuck. How do I keep the labels within the perimeters? And is there a better way to "fit" the word document than guessing correct dimensions?
Thanks!
This is the code:
library(ggplot2)
df <- mpg # Load sample data
# First test graph
ggplot(data = df, mapping = aes(cyl, hwy)) +
geom_smooth() +
geom_point() +
geom_point() +
labs(y = "This is just one very long label to prove a point ..... 1234",
x = "Cyl") +
theme_classic() +
theme(legend.title = element_blank())
ggsave("test1.png")
# Modified test graph to add fit the Word document
ggplot(data = df, mapping = aes(cyl, hwy)) +
geom_smooth() +
geom_point() +
geom_point() +
labs(y = "This is just one very long label to prove a point ..... 1234",
x = "Cyl") +
theme_classic(base_size = 12) + # SIZE CHANGED
theme(legend.title = element_blank())
ggsave("test2.png", width = 8, height = 4) # DIMENSIONS DEFINED
A solution I tend to use involves the officer package as mentioned above. This used to be able export graphs as vector objects to docx so you could change sizes and text in the graph when it's in the document. This seems to have been suspended in recent versions, but still works for powerpoint. The following code puts the graph as a grouped shape in a powerpoint slide where you can tweak it before copying into word:
library(ggplot2)
library(officer)
library(tidyverse)
df <- mpg # Load sample data
# First test graph
plot2 <- ggplot(data = df, mapping = aes(cyl, hwy)) +
geom_smooth() +
geom_point() +
geom_point() +
labs(y = "This is just one very long label to prove a point ..... 1234",
x = "Cyl") +
theme_classic(base_size = 12) + # SIZE CHANGED
theme(legend.title = element_blank())
pptx <- read_pptx()
pptx %>%
add_slide() %>%
# This first line puts it in as a static png image for comparison
ph_with(plot2, location = ph_location_type(type = "body")) %>%
add_slide() %>%
# This line puts in a shape object, which can be ungrouped and edited
ph_with(rvg::dml(ggobj = plot2),
width = 8,
height = 4,
location = ph_location_type(type = "body"))
#> pptx document with 2 slide(s)
print(pptx, "test_graph.pptx")
Created on 2020-12-08 by the reprex package (v0.3.0)
That's a sort of tweaky solution which at least allows you visual control over sizes. This used to be more easily provided through the export package (available on GitHub), but it's not on CRAN anymore and behind the scenes used the now defunct parts of officer to put vector graphics in docx documents.
Edit: See this issue on GitHub for an explanation of why vector graphics to docx is no longer an option through officer.
I have an R applicaion that builds a large number of client editable charts by Office tool (pptx). I try to embed a ggplot graph into an existing slied in vector format. I use the ph_with_vg_at function to export the graph to pptx. The point is that exporting the graph to pptx deletes all that elements that already exist on the slide. How then can I export the graph to a slide without deleting the information on the slide?
Thank's
Joni
OK
It turns out to be simple if you understand the syntax...
Let's take a simple ggplot for example:
require("officer")
require("rvg")
require("ggplot2")
gg <- ggplot(mtcars, aes(x = mpg , y = wt, colour = qsec)) + geom_point() + theme_minimal()
option 1:
Opening an existing presentation and insert a new object (while deleting the objects in the presentation):
doc <- read_pptx() # Creat a virtual new pptx and set the ggplot as a new slide
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with_vg(doc, code =print(gg),type = "body")
print(doc, target = "G:/Layers/gg.pptx") # Exporting to Exist pptx file
option 2:
Opening an existing presentation and embed an object in existing slide without deleting the existing objects on the slide:
doc <- read_pptx(path = "G:/Layers/template.pptx")# Opening an Exist pptx file
doc <- on_slide( doc, index = 1)# Embedding of the ggplot into the slide
doc <- ph_with_vg_at(doc, ggobj = gg, left=0.1, top=0.1, width=7.5, height=6)
print(doc, target = "G:/Layers/template.pptx")# Exporting to Exist pptx file
Joni
Try the eoffice:
install.packages("eoffice")
library(eoffice)
gg <- ggplot(mtcars, aes(x = mpg , y = wt, colour = qsec)) + geom_point() + theme_minimal()
topptx(gg,file="gg.pptx")
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"))
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)
```