柱状图加自定义图.png忘了今天在找什么答案时候,无意中看到下面这个图,而且用R语言还有好几种方法画法,于是我就搬过来了。
具体的方法如下:
- 第一种方法需要了解的函数
require(cowplot) ## 首先加载包
# create the data
set.seed(123) ## 为了方便别人能重复数据
myd <- expand.grid('cat' = LETTERS[1:5], 'cond'= c(F,T), 'phase' = c("Interphase", "Prophase", "Metaphase", "Anaphase", "Telophase")) ## 创建数据框,这个命令简而言之的意思是生成一个5*2*5 = 50行的数据,即这三组数据的排列组合。
myd$value <- floor((rnorm(nrow(myd)))*100) ## floor 表示取整,返回不大于该数的最大整数,与之相对的是ceiling,返回不小于该数值的最小整数
myd$value[myd$value < 0] <- 0 ## 将value列中小于0的数值都赋值为0
# make the barplot
pbar <- ggplot(myd) +
geom_bar(aes(y = value, x = phase, fill = cat), stat="identity", position='dodge') +
scale_y_continuous(limits = c(0, 224), expand = c(0, 0)) +
theme_minimal(14) +
theme(axis.ticks.length = unit(0, "in")) ## 就是简单的画一个柱状图
# make the image strip
pimage <- axis_canvas(pbar, axis = 'x') +
draw_image("http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_interphase.jpg", x = 0.5, scale = 0.9) +
draw_image("http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_prophase.jpg", x = 1.5, scale = 0.9) +
draw_image("http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_metaphase2.jpg", x = 2.5, scale = 0.9) +
draw_image("http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_anaphase2.jpg", x = 3.5, scale = 0.9) +
draw_image("http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_telophase.jpg", x = 4.5, scale = 0.9)
# insert the image strip into the bar plot and draw
ggdraw(insert_xaxis_grob(pbar, pimage, position = "bottom")) ## ggdraw()可以将图形置于特定位置,
网友评论