如何在 R mschart 中调整绘图区域的大小 [英] how to resize plot area in R mschart
问题描述
我正在使用 R 包 mschart
将 MS 图表填充到 PPT 中.我使用函数 ph_with_chart_at
添加了将多个图表添加到一张幻灯片中以定义 ppt 的位置.但是,地块面积太小了.
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.
如何调整绘图大小?
示例:
这是示例代码,当你打开ppt时,我想放大绘图区域的大小或控制y轴之间的空间40%、50%、60%...
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:
推荐答案
改变绘图大小
您可以在 ph_with_chart_at
函数中使用 width
和 height
参数分别指定输出宽度和高度,而 left
和 top
参数控制幻灯片中的位置.例如 ph_with_chart_at(chart=pptsdata1, left=2, top=1, height=3, width=8)
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)
编辑图边距的大小(即图周围的空白)
当您试图隐藏轴的标题时出现问题.由于似乎没有办法完全抑制它们,我们:
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:
- 用单个空格替换标签,即
""
- 将轴标题的字体大小更改为
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")
这篇关于如何在 R mschart 中调整绘图区域的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!