2

I'm using R package mschart to populate MS chart into PPT. I have added add multiple charts into one slide using the function ph_with_chart_at to define the location of ppt. However, the plot area is too small.

How can I resize the plot?

Example:

here is the sample code, when you open the ppt, I want to enlarge the size the plot area or control the space between axis y 40%, 50%,60%...

library(officer)
library(mschart)
library(dplyr)

data <- data.frame(Name=c("a","a","a","a","b","b","b","b"),
                   wave_id=c("2017Q1","2017Q2","2017Q3","2017Q4","2017Q1","2017Q2","2017Q3","2017Q4"),
pct=c(0.68,0.71,0.70,0.72,0.57,0.57,0.57,0.58))

data1 <- data%>%
  ms_linechart(x="wave_id",y="pct",group="Name")%>%
  chart_labels(title=NULL,xlab="",ylab="")%>%
  chart_ax_y(limit_min = 0.4,limit=0.8,
             num_fmt='0%%',major_tick_mark="none",minor_tick_mark="none")

data1_theme<- mschart_theme(
  legend_text = fp_text(font.size=8),
  axis_text = fp_text(font.size=8),
  legend_position = "r",
  grid_major_line=fp_border(width=))

pptsdata1 <- set_theme(data1,data1_theme)


doc <- read_pptx()

doc <-doc%>% 
  add_slide(layout = "Title and Content", master = "Office Theme")%>%
  ph_with_chart_at(chart=pptsdata1,left=1,top=2,height=1.55,width=8)

print(doc, target = "my_plot.pptx")

If you look at the outputted graph, you can see that the plot only takes up a small amount of the plot area, with a lot of white space left around the plot:

enter image description here

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
delia
  • 31
  • 5
  • 1
    Can you provide a basic example of your problem? Check out here on how to make a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Michael Harper Jul 30 '18 at 14:43
  • Also, have you tried the `width` and `height` arguments within the `ph_with_chart_at` function? – Michael Harper Jul 30 '18 at 14:49
  • The width and height arguments within the ph_with_chart_at is used to control the position of chart on ppt. I'm OK with that. My issue is when I create line chart, the plot area is very small, and I need to go to ppt manually resize the plot area to fit the report. – delia Jul 30 '18 at 14:54
  • According to the [documentation](https://cran.r-project.org/web/packages/mschart/mschart.pdf), `left` and `top` should control the position on the existing slide. I willl happy to assist if you can provide an example code: at the moment I can only guess you problem. Please edit the question to include this example. – Michael Harper Jul 30 '18 at 14:59

1 Answers1

2

Changing the plot size

You can use the width and height arguments within the ph_with_chart_at function to specify the output width and height respectively, while the left and top arguments control the position in the slide. For example ph_with_chart_at(chart=pptsdata1, left=2, top=1, height=3, width=8)

Editing Size of Plot Margins (i.e. whitespace around plot)

You problem arises as you are trying to hide the titles of the axes. As there appears no way to suppres them completely, what we:

  1. Replace the labels with a single space i.e. ""
  2. Change the font size of the axis title to axis_title = fp_text(font.size=1)

Updating your example:

library(officer)
library(mschart)
library(dplyr)

data <- data.frame(Name=c("a","a","a","a","b","b","b","b"),
                   wave_id=c("2017Q1","2017Q2","2017Q3","2017Q4","2017Q1","2017Q2","2017Q3","2017Q4"),
pct=c(0.68,0.71,0.70,0.72,0.57,0.57,0.57,0.58))

data1 <- data%>%
  ms_linechart(x="wave_id",y="pct",group="Name")%>%
  chart_labels(title=NULL,xlab=" ",ylab=" ")%>%
  chart_ax_y(limit_min = 0.4,limit=0.8,
             num_fmt='0%%',major_tick_mark="none",minor_tick_mark="none")

data1_theme<- mschart_theme(
  axis_title = fp_text(font.size=1),
  legend_position = "r",
  grid_major_line=fp_border(width=))

pptsdata1 <- set_theme(data1, data1_theme)

doc <- read_pptx()

doc <-doc%>% 
  add_slide(layout = "Title and Content", master = "Office Theme")%>%
  ph_with_chart_at(chart=pptsdata1, left=1,  top=1,  height=3,  width=8)

print(doc, target = "my_plot.pptx")

enter image description here

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • This is the chart area, I want to resize the plot area (which doesn't include legend, axis title) – delia Jul 30 '18 at 15:57
  • Okay, I think we have got there now! I hope you my feedback has helped you see how to make the question clearer in future cases: it is important to clearly state the exact problem so that others know how to help :) if you find the solution useful, can you mark it as accepted and upvote it please? https://stackoverflow.com/help/someone-answers – Michael Harper Jul 30 '18 at 16:15
  • Can you explian why legend_text=fp_text(font.size=0)? – delia Jul 30 '18 at 17:43
  • I'll tell you if you mark the answer as accepted and upvote it ;) just kidding, I think that was left by accident. Just removed it from the answer. – Michael Harper Jul 30 '18 at 18:21