我一直试图将png插入到刻面的ggplot中。我已经关注http://es.codeover.org/questions/32807665/removing-one-tablegrob-when-applied-to-a-box-plot-with-a-facet-wrap并且可以使用样本数据集,但不能使用我的数据集。
我的代码:
library(ggplot2)
library(data.table)
library(scales)
library(plyr)
library(reshape2)
library(car)
library(gridExtra)
library(cowplot)
library(grid)
library(png)
annotation_custom2 <-
function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data)
{
layer(data = data, stat = StatIdentity, position = PositionIdentity,
geom = ggplot2:::GeomCustomAnn, inherit.aes = TRUE, params = list(grob = grob,xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax))
}
这有效:
p <- ggplot(mtcars) + geom_point(aes(mpg, wt)) + facet_wrap(~ cyl)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g1 <- rasterGrob(img, interpolate=FALSE)
g <- grobTree(grobTree(g1, vp=viewport(x=unit(0,"npc"), y=unit(0,"npc"))))
p + annotation_custom2(g, data=data.frame(cyl=8))
我的数据给出错误:eval中的错误(expr,envir,enclos):找不到对象'基因型'
p <- ggplot(FCStd, aes(x = genotypes, y = StandTotal, colour = type)) + geom_point(size = 3.5) + facet_wrap(~type, scales = "free_x")
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g1 <- rasterGrob(img, interpolate=FALSE)
g <- grobTree(grobTree(g1, vp=viewport(x=unit(0,"npc") + 0.5*sum(tg$widths), y=unit(0,"npc") + 0.5*sum(tg$heights))))
p + annotation_custom2(g1, data = data.frame(type == "invasive"))
dput(FCStd)
structure(list(genotypes = structure(c(29L, 27L, 19L, 20L, 18L,
21L, 28L, 30L, 9L, 5L, 3L, 7L, 24L, 4L, 23L, 6L, 8L, 22L, 17L,
12L, 31L, 14L, 11L, 2L, 26L, 25L, 16L, 1L, 13L, 10L, 15L, 32L
), .Label = c("Afghanistan PI250782", "Algeria W65435", "Australia PI197336",
"Australia W65527", "Brazil PI404356", "Chile PI368930", "Chile PI368939",
"Chile PI368940", "Chile PI368959", "CM NS ", "CM S ", "GIL NS ",
"Greece W65319", "Hungary W65565", "Iran PI227025", "Malta W65375",
"Mt Wilson-2", "Mt. Wilson-4", "NM58-12", "NM58-13", "NM58-35",
"Peru PI308523", "Rivercrest-1", "Rivercrest-11", "Spain W65325",
"Spain W65390", "St. Augustine-11", "St. Augustine-14", "St. Augustine-3",
"Starlight", "Turkmenistan W62449", "Uzbekistan W68297"), class = "factor"),
type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("invasive", "native"
), class = "factor"), StandTotal = c(1, 1, 1, 0.2, 0.75,
0.5, 1.272727273, 0.666666667, 0.666666667, 0, 1.285714286,
1, 1, 1, 0.4, 1, 0.5, 1, 0.666666667, 1.259259259, 1.375,
1, 1.142857143, 1.166666667, 1.5, 1.5, 1.8, 1, 0.75, 0, 1,
1), LatGroup4 = c(4L, 4L, 6L, 6L, 6L, 6L, 4L, 6L, 2L, 2L,
1L, 2L, 4L, 1L, 4L, 2L, 2L, 3L, 6L, 7L, 7L, 9L, 7L, 6L, 5L,
7L, 6L, 5L, 7L, 7L, 6L, 8L), LatGroup13 = c(3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 2L, 3L,
3L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 4L), LatGroupNI = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L,
1L, 1L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L,
2L), .Label = c("I", "N", "NI"), class = "factor")), .Names = c("genotypes",
"type", "StandTotal", "LatGroup4", "LatGroup13", "LatGroupNI"
), class = "data.frame", row.names = c(NA, -32L))