【问题标题】:Is it possible to use geom_table() along facet_grid()?是否可以沿 facet_grid() 使用 geom_table()?
【发布时间】:2020-11-12 20:20:01
【问题描述】:

最近我从 ggpmisc 包中发现了函数geom_table(),它允许您在绘图中放置一个表格。但我不知道如何将不同的表格放入网格图中。

我有这个 df 和绘图:

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") 

我也有这两张表,tableA和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)
  

如何将 tableA 放在顶部图表中,将 tableB 放在底部图表中?

如果有人可以提供帮助,我将不胜感激:)

【问题讨论】:

    标签: r ggplot2 facet-grid


    【解决方案1】:

    您需要创建一个小数据框,在列表列中托管您的 tableAtableB

    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") 
    

    【讨论】:

      猜你喜欢
      • 2014-04-20
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      相关资源
      最近更新 更多