It is still not clear to me what you precisely want to do.
You include an Rmd-file (when doing so use four backticks!) producing a doc-file
However I would not know how to edit and position an image.
For editing an image you could the package magick and afterwards include that image in your doc file. As I said I am not sure which (if any) control you then have over the placement/positioning of images.
In the remainder of this post I first include my version of the Rmd file (with the four backticks) and after that an example of the use of magick to annotate your graph. Placement of annotations should be changed of course.
Rmd-file (this line should be removed)
---
title: "Untitled"
author: "My name"
date: '2022-07-15'
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#library(readxl)
library(dplyr)
library(knitr)
library(ggplot2)
```
```{r}
# data = read_xlsx(choose.files())
data <- tibble::tribble(
~`Account Name` , ~`Sales Stage`, ~`TCV Value` ,
"a" ,"SS1", 314,
"a" ,"SS1", 271,
"a" ,"SS2", 600,
"b" ,"SS1", 414,
"b" ,"SS1", 371,
"b" ,"SS2", 700,
)
#Account-wise TCV aggregation
agg_tcv_account = data%>%group_by(`Account Name`)%>%summarise(tcv= sum(`TCV Value`))
#Total value of SS2
total_ss1_val = data%>%subset(`Sales Stage`== "SS1")%>%select(`TCV Value`)%>%sum()
total_ss2_val = data%>%subset(`Sales Stage`=="SS2")%>%select(`TCV Value`)%>%sum()
Total_d_val = data%>%subset(`Account Name`== "a")%>%select(`TCV Value`)%>%sum()
```
## Results
*Sales Stage Insights*
The value of deals in Sales Stage 2 is `r total_ss2_val`.
The value of deals in Sales Stage 1 is `r total_ss1_val`.
Total value of Account D is `r Total_d_val`
```{r table}
table1 = kable(agg_tcv_account)
table1
```
```{r plot}
plot1=ggplot(data) +
aes(x = `Sales Stage`, y = `TCV Value`, fill = `Account Name`) +
geom_col() +
scale_fill_hue(direction = 1) +
theme_minimal()
plot1
```
R-file that uses Magick (this line should be removed)
#library(readxl)
library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.1.2
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
library(magick)
#> Linking to ImageMagick 6.9.12.3
#> Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fontconfig, x11
# data = read_xlsx(choose.files())
data <- tibble::tribble(
~`Account Name` , ~`Sales Stage`, ~`TCV Value` ,
"a" ,"SS1", 314,
"a" ,"SS1", 271,
"a" ,"SS2", 600,
"b" ,"SS1", 414,
"b" ,"SS1", 371,
"b" ,"SS2", 700,
)
plot1=ggplot(data) +
aes(x = `Sales Stage`, y = `TCV Value`, fill = `Account Name`) +
geom_col() +
scale_fill_hue(direction = 1) +
theme_minimal()
ggsave("myplot1.jpeg")
#> Saving 7 x 5 in image
total_ss1_val = data%>%subset(`Sales Stage`== "SS1")%>%select(`TCV Value`)%>%sum()
total_ss2_val = data%>%subset(`Sales Stage`=="SS2")%>%select(`TCV Value`)%>%sum()
Total_d_val = data%>%subset(`Account Name`== "a")%>%select(`TCV Value`)%>%sum()
p1 <- "Sales Stage Insights"
p2 <- paste("The value of deals in Sales Stage 2 is", total_ss2_val,".")
p3 <- paste("The value of deals in Sales Stage 1 is", total_ss1_val,".")
p4 <- paste("Total value of Account D is", Total_d_val,".")
p1234 <- c(p1,p2,p3,p4)
my_annotate <- function(text, x, y, size, color="white", bg.color="black",
family="Lato Black", bg.r=0.06, fontface=2) {
annotate("text", label=text, x=x, y=y,size=size,
color="blue", # bg.color=bg.color,
family=family, # bg.r=bg.r,
fontface=fontface)
}
im <- image_read("myplot1.jpeg")
iminfo <- image_info(im)
# str(iminfo)
# tibble [1 x 7] (S3: tbl_df/tbl/data.frame)
# $ format : chr "JPEG"
# $ width : int 2100
# $ height : int 2100
# $ colorspace: chr "sRGB"
# $ matte : logi FALSE
# $ filesize : int 113743
# $ density : chr "300x300"
width <- pull(iminfo, "width")
height <- pull(iminfo, "height")
# device that will contain variable data
fig <-
image_graph(width = width, height = height, bg = "transparent")
# empty plot that will contain variable data
p <- ggplot() + xlim(0, width) + ylim(0, height) + theme_void()
for (i in 1:4) {
p <- p + my_annotate(p1234[i], width / 2 - 600 , height / 2 - i*60, 12)
}
print(p)
dev.off()
#> png
#> 2
out <- image_composite(im, fig, operator = 'Over')
image_write(out, "total.jpeg")
Created on 2022-07-15 by the reprex package (v2.0.1)