1

I'm trying to create treemaps with rounded corners, like this one 1.

I'm using the treemapify package.

I've tried using Claus Wilke solution for changing the background rectangle with a rounded rectangle found here: 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()

Then I try to switch the rect grob with a roundrectGrob

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)

Gives me the error:

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

The problem I think is that the roundrectGrob() function "Draw a single rectangle with rounded corners.", while here we have a list of rectangles.

How can I swap one single rectGrob with a list of roundrectGrob?

In fact if I pick only the one element, it works fine ( 2 ) but of course plots just the first rectangle:

rounded_rects <- roundrectGrob(x=rects$x[[1]], y=rects$y[[1]], width=rects$width[[1]], height=rects$height[[1]],
                r=unit(0.1, "snpc"),
                just=rects$just, name=rects$name, gp=rects$gp, vp=rects$vp)
gg$grobs[[6]]$children[[3]] <- rounded_rects
cowplot::plot_grid(gg)

I tried making a list of roundrects, but it won't let me substitute a list of roundrects to one rect element

rounded_rects <- lapply(1:n, function(i) roundrectGrob(x=rects$x[[i]], y=rects$y[[i]], width=rects$width[[i]], height=rects$height[[i]],
                                                       r=unit(0.1, "snpc"),
                                                       just=rects$just, name=rects$name, gp=rects$gp, vp=rects$vp))

Thank you for your help!

0 Answers0