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

前端 未结 4 790
滥情空心
滥情空心 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:10

    I think an immediate fix is to create some "empty" entries. I'd create internetImportance data.frame like this:

    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)
    
    internetImportance <- data.frame(Category,Percent)
    
    len <- 4
    df2 <- data.frame(Category = letters[1:len], Percent = rep(0, len), 
                                     Category2 = rep("", len))
    internetImportance$Category2 <- 
     paste0(internetImportance$Category," - ",internetImportance$Percent,"%")
    
    # append number to category name
    internetImportance <- rbind(internetImportance, df2)
    
    # set factor so it will plot in descending order 
    internetImportance$Category <-
        factor(internetImportance$Category, 
        levels=rev(internetImportance$Category))
    

    And then I'd plot ggplot2 with fill=category2 as follows:

    ggplot(internetImportance, aes(x = Category, y = Percent,
        fill = Category2)) + 
        geom_bar(width = 0.9, stat="identity") + 
        coord_polar(theta = "y") +
        xlab("") + ylab("") +
        ylim(c(0,100)) +
        ggtitle("Top Product Categories Influenced by Internet") +
        geom_text(data = internetImportance, hjust = 1, size = 3,
                  aes(x = Category, y = 0, label = Category2)) +
        theme_minimal() +
        theme(legend.position = "none",
              panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              axis.line = element_blank(),
              axis.text.y = element_blank(),
              axis.text.x = element_blank(),
              axis.ticks = element_blank())
    

    This gives me:

    enter image description here

    You can add a geom_text(label="GLOBAL", x=.5, y=.5, size=4) + before theme_minimal to add the text GLOBAL.

提交回复
热议问题