代码之家  ›  专栏  ›  技术社区  ›  PalimPalim

ggplot标签饼图-饼图块旁边-图例不正确

  •  2
  • PalimPalim  · 技术社区  · 6 年前

    我使用以下代码

    education_bachelor_summary<- education_bachelor %>%
      group_by(title_mapped) %>%
      summarise(n=n()) %>%
      mutate(perc = n / sum(n)) %>%
      arrange(desc(n)) %>%
      mutate(label=(str_c(title_mapped, "-", as.character(percent(perc)), sep=" ")))
    
    
    midpoint <-sum(education_bachelor_summary$perc) - cumsum(education_bachelor_summary$perc) + education_bachelor_summary$perc/2
    
    ggplot(education_bachelor_summary, aes(x = "", y=perc, fill = factor(title_mapped))) +
      geom_bar(width = 1, stat = "identity") +
      scale_y_continuous(breaks=midpoint, labels=education_bachelor_summary$label) +
      scale_fill_brewer(palette = "Blues", direction = -1) +
      labs(fill="Bachelor/ Vordiplom",x=NULL,y=NULL,title="",caption="") +
      coord_polar(theta = "y", start=0) +
      theme(axis.ticks = element_blank(), panel.grid  = element_blank(), axis.line = element_blank(), strip.background = element_blank(), panel.background = element_blank())
    

    它产生了下面的情节 enter image description here

    问题

    1. 饼块旁边的标签是 切断 .
    2. 馅饼片上的标签是正确的,但右边的标签混淆了,“Informatik”和“Mathe”被切换了。

    复制示例的代码

    library(tidyverse)
    library(scales)
    education_bachelor_summary <- structure(list(title_mapped = c("BWL","Mathe", "Informatik", "VWL", "Wirtschaftsingenieurwesen"), n = c(82L, 37L, 33L, 10L, 5L), perc = c(0.491017964071856, 0.221556886227545, 0.197604790419162, 0.0598802395209581, 0.029940119760479), label = c("BWL - 49.1%", "Mathe - 2.2%", "Informatik - 19.8%", "VWL - 6.0%", "Wirtschaftsingenieurwesen - 3.0%")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L))
    midpoint <-sum(education_bachelor_summary$perc) - cumsum(education_bachelor_summary$perc) + education_bachelor_summary$perc/2
    
    ggplot(education_bachelor_summary, aes(x = "", y=perc, fill = factor(title_mapped))) +
      geom_bar(width = 1, stat = "identity") +
      scale_y_continuous(breaks=midpoint, labels=education_bachelor_summary$label) +
      scale_fill_brewer(palette = "Blues", direction = -1) +
      labs(fill="Bachelor/ Vordiplom",x=NULL,y=NULL,title="",caption="") +
      coord_polar(theta = "y", start=0) +
      theme(axis.ticks = element_blank(), panel.grid  = element_blank(), axis.line = element_blank(), strip.background = element_blank(), panel.background = element_blank())
    

    更一般地说,我希望增强我的饼图。我想要一张饼图,每张饼旁边都有可读的标签。

    1 回复  |  直到 6 年前
        1
  •  1
  •   P1storius    6 年前

    填充和图例之间的颜色顺序不同,因为是分别定义的。图例颜色在 scale_y_continuous(breaks=midpoint, labels=education_bachelor_summary$label) ,而填充颜色取决于 factor(title_mapped) 把它们放进去。 factor() 如果输入是字符向量,则默认按字母顺序排列。

    要解决此问题,可以为 fill 重视自己,如下所示。

    library(tidyverse)
     library(scales)
     education_bachelor_summary <- structure(list(title_mapped = c("BWL","Mathe", "Informatik", "VWL", "Wirtschaftsingenieurwesen"), n = c(82L, 37L, 33L, 10L, 5L), perc = c(0.491017964071856, 0.221556886227545, 0.197604790419162, 0.0598802395209581, 0.029940119760479), label = c("BWL - 49.1%", "Mathe - 2.2%", "Informatik - 19.8%", "VWL - 6.0%", "Wirtschaftsingenieurwesen - 3.0%")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L))
     midpoint <-sum(education_bachelor_summary$perc) -  cumsum(education_bachelor_summary$perc) + education_bachelor_summary$perc/2
     education_bachelor_summary$fill <- factor(education_bachelor_summary$title_mapped, levels=education_bachelor_summary$title_mapped)
    
     ggplot(education_bachelor_summary, aes(x = "", y=perc, fill = fill)) +
      geom_bar(width = 1, stat = "identity") +
      scale_y_continuous(breaks=midpoint, labels=education_bachelor_summary$label) +
      scale_fill_brewer(palette = "Blues", direction = -1) +
      labs(fill="Bachelor/ Vordiplom",x=NULL,y=NULL,title="",caption="") +
      coord_polar(theta = "y", start=0) +
      theme(axis.ticks = element_blank(), panel.grid  = element_blank(), axis.line = element_blank(), strip.background = element_blank(), panel.background = element_blank())
    

    在我自己的R会话中,标签没有被切断,所以我现在恐怕无法帮助您解决这个问题。你可以试试看 par(mar=c(...)) 用于增加绘图窗口周围的空白(请参见 ?par )