其他
使用ggimage添加图片、图标、国旗等
使用ggimage
包即可实现。该包也是由y叔开发,除了添加图片,还有很多其他好玩的功能。
geom_image
geom_pokemon
geom_emoji
geom_flag
geom_icon
geom_subview
geom_twitchemote
geom_image
添加了一个图层,可以直接用图片
library("ggplot2")
library("ggimage")
set.seed(111)
img <- list.files(system.file("extdata", package="ggimage"),
pattern="png", full.names=TRUE)
d <- data.frame(x = rnorm(10),
y = rnorm(10),
image = sample(img, size=10, replace = TRUE)
)
ggplot(d, aes(x, y)) + geom_image(aes(image=image), size=.05)
ggplot(d, aes(x, y)) + geom_image(aes(image=image), size=.05, by='height')
可以取子集,指定图片:
ggplot(d, aes(x, y)) + geom_image(image=d$image[1])
改变大小:
d$size <- seq(.05, .15, length.out=10)
ggplot(d, aes(x, y)) + geom_image(aes(image=image, size=I(size)))
改变颜色:
ggplot(d, aes(x, y)) + geom_image(aes(image=image), color="firebrick")
geom_pokemon
画皮卡丘(需要科学上网):
ggplot(d, aes(x, y)) + geom_pokemon(aes(image=ifelse(x>0, 'pikachu', 'tauros')), size=.1)
geom_emoji
画表情:
set.seed(123)
iris2 <- iris[sample(1:nrow(iris), 30),]
model <- lm(Petal.Length ~ Sepal.Length, data=iris2)
iris2$fitted <- predict(model)
ggplot(iris2, aes(x = Sepal.Length, y = Petal.Length)) +
geom_linerange(aes(ymin = fitted, ymax = Petal.Length),
colour = "purple") +
geom_abline(intercept = model$coefficients[1],
slope = model$coefficients[2]) +
geom_emoji(aes(image = ifelse(abs(Petal.Length-fitted) > 0.5, '1f622', '1f600')))
geom_flag
画国旗,网络原因搞不定。。。
f <- system.file("extdata/medals.txt", package="ggimage")
medals <- read.table(f, header=TRUE)
p <- ggplot(medals, aes(Country, count)) + geom_col(aes(fill = medal), width = .8)
p + geom_flag(y = -2, aes(image = code)) +
coord_flip() + expand_limits(y = -2) +
scale_fill_manual(values = c("Gold" = "gold", "Bronze" = "#cd7f32", "Silver" = "#C0C0C0"))
geom_icon
画图标,网络原因搞不定。。
d$icon <- sample(c('ios-power', 'ios-wifi', 'ios-pie'), 10, replace=TRUE)
ggplot(d, aes(x,y)) + geom_icon(aes(image=icon))
geom_subview
把饼图添加在图形中,当然也可以嵌套其他图形,我之前介绍过这个功能,很好用!
library(tibble)
dd <- data.frame(x=LETTERS[1:3], y=1:3)
pie <- ggplot(dd, aes(x=1, y, fill=x)) + geom_bar(stat="identity", width=1) + coord_polar(theta="y") +
theme_void() + theme(legend.position="none") + theme_transparent()
df <- tibble(x = sample(2:9),
y = sample(2:9),
width = sample(seq(0.5, 3, length.out=length(x))),
pie = list(pie))
p <- ggplot(data=data.frame(x=c(0, 10), y=c(0, 10)), aes(x, y))+geom_blank()
p + geom_subview(aes(x=x, y=y, subview=pie, width=width, height=width), data=df)
geom_twitchemote
失效了,暂时不能用。
set.seed(1)
x <- 1:10
y <- x + rnorm(10, sd = 1)
notlikethis <- data.frame(x = x, y = y)
n_pals <- 200
pals <- data.frame(
x = runif(n_pals, -2, 12), y = runif(n_pals, -2, 12),
pal = sample(c("wutface", "kappa", "pogchamp"), size = n_pals, replace = TRUE)
)
ggplot(notlikethis, aes(x = x, y = y)) +
geom_twitchemote(data = pals,
aes(image = 'pogchamp'), size = 0.03, alpha = 0.3) +
geom_twitchemote(aes(image = 'notlikethis'), size = 0.15) +
geom_smooth()
以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发!
欢迎在评论区留言或直接添加我的微信!
欢迎关注公众号:医学和生信笔记
“医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!
往期回顾
2022-03-11
2022-02-28
2022-02-27
2022-02-22
2022-02-19