R ggplot2面板层中的多个圆角矩形

我正在尝试创建带有圆角的树图,例如1

我正在使用treemapify软件包。

我尝试使用Claus Wilke解决方案来更改背景矩形,并在此处找到一个圆角矩形:Rounded corners in ggplot2?

tm <- ggplot2::ggplot(G20,ggplot2::aes(area = gdp_mil_usd,fill = region)) +
  geom_treemap() # save treemap ggplot
gg <- ggplotGrob(tm) # save as grob
rects <- gg$grobs[[6]]$children[[3]] # grobs[[6]] is the panel,and [[3]] are the rect()

然后我尝试用roundrectGrob切换rect grob

rounded_rects <- roundrectGrob(x=rects$x,y=rects$y,width=rects$width,height=rects$height,r=unit(0.1,"snpc"),just=rects$just,name=rects$name,gp=rects$gp,vp=rects$vp)

给我错误:

Error in validDetails.roundrect(x) : 
  'x','y','width',and 'height' must have length 1

我认为的问题是roundrectGrob()函数“绘制具有圆角的单个矩形。”,而这里我们有一个矩形列表。

如何将一个rectGrob与roundrectGrob列表交换?

实际上,如果我只选择一个元素,它就可以正常工作(2),但是当然只绘制第一个矩形:

rounded_rects <- roundrectGrob(x=rects$x[[1]],y=rects$y[[1]],width=rects$width[[1]],height=rects$height[[1]],vp=rects$vp)
gg$grobs[[6]]$children[[3]] <- rounded_rects
cowplot::plot_grid(gg)

我尝试制作一个Roundrects列表,但是它不允许我将Roundrects列表替换为一个rect元素

rounded_rects <- lapply(1:n,function(i) roundrectGrob(x=rects$x[[i]],y=rects$y[[i]],width=rects$width[[i]],height=rects$height[[i]],vp=rects$vp))

谢谢您的帮助!

a125000178 回答:R ggplot2面板层中的多个圆角矩形

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2331874.html

大家都在问