R - ggplot中奇怪的饼图行为 [英] R - Strange pie chart behavior in ggplot
问题描述
我试图从一个小数据框中生成一个饼图。首先,一切运行良好
library(ggplot2)
library(data.table)
$ b $ (c2,levels = c(first,c) - c(2,3)
c2 < - c(second,third)
c2 < - c第二,第三,第四))
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秒0.7
2:3第三个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(休息=中点,标签= ct $ (限额= c(-1,2.5))
vysg
当我需要添加新(data.table)(第一个和第四个结果为零)
ctlab < - data.table(水平(c2))
nlabs < - ctlab [!V1%in%ct $ c2]
nlabs [,V1:=因子(V1,levels = c(first,second (c1 = c(1,4),c2 = nlabs [,V1],c3 = 0)
ct< (ct,nct)
colx <-c(绿色,蓝色,红色,棕色)
ct $ c2 < - 因子(ct $ c2, = c(第一,第二,第三,第四))
ct $ c4 < - as.character(ct $ c2)
keycols = c(c1)
setkeyv(ct,keycols)
ct
c1 c2 c3 c4
1:1先0.0首先
2:2秒0。 7秒
3:3第三个0.3第三个
4:4第四个0.0第四个
data.table看起来不错,但图表不是
中点< - 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
警告信息:
在`[[[< -.factor`('* tmp *`,n,value =first / fourth):
无效因子水平,NA生成
用标签中的c4(字符串)替换c2后,警告不出现,但图表不是OK
pre $ code $中点点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')+
主题(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 =中点,labels = ct $ c4)+
scale_fill_manual(values = colx)+
scale_x_continuous(limits = c(-1,2.5))
vysg
我猜问题隐藏在因子(c2)中但不能fin d如何修改它。我明确设置了旧的数据框架和新的数据框架。
你的问题是0°和360° °是相同的角度,ggplot2知道这一点。你会看到,如果你只绘制了这个:
ggplot(ct,aes(x = 1,y = c3,fill = c2))+
geom_bar(stat =identity,width = 2)+
coord_polar(theta ='y')
因此,您需要为标签做一些准备:
中点[中点== 1] < - 0
labs < - 集合(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 =实验室$ Group.1,labels =实验室$ x)+
scale_fill_manual( values = colx)+
scale_x_continuous(limits = c(-1,2.5))
vysg
标尺标签中的不同颜色是不可能的(无需在网格图形级别进行编辑)。也许 scale_fill_manual(values = colx [as.integer(factor(midpoint))])
会满足您的需求。
最后,强制性建议:通常比饼图更好地说明这些数据。
I am trying to produce a pie chart from a small data frame. At first everything worked well
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
The problems start when I need to add new rows to the dataframe(data.table) (the zero results for first and fourth)
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
The data.table looks ok but the chart is not
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
After replacing c2 by c4 (string) in labels the warning does not appear but the chart is not ok
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
I guess the problem is hidden in the factor (c2) but cannot find a way how to amend it. I explicitly set levels in both - the old data.frame and the new one.
Your problem is that 0° and 360° are the same angle and ggplot2 knows this. You'd see that if you only plotted this:
ggplot(ct, aes(x=1, y=c3, fill=c2)) +
geom_bar(stat="identity",width=2) +
coord_polar(theta='y')
Thus, you need to do some preparation for the labels:
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
Different colors in the scale label are not possible (without editing at the grid graphics level). Maybe scale_fill_manual(values=colx[as.integer(factor(midpoint))])
would suit your needs.
Finally, the obligatory advice: There is usually a much better option than a pie chart to illustrate such data.
这篇关于R - ggplot中奇怪的饼图行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!