我在饼图中用R绘制了一个data.frame。这是代码
library(plotrix)
piepercent<- round(100*cause_wise$suicides/sum(cause_wise$suicides), 1)
png(file = "plots/cause suicide.png")
pie3D(cause_wise$suicides,labels = piepercent,explode = 0.1,
main = "Suicide by Gender(in percentages)")
#legend("topright", cause, cex = 0.8, fill = rainbow(length(cause)))
dev.off()
我试图在这里绘制的data.frame有38个值,我想省略那些对piepercent
没有显着贡献的值到一个大区域,比如说小于2%。有没有办法可以做到这一点?
以下是图表的外观:
答案 0 :(得分:1)
将小于阈值的那些聚合成一个类别,然后绘制:
library(plotrix)
library(dplyr)
# dummy data
cause_wise <- data.frame(suicides = c(2, 3, 1, 50, 1, 50, 45))
# sum values where percentage is less than 2%
plotDat <- cause_wise %>%
mutate(grp = ifelse(suicides/sum(suicides) < 0.02, "tooSmall", row_number())) %>%
group_by(grp) %>%
summarise(suicides = sum(suicides)) %>%
select(-grp) %>%
ungroup()
# set label and color(grey for <2%)
piepercent <- round(100 * plotDat$suicides/sum(plotDat$suicides), 1)
piecol <- c(rainbow(length(piepercent) - 1 ), "grey")
# why oh why 3D pie chart...
pie3D(plotDat$suicides,
labels = piepercent,
explode = 0.1,
col = piecol,
main = "Suicide by Gender (in percentages)")