我正在使用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")
如果查看输出的图,您会发现该图仅占绘图区的一小部分,并在图周围留有大量空白:
答案 0 :(得分:1)
更改地块大小
您可以在width
函数中使用height
和ph_with_chart_at
参数分别指定输出宽度和高度,而left
和top
参数控制幻灯片中的位置。例如ph_with_chart_at(chart=pptsdata1, left=2, top=1, height=3, width=8)
编辑绘图边距大小(即绘图周围的空白)
在尝试隐藏轴标题时会出现问题。由于似乎没有办法完全禁止他们,所以我们:
""
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")