我正在使用以下代码
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}}
更一般而言,我希望增强饼图。我想要一个饼图,每块饼旁边都带有可读标签。
答案 0 :(得分:1)
填充和图例之间的颜色顺序不同,因为您分别定义它们。图例颜色在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
)