分享一个用纹理填充柱状图的小例子,喜欢的小伙伴可以关注个人公众号R语言数据分析指南,持续分享更多优质资源
devtools::install_github("clauswilke/ggtextures")
library(ggtextures)
library(grid)
library(magick)
library(tidyverse)
df <- tibble(
trt = c("a", "b", "c"),
outcome = c(2.3, 1.9, 3.2),
image = c(
"http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks2-256.jpg",
"http://www.hypergridbusiness.com/wp-content/uploads/2012/12/stone2-256.jpg",
"http://www.hypergridbusiness.com/wp-content/uploads/2012/12/siding1-256.jpg"))
ggplot(df, aes(trt, outcome, image = image)) +
geom_textured_col(img_width = unit(0.3, "null"))+
theme_bw()+
theme_minimal()+
theme(axis.title.x=element_blank(),
axis.ticks.y=element_blank(),
axis.ticks.x=element_blank(),
legend.title=element_blank())
scale_y_continuous(expand=c(0,0))
images = c(
compact = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks2-256.jpg",
midsize = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/stone2-256.jpg",
suv = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/siding1-256.jpg",
`2seater` = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/mulch1-256.jpg",
minivan = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks1-256.jpg",
pickup = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/wood3-256.jpg",
subcompact = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/concrete1-256.jpg"
)
ggplot(mpg, aes(class, image = class)) +
geom_textured_bar() +
scale_image_manual(values = images)+
theme_minimal()+
theme(axis.title.x=element_blank(),
axis.ticks.y=element_blank(),
axis.ticks.x=element_blank(),
legend.title=element_blank())+
scale_y_continuous(expand=c(0,0))
ggplot(mpg, aes(factor(trans), image = class)) +
geom_textured_bar() +
scale_image_manual(values = images)+
theme_minimal()+
theme(axis.title.x=element_blank(),
axis.ticks.y=element_blank(),
axis.ticks.x=element_blank(),
legend.title=element_blank(),
axis.text.x =element_text(angle =90,
hjust =0.5,vjust = 0.5))+
scale_y_continuous(expand=c(0,0))
网友评论