Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grob参数从data.frame到ggplot2_R_Dataframe_Ggplot2_Arguments_Grob - Fatal编程技术网

Grob参数从data.frame到ggplot2

Grob参数从data.frame到ggplot2,r,dataframe,ggplot2,arguments,grob,R,Dataframe,Ggplot2,Arguments,Grob,如何将一组grob(ximin、xmax、ymin、ymax)的参数从data.frame传递到annotation_自定义函数(ggplot2)。下面的示例说明了该问题 # Grobs a <- ggplot(diamonds, aes(carat)) + geom_histogram(fill = "red") + theme_void() g1 <- ggplotGrob(a) b <- ggplot(diamonds, aes(carat)) + geom_histog

如何将一组grob(ximin、xmax、ymin、ymax)的参数从data.frame传递到annotation_自定义函数(ggplot2)。下面的示例说明了该问题

# Grobs
a <- ggplot(diamonds, aes(carat)) + geom_histogram(fill = "red") + theme_void()
g1 <- ggplotGrob(a)
b <- ggplot(diamonds, aes(carat)) + geom_histogram(fill = "blue") + theme_void()
g2 <- ggplotGrob(b)

# Structure of the arguments location
my.data <- data.frame(grob = c("g1", "g2"), xmin = c(1.5, 5.5), xmax = c(4.5, 8), 
                  ymin = c(2.5, 4), ymax = c(6, 8))

# Desired result
df <- data.frame(x = 1:10, y = 1:10)
ggplot(df, aes(x, y)) + geom_blank() + theme_bw() +
annotation_custom(g1, xmin = 1.5, xmax = 4.5, ymin = 2.5, ymax = 6) +
annotation_custom(g2, xmin = 5.5, xmax = 8, ymin = 4, ymax = 8)
#Grobs

a使用
geom_custom()
会更自然,但同时您可以创建一个图层列表

my.data <- data.frame(xmin = c(1.5, 5.5), xmax = c(4.5, 8), 
                      ymin = c(2.5, 4), ymax = c(6, 8))
my.data$grob <- list(g1,g2)
get_grob <- function(grob, xmin, xmax, ymin, ymax){
  annotation_custom(grob=grob[[1]], xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)
}
df <- data.frame(x = 1:10, y = 1:10)
ggplot(df, aes(x, y)) + geom_blank() + theme_bw() +
  plyr::mlply(my.data, get_grob)
my.data