在 R 中绘制 Pie3D 图
Drawing Pie3D Graph in R
我在饼图中用 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% .我有办法做到这一点吗?
图表如下所示:
将小于阈值的归为一类,然后作图:
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)")
我在饼图中用 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% .我有办法做到这一点吗?
图表如下所示:
将小于阈值的归为一类,然后作图:
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)")