我的数据集中的值之一为零,我认为因此无法正确调整饼图中的标签。
#Providing you all a sample dataset
Averages <- data.frame(Parameters = c("Cars","Motorbike","Bicycle","Airplane","Ships"), Values = c(15.00,2.81,50.84,51.86,0.00))
mycols <- c("#0073C2FF", "#EFC000FF", "#868686FF", "#CD534CFF","#FF9999")
duty_cycle_pie <- Averages %>% ggplot(aes(x = "", y = Values, fill = Parameters)) +
geom_bar(width = 1, stat = "identity", color = "white") +
coord_polar("y", start = 0)+
geom_text(aes(y = cumsum(Values) - 0.7*Values,label = round(Values*100/sum(Values),2)), color = "white")+
scale_fill_manual(values = mycols)
标签放置不正确。请告诉我如何获得3D饼图。
答案 0 :(得分:1)
欢迎来到stackoverflow。我很乐意为您提供帮助,但是,我必须指出,饼图具有很高的争议性,而3D饼图被认为是不良做法。
https://www.darkhorseanalytics.com/blog/salvaging-the-pie https://en.wikipedia.org/wiki/Misleading_graph#3D_Pie_chart_slice_perspective
此外,如果变量的名称反映了您的实际数据集(Averages
),则不适合使用饼图,因为这些部分似乎并未描述整体的一部分。例如:Bicycle的平均值为50.84,Airplane的平均值为51.86。这些结果分别占43%和42%令人困惑;条形图会更容易理解。
尽管如此,您关于布局的问题的答案可以通过position_stack()
来解决。
library(tidyverse)
Averages <-
data.frame(
Parameters = c("Cars","Motorbike","Bicycle","Airplane","Ships"),
Values = c(15.00,2.81,50.84,51.86,0.00)
) %>%
mutate(
# this will ensure the slices go biggest to smallest (a best practice)
Parameters = fct_reorder(Parameters, Values),
label = round(Values/sum(Values) * 100, 2)
)
mycols <- c("#0073C2FF", "#EFC000FF", "#868686FF", "#CD534CFF","#FF9999")
Averages %>%
ggplot(aes(x = "", y = Values, fill = Parameters)) +
geom_bar(width = 1, stat = "identity", color = "white") +
coord_polar("y", start = 0) +
geom_text(
aes(y = Values, label = label),
color = "black",
position = position_stack(vjust = 0.5)
) +
scale_fill_manual(values = mycols)
要将棋子移到饼图的外部,您可以查看ggrepel
https://stackoverflow.com/a/44438500/4650934
就我之前的观点而言,我可以尝试使用类似的方法代替饼图:
ggplot(Averages, aes(Parameters, Values)) +
geom_col(aes(y = 100), fill = "grey70") +
geom_col(fill = "navyblue") +
coord_flip()