Dưới đây là một Geom tối giản để hiển thị hình ảnh raster thay vì điểm,
library(ggplot2)
library(grid)
## replace by a named list with matrices to be displayed
## by rasterGrob
.flaglist <- list("ar" = matrix(c("blue", "white", "blue"), 1),
"fr" = matrix(c("blue", "white", "red"), 1))
flagGrob <- function(x, y, country, size=1, alpha=1){
grob(x=x, y=y, country=country, size=size, cl = "flag")
}
drawDetails.flag <- function(x, recording=FALSE){
for(ii in seq_along(x$country)){
grid.raster(x$x[ii], x$y[ii],
width = x$size[ii]*unit(1,"mm"), height = x$size[ii]*unit(0.5,"mm"),
image = .flaglist[[x$country[[ii]]]], interpolate=FALSE)
}
}
scale_country <- function(..., guide = "legend") {
sc <- discrete_scale("country", "identity", scales::identity_pal(), ..., guide = guide)
sc$super <- ScaleDiscreteIdentity
class(sc) <- class(ScaleDiscreteIdentity)
sc
}
GeomFlag <- ggproto("GeomFlag", Geom,
required_aes = c("x", "y", "country"),
default_aes = aes(size = 5, country="fr"),
draw_key = function (data, params, size)
{
flagGrob(0.5,0.5, country=data$country, size=data$size)
},
draw_group = function(data, panel_scales, coord) {
coords <- coord$transform(data, panel_scales)
flagGrob(coords$x, coords$y, coords$country, coords$size)
}
)
geom_flag <- function(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...) {
layer(
geom = GeomFlag, mapping = mapping, data = data, stat = stat,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, ...)
)
}
set.seed(1234)
d <- data.frame(x=rnorm(10), y=rnorm(10),
country=sample(c("ar","fr"), 10, TRUE),
stringsAsFactors = FALSE)
ggplot(d, aes(x=x, y=y, country=country, size=x)) +
geom_flag() +
scale_country()
(đầu ra từ gói ggflags)
Cảm ơn bạn đã trả lời! Tôi đã xóa liên kết đó vì tôi nghĩ rằng bạn đã đúng. Nói chung, trừ khi tôi đang thiếu một cái gì đó, nó không có vẻ giống như hàm faces(), và stat_spoke thực sự giải quyết nhu cầu của tôi. faces() chỉ hiển thị khuôn mặt ... bạn không thể thay đổi hình ảnh. Và stat_spoke không cho phép bạn thay đổi hình ảnh. Nhưng grImport trông rất hứa hẹn! Cảm ơn! – griffin
Các liên kết hình ảnh của bạn bị hỏng ... Tôi chọc xung quanh một chút để sửa chúng nhưng không thành công. – joran
Có vẻ như câu hỏi đã được chỉnh sửa ở mức độ mà câu trả lời không còn ý nghĩa; cũng như có liên kết bị hỏng với hình ảnh. –