Rev. | 6efbcccd7fe529130016bde441a875a084191313 |
---|---|
크기 | 1,034 bytes |
Time | 2019-03-20 18:30:34 |
Author | Lorenzo Isella |
Log Message | A code to plot tables in ggplot. |
rm(list=ls())
library(ggplot2)
library(gridExtra)
set.seed(1)
##solution with annotation
mydata <- data.frame(a=1:50, b=rnorm(50))
mytable <- cbind(sites=c("site 1","site 2","site 3","site 4"),mydata[10:13,])
gpl1 <- ggplot(mydata,aes(x=a,y=b)) +
geom_point(colour="blue") +
geom_point(data=mydata[10:13, ], aes(x=a, y=b), colour="red", size=5) +
annotation_custom(tableGrob(mytable, rows=NULL), xmin=35, xmax=50, ymin=-2, ymax=-1)
ggsave( "table_graph1.pdf", gpl1, width=8, height=5)
##solution with geom_table
library(ggpmisc)
set.seed(1)
mydata <- data.frame(a = 1:50, b = rnorm(50))
table <- cbind(sites=c("site 1", "site 2", "site 3", "site 4"), mydata[10:13, ])
gpl2 <- ggplot(mydata, aes(x = a, y = b)) +
geom_point(colour = "blue") +
geom_point(data = mydata[10:13, ], aes(x = a, y = b), colour = "red", size = 5) +
annotate(geom = "table", x = 37, y = -0.8, label = list(table),
vjust = 1, hjust = 0)
ggsave( "table_graph2.pdf", gpl2, width=8, height=5)
print("So far so good")