Hi All,
I want to export ggplot2 graph to PowerPoint, but when I do it the regression equation
is going to be distorted. Why is that ? What should I do better ?
I want my graph to be editable in Powerpoint , when I export as bitmap obviously everything is OK, but is not editable.
library(tidyverse)
library(ggpmisc)
#>
#> Attaching package: 'ggpmisc'
#> The following object is masked from 'package:ggplot2':
#>
#> annotate
library(rvg)
library(officer)
fit <- lm(disp ~ mpg, data = mtcars)
ggplotRegression2 <- function(fit) {
require(ggplot2)
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
labs(title = paste(
"Adj R2 = ", signif(summary(fit)$adj.r.squared, 5),
"Intercept =", signif(fit$coef[[1]], 5),
" Slope =", signif(fit$coef[[2]], 5),
" P =", signif(summary(fit)$coef[2, 4], 5)))
}
p <- ggplotRegression2(fit)
my.formula <- y ~ x
y <- mtcars$disp
x <- mtcars$mpg
p + labs(title = paste(
"Adj R2 = ", signif(summary(fit)$adj.r.squared, 5),
"Intercept =", signif(fit$coef[[1]], 5),
" Slope =", signif(fit$coef[[2]], 5),
" P =", signif(summary(fit)$coef[2, 4], 5)
), subtitle = "My subtitle", caption = "My caption") +
geom_smooth(method = "lm", se = TRUE, color = "red", formula = my.formula) +
stat_poly_eq(
formula = my.formula,
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")), parse = TRUE, label.x = "right", geom = "label_npc", alpha = 0.33)
#> `geom_smooth()` using formula 'y ~ x'
p2 <- p + labs(title = paste(
"Adj R2 = ", signif(summary(fit)$adj.r.squared, 5),
"Intercept =", signif(fit$coef[[1]], 5),
" Slope =", signif(fit$coef[[2]], 5),
" P =", signif(summary(fit)$coef[2, 4], 5)
), subtitle = "My subtitle", caption = "My caption") +
geom_smooth(method = "lm", se = TRUE, color = "red", formula = my.formula) +
stat_poly_eq(
formula = my.formula,
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")), parse = TRUE, label.x = "right", geom = "label_npc", alpha = 0.33)
#as vector graphic
t <- "example.pptx"
fig_vg <- dml(ggobj = p2)
read_pptx() %>%
add_slide(layout = "Title Only", master = "Office Theme") %>%
ph_with(fig_vg, location = ph_location(left = .5, top = 1.3, height = 5, width = 5)) %>%
print(t)
#> `geom_smooth()` using formula 'y ~ x'
#as bitmap
t <- "example.pptx"
fig_vg2 <- p2
read_pptx() %>%
add_slide(layout = "Title Only", master = "Office Theme") %>%
ph_with(fig_vg2, location = ph_location(left = .5, top = 1.3, height = 5, width = 5)) %>%
print(t)
#> `geom_smooth()` using formula 'y ~ x'
Created on 2020-11-15 by the reprex package (v0.3.0)
Any help is much appreciated,
regards,