Rで解析:パワーポイントで編集可能な図を出力!「rvg」パッケージ

パワーポイントやエクセルの中で編集可能な図を出力できるパッケージの紹介です。図はRに標準で収録されている「plot」コマンドだけでなく「ggplot2」パッケージの出力に対応しています。本パッケージは「officer」パッケージと組み合わせて使用します。

なお、紹介バージョンではエクセルへの出力はエラーで失敗します。そのため、実行コマンドで参考としてコメントアウトで紹介しています。しかし、パワーポイントへの出力は問題なく、パワーポイント内で図の編集が可能です。

パッケージバージョンは0.3.0。windows11のR version 4.2.2で確認しています。

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("rvg")

実行コマンド

詳細はコマンド、パッケージのヘルプを確認してください。

・準備

###データ例の作成#####
#officerパッケージがなければインストール
if(!require("officer", quietly = TRUE)){
  install.packages("officer");require("officer")
}
#ggh4xパッケージがなければインストール
if(!require("ggh4x", quietly = TRUE)){
  install.packages("ggh4x");require("ggh4x")
}
#tidyverseパッケージがなければインストール
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}

set.seed(1234)
n <- 50
TestData <- tibble(Group = sample(paste0("Group ", 1:2),
                                  n, replace = TRUE),
                   SubGroup = sample(paste0("SubGroup ", 1:3),
                                     n, replace = TRUE),
                   Data_X = 1:n,
                   Data_Y = rnorm(n))

#########################################
###&#12300;ggh4x&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12434;&#21033;&#29992;&#12375;&#12390;&#20316;&#22259;#####
#########################################
#https://www.karada-good.net/analyticsr/r-753/
#&#21508;&#12467;&#12510;&#12531;&#12489;&#12398;strip&#12458;&#12503;&#12471;&#12519;&#12531;&#12395;&#20307;&#35009;&#12434;&#36969;&#24540;&#12377;&#12427;:strip_themed&#12467;&#12510;&#12531;&#12489;
TestStrip <- strip_themed(
  #&#27700;&#24179;&#26041;&#21521;&#12398;&#20307;&#35009;
  #list&#12391;&#21508;&#12521;&#12505;&#12523;&#12398;&#33394;&#12434;&#35373;&#23450;&#21487;&#33021;:fill,color
  background_x = 
    list(element_rect(fill = "dodgerblue"),
         element_rect(fill = "green"),
         element_rect(fill = "white")),
  text_x = elem_list_text(colour = "black",
                          face = "bold"),
  #by_layer_x&#12458;&#12503;&#12471;&#12519;&#12531;:FALSE:&#30064;&#12394;&#12427;&#35201;&#32032;&#12434;&#20491;&#12293;&#12395;&#36969;&#24540;
  by_layer_x = FALSE,
  #&#22402;&#30452;&#26041;&#21521;&#12398;&#20307;&#35009;
  background_y = elem_list_rect(
    fill = c("gold", "red")
  ),
  text_y = elem_list_text(angle = c(0, 45)),
  #by_layer_y&#12458;&#12503;&#12471;&#12519;&#12531;:FALSE:&#30064;&#12394;&#12427;&#35201;&#32032;&#12434;&#20491;&#12293;&#12395;&#36969;&#24540;
  by_layer_y = TRUE
)

#&#12503;&#12525;&#12483;&#12488;;BasePlot&#12395;&#20445;&#23384;&#12377;&#12427;
Ggh4xPlot <- ggplot(TestData, aes(x = Data_X,
                                  y = Data_Y,
                                  color = Group)) +
  geom_point() +
  theme(axis.line = element_line(colour = "black")) +
  facet_grid2(rows = vars(Group), cols = vars(SubGroup),
              scales = "fixed", space = "fixed",
              axes = "margins", remove_labels = "none",
              strip = TestStrip)

#########################################
###&#12300;ggplot2&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12434;&#21033;&#29992;&#12375;&#12390;&#20316;&#22259;###
#########################################
BoxPlot <- ggplot(data = TestData,
                  mapping = aes(x = Group,
                                 y = Data_Y,
                                 fill = Group)) +
  geom_boxplot()
########

・「rgv」パッケージでパワーポイントエクセルへ図を出力する

#&#12497;&#12483;&#12465;&#12540;&#12472;&#12398;&#35501;&#12415;&#36796;&#12415;
library("rvg")

