Making a circular barplot with a hollow center (aka race track plot)

前端 未结 4 876
滥情空心
滥情空心 2021-01-30 13:22

I was asked to recreate the following style of plot. (Please ignore the question of whether this is a good type of visualization and charitably consider this as adding a colorf

4条回答
  •  逝去的感伤
    2021-01-30 14:02

    Since the plot is circular, it can be easily done by circlize package.

    First the data:

    Category <- c("Electronics", "Appliances", "Books", "Music", "Clothing", 
            "Cars", "Food/Beverages", "Personal Hygiene", 
            "Personal Health/OTC", "Hair Care")
    Percent <- c(81, 77, 70, 69, 69, 68, 62, 62, 61, 60)
    color = rainbow(length(Percent))
    

    Reverse the three vectors since circlize adds each element from inside to outside by default:

    Category = rev(Category)
    Percent = rev(Percent)
    color = rev(color)
    

    If you image the circle is a bent plot region, then it is just adding rectangles, lines and texts.

    library(circlize)
    
    par(mar = c(1, 1, 1, 1))
    circos.par("start.degree" = 90)
    circos.initialize("a", xlim = c(0, 100)) # 'a` just means there is one sector
    circos.trackPlotRegion(ylim = c(0.5, length(Percent)+0.5), track.height = 0.8, 
        bg.border = NA, panel.fun = function(x, y) {
        xlim = get.cell.meta.data("xlim") # in fact, it is c(0, 100)
        for(i in seq_along(Percent)) {
            circos.lines(xlim, c(i, i), col = "#CCCCCC")
            circos.rect(0, i - 0.45, Percent[i], i + 0.45, col = color[i], 
                border = "white")
            circos.text(xlim[2], i, paste0(Category[i], " - ", Percent[i], "%"), 
                adj = c(1, 0.5)) 
        }
    })
    circos.clear()
    
    text(0, 0, "GLOBAL", col = "#CCCCCC")
    

    enter image description here

提交回复
热议问题