NBE
NBE

Reputation: 651

r: Inserting ggtexttable() inside a ggplot graph

I'm trying to insert a table that I created using the ggtexttable() function from the ggpubr package inside the plotting boundary of my ggplot plot. However, I keep getting this error:

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

I don't understand why I am getting error but I have a feeling it has to do with that I have dates on my x-axis? I would appreciate any feedback to fix this issue! Thanks!

Data:

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

Code:

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)

Upvotes: 2

Views: 2246

Answers (1)

MrFlick
MrFlick

Reputation: 206243

I see two problems here. First, tableGrob is a function for creating a grob from a data.frame. But you've already created your table do you don't need that function. But ggtexttable returns a ggplot object, but you need a grob, so you need ot use ggplotGrob to turn that ggplot object into something you can use with annotation_custom.

The second problem is the range you specific for your x values. Since your data is formatted as a Date vector, those values are stored as the number of days since 1970-01-01 so values of 1.5 and 1.8 are way outside the range of what you are actually plotting. You can see your actual range with

range(as.numeric((HUC_df1$stdate)))
# [1] 11296 11298

So fixing those two problems, what you want for the example is

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

enter image description here

Upvotes: 2

Related Questions