Adding sub-tables on each ggplot face bar to r

I am trying to create a complete set of graphs using facet_wrap from the ggplot2 package in R.

As a simplified example, I used a subset of the mpg dataset included in ggplot2

 library(plyr) library(ggplot2) library(gtable) library(gridExtra) myData = subset(mpg, manufacturer == "audi" | manufacturer == "chevrolet") myData = droplevels(myData) 

Here is my code for building the data:

 p = ggplot(myData, aes(x=hwy, y=cty, colour=model) ) p = p + facet_wrap( ~ manufacturer)#, scales="free") # sets panel division p = p + geom_point(size = 3) # sets points aspect p = p + geom_smooth(stat="identity") print(p) 

Now here is the hard part ... I have another dataframe 'indivParam' with additional information that I would like to display as a table on the plot. Say it is stupid:

 indivParam = ddply(myData, .(manufacturer , model), summarize, var1 = unique(class), var2 = round(mean(displ)), var3 = round(mean(cyl))) 

What I'm trying to do is add a subtable on each panel with information extracted from indivParam. For example, add the following table to the first panel of the chart:

 tg = tableGrob(subset(indivParam, manufacturer == "audi"), show.rownames=FALSE, gp=gpar(fontsize=8, lwd=2), xmin=15, xmax=30, ymin=10, ymax=20) grid.newpage() grid.draw(tg) 

I tried several options ...

  • using annotate() , but this argument does not skip data files ...

  • using annotation_custom() , as suggested in this topic: Adding a table to the ggplot build area in r

     p1 = p + annotation_custom(tableGrob(indivParam, show.rownames=FALSE, gp=gpar(fontsize=8, lwd=2)), xmin=15, xmax=30, ymin=10, ymax=20) print(p1) 

    This does not work either because it displays the entire table on each panel, and not a subtable with data related to each panel ()

  • Finally, after reading the examples on the 'tableGrob' doc page , I tried to create one grid with all the sub-valued rodents and simply overlay them on the graph:

     lg <- lapply(as.character(unique(indivParam$manufacturer)), function(x) tableGrob( as.data.frame(dlply(indivParam, .(manufacturer))[x]), name="test",show.rownames=FALSE, gp=gpar(fontsize=8, lwd=2))) grid.newpage() print(p) grid.draw(do.call(arrangeGrob, lg)) 

    But then the organization does not match the one used by the facet .., and I suspect that even if I could place two tables next to each other, they would be centered and hide the graphs ...

Is it possible to improve this last attempt by choosing the position of sub-elements? Or is there an even better way to solve this problem? It would be obvious to use geom_table() , but I don't think this geome exists (yet) ...

Any help / hint would be much appreciated! :-)

+5
source share
1 answer

Here is the solution with the awesome ggpmisc package:

 library(ggpmisc) library(dplyr) library(tibble) myData <- filter(mpg, manufacturer == "audi" | manufacturer == "chevrolet") gg <- ggplot(myData, aes(x=hwy, y=cty, colour=model)) + facet_wrap(~ manufacturer) + geom_point(size = 3) + geom_smooth(stat="identity") tb <- myData %>% group_by(manufacturer, model) %>% summarize(var1 = round(mean(displ)), var2 = round(mean(cyl))) %>% ungroup() tbs <- lapply(split(tb, tb$manufacturer), "[", -1) df <- tibble(x = rep(-Inf, length(tbs)), y = rep(Inf, length(tbs)), manufacturer = levels(as.factor(tb$manufacturer)), tbl = tbs) gg + geom_table(data = df, aes(x = x, y = y, label = tbl), hjust = 0, vjust = 1) 

enter image description here

0
source

Source: https://habr.com/ru/post/1201348/


All Articles