Search code examples
rggplot2dplyrchartsgt

how to insert gt table into a ggplot2 line chart


I want to insert the gt table to a ggplot2 line chart

here is my code for the line chart code below

here is my dataset named value to create the line ggplot2 line charts

Month Facevalue pct
January 36,434,456 -19
February 37,434,457 13
March 34,424,458 4
April 36,595,759 1
May 34,434,460 -6
June 44,434,461 54
July 22,434,462 -32
August 16,434,463 12
September 44,434,464 1
October 31,434,465 15
November 14,434,466 -4
December 11,434,467 9
p<-ggplot(data = value, aes(x=Month, y=Facevalue, group=1 , fill = Month))+
  geom_line(size = 1) +
  geom_point(color = "#0000FF" , shape = "diamond" , size = 8)+
  geom_text(aes(label = Facevalue), vjust = -0.5)+
  scale_y_continuous(label=scales::comma) +
  xlab("Monthly Face Value") + 
  ylab("Month")+
  labs(title =  "Polaris Bank Face value Trend from January to August 2021", element_text(face = "bold", colour = "white", size = 20))+
  theme_gray(base_size = 11)+
  theme(
        panel.border = element_rect(linetype = "dashed", fill = NA),
        panel.grid.major = element_line(colour = "#583759"),
        axis.line = element_line(size = 3, colour = "#0000FF"),
        axis.text = element_text(colour = "#400134"),
        axis.ticks = element_line(size = 0.5),
        axis.title.y = element_text(size = rel(1.5), angle = 90),
        axis.ticks.length.y = unit(.2, "cm"),
        panel.grid.minor = element_line(color = "red"),
  axis.ticks.length.x = unit(-.25, "cm"),
  axis.text.x = element_text(margin = margin(t = .3, unit = "cm")),
  legend.justification = "top")+
  scale_y_continuous(labels = unit_format(unit = "M", scale=1e-6))

p + coord_cartesian(ylim = c(1000000, 100000000))

which gives the chart in the image below

enter image description here

and my gt table goes like this #create a table and named it growth growth <-data.frame( Note=c("Average success rate :","Average processing duration:","Average daily count:", "Total count processed:"), June=c({""},{""},{""},{""}), July=c({""},{""},{""},{""})) #create a data frame

 #insert the growth data frame in gt table
gt(growth)%>%
   tab_header("Point")%>%
  tab_options(table.width = pct(50))%>%
  tab_options(heading.background.color = "#7D0552", table.background.color = "lightcyan",
        table.font.size = px(14), table.font.color = "#000000", table.font.weight = "bold",
        )%>%
  cols_align(align = "center")%>%
  tab_style(
    style = cell_borders(
      sides = "all",
      color = "#7D0552",
      weight = px(2),
      style = "solid"),
    locations = cells_body(
      columns = everything(),
    rows = everything()))%>%
  cols_width(
    Note ~ px(200),
    ends_with(c("e","y")) ~ px(100),
    everything() ~ px(60))

which result in this gt table image

enter image description here

so how do I insert the gt(growth) into the ggplot2 line chart to give The desired output image below

enter image description here


Solution

  • I was able to find a solution to this problem by using the patchwork package

    the name of my table I want to insert is called my_table

    my plot is p

    library (patchwork)
    wrap_plots(p,my_table) 
    

    which in return gives me the solution to the problem