简体   繁体   中英

Is it possible to use geom_table() along facet_grid()?

Recently I discovered the function geom_table() , from ggpmisc package, which allows you to put a table inside a plot. But I don't know how to put different tables into a grid plot.

I have this df and plot:

library(lubridate)
library(ggplot2)
library(ggpmisc)

Date <- c("2010-01-28", "2010-02-28", "2010-03-28", 
          "2010-04-28", "2010-05-28", "2010-06-28", 
          "2010-07-28", "2010-08-28", "2010-09-28", 
          "2010-10-28")

Date <- as_date(Date)

Country <- rep("Japan", 10)
A <- runif(10, min=30, max=90)
B <- runif(10, min = 1, max = 15)


df <- data.frame(Date, Country, A, B)


df %>% pivot_longer(-c(Date, Country)) %>%
  ggplot(aes(x=Date,y=value,group=1,color=Country))+ 
  geom_line(size = 0.9) +
  facet_grid(name~Country, scales = "free", switch = "y") 

在此处输入图像描述

I also have these two tables, tableA and tableB:

Time <- c("Today", "Yesterday", "One week ago")

Value_A <- 10:12
Value_B <- 1:3

tableA <- data.frame(Time, Value_A)
tableB <- data.frame(Time, Value_B)
  

How I put tableA in the top graph and tableB in the bottom graph?

I appreciate it if someone can help:)

You need to create a little data frame that hosts your tableA and tableB in a list column:

d <- tibble(x = c(0.95, 0.95), y = c(0.95, 0.95),
            name = c("A", "B"), tb = list(tableA, tableB))

df %>% pivot_longer(-c(Date, Country)) %>%
  ggplot(aes(x=Date,y=value,group=1,color=Country))+ 
  geom_line(size = 0.9) +
  geom_table_npc(data = d, aes(npcx = x, npcy = y, label = tb)) +
  facet_grid(name~Country, scales = "free", switch = "y") 

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM