如何在R中将一个表和多个图组合成一个组合图?

abithluo  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(111)

我试图在R中将多个图和一个表组合成一个组合图。我的情节是伟大的,直到我试图包括表以及。就在这时,我收到了这条消息:gList(...)中的错误:"gList"中只允许'grobs'。我猜它不是最好的工作,所有的图都是通过ggplots2制作的,结果是列表,表格只是一个使用可编辑函数的虚构数据框。下面是我创建表格的代码以及合并图的代码
表格代码:

table1 <- FinalPitches %>%
  arrange(desc(Pitches)) %>%
  kable(col.names = c("Player Name", "Pitch Type", "Pitches", "Velocity", "Horizontal Break", "Vertical Break",
                      "Spin Rate", "VAA", "Strike%","CSW%", "Whiff%", "Stuff+")) %>%
  kable_styling()

合并代码:

# Define the first arrangeGrob
first_arrange <- arrangeGrob(
  BreakChart,
  LocationMap,
  nrow = 1,
  widths = c(0.40, 0.50)  # Adjusted widths to add up to 1
)

# Define the second arrangeGrob with smaller widths
second_arrange <- arrangeGrob(
  Even1,
  Ahead1,
  Behind1,
  TwoStrikes1,
  nrow = 1,
  widths = c(0.15, 0.15, 0.15, 0.15)  # Adjusted widths to make them smaller
)

# Define the third
third_arrange <- arrangeGrob(
  table1,
  nrow = 1,
  widths = c(0.15)  # Adjusted widths to make them smaller
)

# Combine the plots into a grid layout
combined_plot6 <- grid.arrange(
  first_arrange,   # First row (top)
  second_arrange, # Second row (bottom)
  third_arrange,  # third row (bottom)
  ncol = 1,        # Single column layout
  nrow = 3,        # Dividing the grid into two rows
  heights = unit(c(1, 1, 0.5), c("null", "null", "null"))  # Adjusted heights for the rows
)

# Display the combined plot
print(combined_plot6)

我很抱歉没有可复制的代码,但如果任何人都可以得到它有点工作与表和情节被包括在一起,这将是了不起的,我会接受作为正确的答案。谢谢!!这是一张我想要的照片。底部的可视化是完美的,我只是不能得到表添加到图形。

gab6jxml

gab6jxml1#

这里有一个方法可以应用于您的问题:

library(ggplot2)
library(patchwork)
library(gridExtra) # for tableGrob
library(dplyr)# to help with creating a minimal table
library(tibble) # to remove rownames

#preparing illustrative table and graphs

tbl <- 
  mtcars |> 
  mutate(names = row.names(mtcars)) |> 
  select(names, everything()) |> 
  remove_rownames()

# I'm not sure kable is easily convertable into a grob,
# so use gridExtra for table, as this play better with grobs and patchwork.

tbl1 <- tableGrob(tbl[1:3, 1:10], theme=ttheme_minimal(), rows=NULL)

p1 <- 
  ggplot(mtcars, aes(mpg, cyl)) +
  geom_point()

p2 <- 
  ggplot(mtcars, aes(mpg, disp)) +
  geom_point()

#nominal pie chart

pie1 <- mtcars  |> 
  summarise(count = n(), .by = cyl)  |> 
  mutate(cyl = as.factor(cyl)) |> 
  ggplot(aes(x = "x", y = count, fill = cyl)) +
  geom_bar(stat = "identity", position = "stack") + 
    geom_text(aes(label = count), position = position_stack(vjust = 0.5), size = 8) +  
    labs(title = "Pie chart 1") +
  coord_polar(theta = "y") +
    theme_void() +
    theme(legend.position = "none")

plot_layout <- 
  "AAAA
   BBCC
   DEFG"  
  
  wrap_plots(tbl1, p1, p2, pie1, pie1, pie1, pie1,
             design = plot_layout)

创建于2023-06-24带有reprex v2.0.2

相关问题