我当前正在尝试创建一个具有徽标映射到X和Y坐标的图。但是,目标是拥有另一组具有相同徽标的点,但是具有一个alpha值,以使徽标更加透明。
这是一个可复制的示例。目的是使左侧的徽标透明。
library(ggimage)
library(tidyverse)
library(RCurl)
Sample_X1 <- c(1,0,2,3,1)
Sample_X2 <- c(4,5,7,4,3)
Sample_Y <- c('A','B','C','D','E')
Logo <- 'https://i1.pngguru.com/preview/233/348/954/numix-circle-for-windows-rstudio-icon-png-icon-thumbnail.jpg'
data <- data.frame(cbind(Sample_X1, Sample_X2, Sample_Y, Logo))
ggplot(data = data) +
geom_segment(aes(x = Sample_X1, xend = Sample_X2, y = Sample_Y, yend = Sample_Y), size = 1) +
geom_image(aes(x = Sample_X1, y = Sample_Y, image = Logo), size = .05) +
geom_image(aes(x = Sample_X2, y = Sample_Y, image = Logo), size = .05) +
theme_minimal() +
labs(title = 'Example for Stack Overflow')
有没有一种方法可以用ggplot做到这一点?到目前为止,我仅能找到使背景徽标透明的方法,但无法将它们映射到XY数据。
感谢您的帮助!
答案 0 :(得分:1)
image_fun
参数可用于定义应如何处理图像。 image_fx
可用于设置Alpha通道。
library(ggplot2)
transparent <- function(img) {
magick::image_fx(img, expression = "0.5*a", channel = "alpha")
}
ggplot(data.frame(x = runif(10), y = runif(10)), aes(x, y)) +
ggimage::geom_image(
image = system.file("extdata/Rlogo.png", package="ggimage"),
image_fun = transparent,
size = 0.3
)