2

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)
NBE
  • 641
  • 2
  • 11
  • 33
  • What is the content of `HUC1_count`? What where does `theme_graphs`? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions – MrFlick Jul 13 '18 at 18:49
  • @MrFlick Sorry. Just updated question. – NBE Jul 13 '18 at 18:55
  • In the future, it would be better just to drop any lines of code that aren't directly related to the problem. Just get rid of all the extra lines of theme and customizing labels. That's just extra code to sort through that's not directly related to the problem. – MrFlick Jul 13 '18 at 19:05

1 Answers1

2

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295