admin管理员组

文章数量:1399805

I have a code that makes a bunch of ggplot object, saves them as variables, then adds them to a list. I then loop through the list of plots adding them to slides in a powerpoint file using officeR. This works fine and great, because I can easily do:

my_list <- list()
for (df_num in names(list_o_dfs)){
    df <- list_o_dfs[[df_num]] 
    df %>% ggplot(x=X, y=Y) + geom_point() -> my_plot
    str_glue("df_{df_num}") -> slide_title
    my_list[[slide_title]] <- my_plot
}

Then a little of this lets me save every plot as a slide.

ppt <- read_pptx()
for (plot_ttl in names(my_list)){
  ppt <- add_slide(ppt, "Title and Content")
  ppt <- ph_with(
    ppt,
    value = plot_ttl,
    location = ph_location_label("Title 1")
  )
  ppt <- ph_with(ppt,
                 value = my_list[[plot_ttl]],
                 location = ph_location_label("Content Placeholder 2"))}
  print(ppt, target = "example.pptx"))
  
  }

Along those lines my_list[[df_1]] (or my_plot) will replot the appropriate (or last) plot object for me.

HOWEVER, I would like to use the base R heatmap() function which seems to just output a normal list. So if I call: heatmap(matrix(rexp(200, rate=.1), ncol=20))

I get a lovely heatmap: But then I try to store/save it and recall it with something like:

my_list <- list()
heatmap(matrix(rexp(200, rate=.1), ncol=20)) -> hm
my_list[['Heatmap']] <- hm

Calling hm or my_list[['Heatmap']] returns:

$rowInd
 [1] 10  2  6  5  4  8  9  7  1  3

$colInd
 [1]  5 10 11  2 17 14  3 18 19  9 12  6 15  1 20 13  4  7  8 16

$Rowv
NULL

$Colv
NULL

with no plot to accompany it. plot(hm) returns:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
'x' is a list, but does not have components 'x' and 'y'

Is there a way to make an object or variable with my heatmap that can easily be recalled in plot form so I can add it to my powerpoint? Do I have to save it as a png and load it back in?

I have a code that makes a bunch of ggplot object, saves them as variables, then adds them to a list. I then loop through the list of plots adding them to slides in a powerpoint file using officeR. This works fine and great, because I can easily do:

my_list <- list()
for (df_num in names(list_o_dfs)){
    df <- list_o_dfs[[df_num]] 
    df %>% ggplot(x=X, y=Y) + geom_point() -> my_plot
    str_glue("df_{df_num}") -> slide_title
    my_list[[slide_title]] <- my_plot
}

Then a little of this lets me save every plot as a slide.

ppt <- read_pptx()
for (plot_ttl in names(my_list)){
  ppt <- add_slide(ppt, "Title and Content")
  ppt <- ph_with(
    ppt,
    value = plot_ttl,
    location = ph_location_label("Title 1")
  )
  ppt <- ph_with(ppt,
                 value = my_list[[plot_ttl]],
                 location = ph_location_label("Content Placeholder 2"))}
  print(ppt, target = "example.pptx"))
  
  }

Along those lines my_list[[df_1]] (or my_plot) will replot the appropriate (or last) plot object for me.

HOWEVER, I would like to use the base R heatmap() function which seems to just output a normal list. So if I call: heatmap(matrix(rexp(200, rate=.1), ncol=20))

I get a lovely heatmap: But then I try to store/save it and recall it with something like:

my_list <- list()
heatmap(matrix(rexp(200, rate=.1), ncol=20)) -> hm
my_list[['Heatmap']] <- hm

Calling hm or my_list[['Heatmap']] returns:

$rowInd
 [1] 10  2  6  5  4  8  9  7  1  3

$colInd
 [1]  5 10 11  2 17 14  3 18 19  9 12  6 15  1 20 13  4  7  8 16

$Rowv
NULL

$Colv
NULL

with no plot to accompany it. plot(hm) returns:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
'x' is a list, but does not have components 'x' and 'y'

Is there a way to make an object or variable with my heatmap that can easily be recalled in plot form so I can add it to my powerpoint? Do I have to save it as a png and load it back in?

Share Improve this question asked Mar 24 at 21:19 Bradley SutliffBradley Sutliff 655 bronze badges 1
  • @stefan's answer worked like a charm for my use case. I was curious if there is another way to generally "save" and recall base plots and finally found this thread on recordPlot, in case that is helpful to anyone else in the future. – Bradley Sutliff Commented Mar 25 at 13:20
Add a comment  | 

1 Answer 1

Reset to default 3

To export a base plot you have to wrap the plotting code in officer::plot_instr:

library(officer)

my_list <- lapply(
  1:2,
  \(x) plot_instr(heatmap(matrix(rexp(200, rate = .1), ncol = 20)))
)
names(my_list) <- c("plot1", "plot2")

ppt <- read_pptx()
for (plot_ttl in names(my_list)) {
  ppt <- add_slide(ppt, "Title and Content")
  ppt <- ph_with(
    ppt,
    value = plot_ttl,
    location = ph_location_label("Title 1")
  )
  ppt <- ph_with(ppt,
    value = my_list[[plot_ttl]],
    location = ph_location_label("Content Placeholder 2")
  )
}
print(ppt, target = "example.pptx")

本文标签: rSave heatmap outputobjectStack Overflow