#########################################
###&#12497;&#12527;&#12540;&#12509;&#12452;&#12531;&#12488;&#12395;&#25407;&#20837;##################
#########################################

#&#12464;&#12521;&#12501;&#12398;&#22793;&#25563;:dml&#12467;&#12510;&#12531;&#12489;
#ggplot&#20197;&#22806;&#12398;&#20316;&#22259;&#12467;&#12510;&#12531;&#12489;&#12434;&#25351;&#23450;:code&#12458;&#12503;&#12471;&#12519;&#12531;
TestPlot <- dml(code = plot(TestData$Data_X, TestData$Data_Y))
#ggplot&#12458;&#12502;&#12472;&#12455;&#12463;&#12488;&#12434;&#25351;&#23450;:ggobj
TestGGobj <- dml(ggobj = Ggh4xPlot)

###&#12300;officer&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12398;&#12467;&#12510;&#12531;&#12489;&#12391;&#22475;&#12417;&#36796;&#12416;
#&#12497;&#12527;&#12540;&#12509;&#12452;&#12531;&#12488;&#12434;&#26032;&#35215;&#20316;&#25104;:read_pptx&#12467;&#12510;&#12531;&#12489;
NewPPT <- read_pptx()
#&#12473;&#12521;&#12452;&#12489;&#36861;&#21152;:add_slide&#12467;&#12510;&#12531;&#12489;
NewPPT <- add_slide(NewPPT, layout = "Title and Content",
                    master = "Office Theme")
#&#12473;&#12521;&#12452;&#12489;&#12395;&#22259;&#12434;&#36861;&#21152;:ph_with&#12467;&#12510;&#12531;&#12489;
NewPPT <- ph_with(NewPPT, value = TestPlot,
                  location = ph_location_left())
NewPPT <- ph_with(NewPPT, value = TestGGobj,
                  location = ph_location_right())
#&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12395;&#12497;&#12527;&#12540;&#12509;&#12452;&#12531;&#12488;&#12434;&#20445;&#23384;
print(NewPPT, target = "NewPPT.pptx")

########

#########################################
###&#12456;&#12463;&#12475;&#12523;&#12395;&#25407;&#20837;########################
#########################################

###&#12300;officer&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12398;&#12467;&#12510;&#12531;&#12489;&#12391;&#22475;&#12417;&#36796;&#12416;
#&#12456;&#12463;&#12475;&#12523;&#12434;&#26032;&#35215;&#20316;&#25104;:read_xlsx&#12467;&#12510;&#12531;&#12489;
#NewXLSX <- read_xlsx()
#&#12471;&#12540;&#12488;&#12434;&#36861;&#21152;
#NewXLSX <- add_sheet(NewXLSX, label = "&#12390;&#12377;&#12392;")
##&#12300;rvg&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;
#&#12471;&#12540;&#12488;&#12395;&#22259;&#12434;&#36861;&#21152;:xl_add_vg&#12467;&#12510;&#12531;&#12489;;
#&#35373;&#23450;&#12375;&#12390;&#12418;"Feuil1"&#12471;&#12540;&#12488;&#12364;&#36861;&#21152;&#12373;&#12428;&#12414;&#12377;
#ggplot&#20197;&#22806;&#12398;&#20316;&#22259;&#12467;&#12510;&#12531;&#12489;&#12434;&#25351;&#23450;:code&#12458;&#12503;&#12471;&#12519;&#12531;
#NewXLSX <- xl_add_vg(NewXLSX, sheet = "&#12390;&#12377;&#12392;",
#                     code = plot(TestData$Data_X, TestData$Data_Y), 
#                     width = 5, height = 5, left = 1, top = 2 )
#ggplot&#12458;&#12502;&#12472;&#12455;&#12463;&#12488;&#12434;&#25351;&#23450;:ggobj
#NewXLSX <- xl_add_vg(NewXLSX, sheet = "&#12390;&#12377;&#12392;",
#                     code = print(Ggh4xPlot), 
#                     width = 5, height = 5, left = 2, top = 2 )
#&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12395;&#12456;&#12463;&#12475;&#12523;&#12434;&#20445;&#23384;
#print(NewXLSX, target = "NewXLSX.xlsx")
########

出力例


少しでも、あなたの解析が楽になりますように!!

Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.
タイトルとURLをコピーしました