我想使用视口绘制由多个grob对象组合而成的图片。像这样一个:
#' Draw pie and additional graphics in viewports
#'
#' @return
#' @export
#'
#' @examples
#' ret<-draw_pie_with_graphics()
#' ret
draw_pie_with_graphics<-function(){
pie_df<-read_csv('
n,percent,rank,lab.ypos,lbl,color
33780,0.785,2,0.353,33.8k 78.5%,#000033
9240,0.215,1,0.882,#E9540D') %>%
mutate(rank=as.character(rank))
p_pie<-ggplot(pie_df,aes(x = "",y = percent,fill = rank)) +
geom_bar(width = 1,stat = "identity",color = "white") +
coord_polar("y",start = 3.14)+
geom_text(aes(y = lab.ypos,label = lbl),color = "white",fontface=2)+
scale_fill_manual(values = df_pie_short$color %>% rev,name="",labels=df_pie_short$lbl %>% rev) +
theme_void()+
theme(text = element_text(size = font_size),legend.position = "none")
grid.draw(p_pie)
vp <- viewport(x = 0.1,y = 1,width = 0.2,height = 0.4,just = c("left","top"))
pushViewport(vp)
# grid.draw(rectGrob())
g<-grid.lines(x = unit(c(1,.2,.2),"npc"),y = unit(c(-.50,-.50,.71),default.units = "npc",arrow =arrow(ends="last",type="closed"),name = NULL,gp=gpar(color=EXECUTIVE_SUMMARY_MACRO_RESULT_COLORS$Reply),draw = TRUE,vp = viewport(x = 0.2,y = 0.65,width = 0.9,height = 0.5,"bottom"))
)
grid.draw(g)
popViewport()
}
但是预期的结果是TableGrob
个对象,我可以grid.arrange
来绘制更复杂的图片:
> res
TableGrob (2 x 1) "arrange": 2 grobs
z cells name grob
1 1 (1-1,1-1) arrange gtable[arrange]
2 2 (2-2,1-1) arrange gtable[arrange]
我知道我不应该在这种情况下使用grid.draw
,但是如何为现有图形对象绘制视口?
我不能使用annotate
,因为我需要对图形进行更多控制。