如何在R mschart中调整绘图区域的大小

时间:2018-07-30 14:41:49

标签: r mschart

我正在使用R包mschart将MS图表填充到PPT中。我使用功能ph_with_chart_at将多个图表添加到一张幻灯片中,以定义ppt的位置。但是,情节区域太小。

如何调整图的大小?

示例

这是示例代码,当您打开ppt时,我想扩大绘图区域的大小或控制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")

如果查看输出的图,您会发现该图仅占绘图区的一小部分,并在图周围留有大量空白:

enter image description here

1 个答案:

答案 0 :(得分:1)

更改地块大小

您可以在width函数中使用heightph_with_chart_at参数分别指定输出宽度和高度,而lefttop参数控制幻灯片中的位置。例如ph_with_chart_at(chart=pptsdata1, left=2, top=1, height=3, width=8)

编辑绘图边距大小(即绘图周围的空白)

在尝试隐藏轴标题时会出现问题。由于似乎没有办法完全禁止他们,所以我们:

  1. 将标签替换为单个空格,即""
  2. 将轴标题的字体大小更改为axis_title = fp_text(font.size=1)

更新您的示例:

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