2016-03-24 16 views
1

Bu gönderi bir kaç gün önce bir question sorusuyla ilgilidir ve özellikle bu post'a (bundan aşağıdaki kodu ödünç aldım) sordum. Yeni bir soru gerektirdiğini düşündüm çünkü bu yeni bir konu.R: png rasterlerinin oluşturulması, grid.raster öğesinin okunması

ggplot2'de geom_point için özel resimlerle grafik çizmeye çalışıyorum, sonra bu görüntüleri efsaneye yapıştırın. Ancak, görüntü rasterlerini bir listeye koyduğumda ve grid.raster'daki bu listenin referans öğeleri bir hata atar. Png rasterlerini bir listede saklamak için bir yol var mı? Böylece daha sonra grid.raster'den çağrılabilirler? İşte bir örnek ...

library(png) 
library(ggplot2) 
library(grid) 

# Get image 
img <- readPNG(system.file("img", "Rlogo.png", package="png")) 

# Grab online image 
url <- "https://www.rstudio.com/wp-content/uploads/2014/06/RStudio-Ball.png" 
destfile <- "myfile.png" 
r_studio <- download.file(url, destfile, mode="wb") 
r_studio <- readPNG("myfile.png") 

# Put images in list  
image_list <- list(img, r_studio) 

# Plot 
p = ggplot(mtcars, aes(mpg, disp, colour = factor(vs))) + 
    geom_point() + 
    theme(legend.key.size = unit(1, "cm")) 

# Get ggplot grob 
gt = ggplotGrob(p) 
grid.newpage() 
grid.draw(gt) 


# Search using regular expressions 
Tree = as.character(current.vpTree()) 
pos = gregexpr("\\[key.*?\\]", Tree) 
match = unlist(regmatches(Tree, pos)) 

match = gsub("^\\[(key.*?)\\]$", "\\1", match) # remove square brackets 
match = match[!grepl("bg", match)] # removes matches containing bg 

# Loop through image list. Change the legend keys to images 
for(i in 1:2){ 
downViewport(match[i]) 
grid.rect(gp=gpar(col = NA, fill = "white")) 
grid.raster(image_list[i], interpolate=FALSE) 
upViewport(0) 
} 

Ben bir liste halinde görüntüleri koyarak listeye çift gelen türünü değiştirir fark

Error in UseMethod("as.raster") : 
no applicable method for 'as.raster' applied to an object of class "list" 

aşağıdaki hatayı olsun bu son döngü çalıştırmak, bu yüzden ne zaman Bunun bununla bir ilgisi olduğunu tahmin ediyorum. Eğer image_list[i] yaptığınızda çünkü image_list subsetting biçimi nedeniyle bu hatayı alıyorsanız

typeof(img) 
[1] "double" 
typeof(image_list[1]) 
[1] "list" 

cevap

1

, sen

dizisi (görüntü) elde etmek için [] yani image_list[[i]] fazladan bir çift ekleyerek bir seviye aşağıya gitmek gerekir
library(png) 
library(ggplot2) 
library(grid) 

# Get image 
img <- readPNG(system.file("img", "Rlogo.png", package="png")) 

# Grab online image 
url <- "https://www.rstudio.com/wp-content/uploads/2014/06/RStudio-Ball.png" 
destfile <- "myfile.png" 
r_studio <- download.file(url, destfile, mode="wb") 
r_studio <- readPNG("myfile.png") 

#Create the object `img`: your example code above does not do this 
img <- r_studio 

# Put images in list  
image_list <- list(img, r_studio) 

# Plot 
p = ggplot(mtcars, aes(mpg, disp, colour = factor(vs))) + 
    geom_point() + 
    theme(legend.key.size = unit(1, "cm")) 

# Get ggplot grob 
gt = ggplotGrob(p) 
grid.newpage() 
grid.draw(gt) 

# Search using regular expressions 
Tree = as.character(current.vpTree()) 
pos = gregexpr("\\[key.*?\\]", Tree) 
match = unlist(regmatches(Tree, pos)) 

match = gsub("^\\[(key.*?)\\]$", "\\1", match) # remove square brackets 
match = match[!grepl("bg", match)] # removes matches containing bg 

# Loop through image list. Change the legend keys to images 
for(i in 1:2){ 
downViewport(match[i]) 
grid.rect(gp=gpar(col = NA, fill = "white")) 
grid.raster(image_list[[i]], interpolate=FALSE) 
upViewport(0) 
}