gpt4 book ai didi

R - ggplot 中奇怪的饼图行为

转载 作者:行者123 更新时间:2023-12-02 09:23:50 26 4
gpt4 key购买 nike

我正在尝试从一个小数据框中生成一个饼图。起初一切都很顺利

library(ggplot2)
library(data.table)

c1 <- c(2,3)
c2 <- c("second","third")
c2 <- factor(c2, levels = c("first","second","third","fourth"))
c3 <- c(0.7,0.3)
cs <- data.frame(c1,c2,c3)
ct <- data.table(cs)
colx <- c("blue","red")
midpoint <- cumsum(ct$c3) - ct$c3/2

keycols = c("c1")
setkeyv(ct,keycols)
ct



c1 c2 c3
1: 2 second 0.7
2: 3 third 0.3

vysg <- ggplot(ct, aes(x=1,y=c3,fill=c2)) +
geom_bar(stat="identity",width=2) +
coord_polar(theta='y')+
theme(axis.ticks=element_blank(), axis.title=element_blank(),
axis.text.y = element_blank(), panel.grid = element_blank(),
axis.text.x = element_text(color=colx,size=15,hjust=0))+

scale_y_continuous(breaks = midpoint, labels = ct$c2) +
scale_fill_manual(values=colx) +
scale_x_continuous(limits=c(-1,2.5))
vysg

enter image description here

当我需要向数据框(data.table)添加新行(第一和第四的零结果)时,问题就开始了

ctlab <- data.table(levels(c2))
nlabs <- ctlab[!V1%in%ct$c2]
nlabs[, V1 := factor(V1,levels=c("first","second","third","fourth"))]
nct <- data.frame(c1=c(1,4),c2=nlabs[,V1],c3=0)
ct <- rbind(ct,nct)
colx <- c("green","blue","red","brown")
ct$c2 <- factor(ct$c2,levels=c("first","second","third","fourth"))
ct$c4 <- as.character(ct$c2)
keycols = c("c1")
setkeyv(ct, keycols)
ct

c1 c2 c3 c4
1: 1 first 0.0 first
2: 2 second 0.7 second
3: 3 third 0.3 third
4: 4 fourth 0.0 fourth

数据表看起来不错,但图表却不然

midpoint <- cumsum(ct$c3) - ct$c3/2
vysg <- ggplot(ct, aes(x=1,y=c3,fill=c2)) +
geom_bar(stat="identity",width=2) +
coord_polar(theta='y') +
theme(axis.ticks=element_blank(), axis.title = element_blank(),
axis.text.y = element_blank(), panel.grid = element_blank(),
axis.text.x=element_text(color=colx,size=15,hjust=0)) +
scale_y_continuous(breaks = midpoint, labels = ct$c2) +
scale_fill_manual(values = colx) +
scale_x_continuous(limits = c(-1,2.5))
vysg

Warning message:
In `[[<-.factor`(`*tmp*`, n, value = "first/fourth") :
invalid factor level, NA generated

enter image description here

在标签中用 c4(字符串)替换 c2 后,警告不会出现,但图表不正常

midpoint <- cumsum(ct$c3) - ct$c3/2
vysg <- ggplot(ct, aes(x=1,y=c3,fill=c2)) +
geom_bar(stat="identity",width=2) +
coord_polar(theta = 'y') +
theme(axis.ticks=element_blank(), axis.title=element_blank(),
axis.text.y = element_blank(), panel.grid = element_blank(),
axis.text.x = element_text(color=colx,size=15,hjust=0)) +
scale_y_continuous(breaks = midpoint, labels = ct$c4) +
scale_fill_manual(values=colx) +
scale_x_continuous(limits=c(-1,2.5))
vysg

enter image description here

我猜问题隐藏在因素(c2)中,但找不到修改它的方法。我在旧的 data.frame 和新的 data.frame 中明确设置了级别。

最佳答案

你的问题是 0° 和 360° 是相同的角度,并且 ggplot2 知道这一点。如果你只绘制这个,你就会看到:

ggplot(ct, aes(x=1, y=c3, fill=c2)) + 
geom_bar(stat="identity",width=2) +
coord_polar(theta='y')

因此,您需要为标签做一些准备:

midpoint[midpoint == 1] <- 0
labs <- aggregate(ct$c2, list(midpoint), FUN = function(x) paste(x, collapse = "/"))
vysg<-ggplot(ct, aes(x=1, y=c3, fill=c2)) +
geom_bar(stat="identity",width=2) +
coord_polar(theta='y')+
theme(axis.ticks=element_blank(), axis.title=element_blank(),
axis.text.y=element_blank(), panel.grid = element_blank(),
axis.text.x=element_text(color=colx,size=15,hjust=0))+
scale_y_continuous(breaks=labs$Group.1, labels=labs$x)+
scale_fill_manual(values=colx)+
scale_x_continuous(limits=c(-1,2.5))
vysg

resulting plot

刻度标签中不可能有不同的颜色(无需在网格图形级别进行编辑)。也许 scale_fill_manual(values=colx[as.integer(factor(midpoint))]) 会满足您的需求。

最后,强制性建议:通常有比饼图更好的选择来说明此类数据。

关于R - ggplot 中奇怪的饼图行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39514272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com