【问题标题】:r: Inserting ggtexttable() inside a ggplot graphr:在 ggplot 图中插入 ggtexttable()
【发布时间】:2018-12-22 04:32:10
【问题描述】:

我正在尝试在我的 ggplot 绘图的绘图边界内插入我使用 ggpubr 包中的 ggtexttable() 函数创建的表。但是,我不断收到此错误:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class "c("gg", "ggplot")" to a data.frame

我不明白为什么会出错,但我感觉这与我的 x 轴上有日期有关?我将不胜感激任何反馈以解决此问题!谢谢!

数据:

HUC_df1 <- structure(list(charnam = c("Total dissolved solids", "Total dissolved solids", 
"Total dissolved solids"), stdate = structure(c(11297, 11296, 
11298), class = "Date"), val = c(439, 437, 510), HUC14 = c("HUC02030104020030", 
"HUC02030104020030", "HUC02030104020030")), .Names = c("charnam", 
"stdate", "val", "HUC14"), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"))
HUC1_count<-structure(list(year = "2000", n_greater = 1L, percentage = 33.33, 
    n = 3L), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-1L), .Names = c("year", "n_greater", "percentage", "n"))

代码:

library(ggpubr)
library(ggplot2)

theme_graphs<- theme_linedraw()+
  theme(plot.title=element_text(size=15, face="bold",vjust=0.5,hjust = 0.5),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        plot.background = element_blank(),
        panel.background = element_blank(),
        legend.position = c(0.5, 0.2),
        legend.background = element_blank(),
        legend.text=element_text(size=10, face="bold"))
HUC1_table<-ggtexttable(HUC1_count, 
                     theme = ttheme("classic"),rows=NULL,
                     cols=c("Year","Number of Samples\n>500",
                            "Percent of Samples\n>500","Total Samples"))
HUC1<-ggplot(data = HUC_df1, aes(x =stdate, y = val)) +
  geom_point()+
  geom_hline(aes(yintercept = 500,color="red"),size=1.3)+
  scale_y_continuous(expand = c(0, 0), limits = c(0))+
  coord_cartesian(ylim = c(0, 700))+
  scale_x_date(date_labels ="%b%Y")+
  ggtitle("Elizabeth R (below Elizabeth CORP BDY) (HUC02030104020030)\nTDS Concentration (mg/L);1997-2018") +
  xlab("Year") + ylab(" TDS Concentration (mg/L)")+
  scale_color_manual("",
                     values = c("red"),
                     labels=c("Freshwater Aquatic Life Criteria for TDS = 500 mg/L"))+

  theme_graphs+
  theme(legend.position =c(0.5, -0.098))


  HUC1<-HUC1+annotation_custom(tableGrob(HUC1_table), xmin=1.5, 
                               xmax=1.8,
                               ymin=200, ymax=300)

【问题讨论】:

  • HUC1_count的内容是什么? theme_graphs 在哪里?在寻求帮助时,您应该包含一个简单的reproducible example,其中包含示例输入和所需的输出,可用于测试和验证可能的解决方案
  • @MrFlick 对不起。刚刚更新的问题。
  • 在未来,最好只删除与问题没有直接关系的任何代码行。只需摆脱所有额外的主题和自定义标签行。这只是与问题没有直接关系的额外代码。

标签: r plot ggplot2 ggpubr


【解决方案1】:

我在这里看到两个问题。首先,tableGrob 是一个用于从 data.frame 创建 grob 的函数。但是你已经创建了你的表,你不需要那个功能。但是ggtexttable 返回一个 ggplot 对象,但您需要一个 grob,因此您需要使用 ggplotGrob 将该 ggplot 对象转换为您可以与 annotation_custom 一起使用的对象。

第二个问题是您为 x 值指定的范围。由于您的数据被格式化为日期向量,因此这些值存储为自 1970 年 1 月 1 日以来的天数,因此 1.5 和 1.8 的值超出了您实际绘制的范围。您可以使用

查看您的实际范围
range(as.numeric((HUC_df1$stdate)))
# [1] 11296 11298

所以解决这两个问题,你想要的例子是

HUC1+annotation_custom(ggplotGrob(HUC1_table), xmin=11296, 
                                xmax=11298,
                                ymin=200, ymax=300)

【讨论】:

  • 非常感谢!给小王带来不便,敬请见谅!
猜你喜欢
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 2022-06-21
相关资源
最近更新 更多