r - How can I add a table to my ggplot2 output? -
is there quick way add table ggplot2 graph? table have value of each line @ same breakpoints specified in scale_x_continuous(), percentage (%) symbol next them. end goal create image below. however, don't know how add table.
the following block of code makes 2 lines in ggplot2 , should adequate provide me example:
require(ggplot2) df <- data.frame(a = seq(0, 90, 10), b = seq(10, 100, 10)) df.plot <- ggplot(data = df, aes(x = seq(1, 100, 10))) + geom_line(aes(y = a), colour = 'red') + geom_line(aes(y = b), colour = 'blue') + scale_x_continuous(breaks = seq(0,100,10)) df.plot a similar question asked here, given answer more of workaround , wouldn't table 2 rows. going mess around clues provided brian diggs, figured post in case has done this. appreciated!
edit: @baptiste helping me figure out. posted own response below finished started.
here's basic example of strategy used learnr:
require(ggplot2) df <- data.frame(a = seq(0, 90, 10), b = seq(10, 100, 10)) df.plot <- ggplot(data = df, aes(x = seq(1, 100, 10))) + geom_line(aes(y = a), colour = 'red') + geom_line(aes(y = b), colour = 'blue') + scale_x_continuous(breaks = seq(0,100,10)) # make dummy labels table content df$lab <- month.abb[ceiling((df$a+1)/10)] df.table <- ggplot(df, aes(x = a, y = 0, label = lab, colour = b)) + geom_text(size = 3.5) + theme_minimal() + scale_y_continuous(breaks=null)+ theme(panel.grid.major = element_blank(), legend.position = "none", panel.border = element_blank(), axis.text.x = element_blank(), axis.ticks = element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank()) ga <- ggplotgrob(df.plot) gb <- ggplotgrob(df.table)[6,] gb$heights <- unit(1,"line") require(gridextra) gab <- rbind(ga, gb) grid.newpage() grid.draw(gab) 
Comments
Post a Comment