mid*_*iby 11 r data-visualization ggplot2 pie-chart
我想用ggplot制作一个图,如下所示.这个想法是在两个分类变量之间绘制"百分比匹配".通过改变点的大小很容易接近,但我想知道是否有可能制作这些小饼图......
一个示例代码,用于绘制此点,其中点的大小作为分数的度量.
temp <- data.frame(Exercise=c(1, 1, 1, 2, 2, 2),
Name=c(1, 2, 3, 1, 2, 3), Score=c(0.2, 0.5, 0.3, 0.9, 1.0, 0.6))
ggplot(temp) + geom_point(aes(Exercise, Name, size=Score))
Run Code Online (Sandbox Code Playgroud)
如何更改此代码以提供接近下图的内容?

使用绘图作为点的形状是棘手的.但是,您可以使用facet_grid()来解决问题并使其非常接近您的模型:
ggplot(temp) +
geom_bar(aes(x=1, y=Score), stat="identity") +
facet_grid(Exercise~Name) +
coord_polar(theta = "y") +
scale_y_continuous(breaks = NULL) +
scale_x_continuous(name = element_blank(), breaks = NULL)
Run Code Online (Sandbox Code Playgroud)

首先,修改原始数据框,使前6行包含原始数据score,最后6行包含1减去原始数据score.然后添加group包含这两个组的级别的列.
temp <- data.frame(Exercise=c(1, 1, 1, 2, 2, 2),
Name=c(1, 2, 3, 1, 2, 3), Score=c(0.2, 0.5, 0.3, 0.9, 1.0, 0.6))
temp<-rbind(temp,temp)
temp$Score[7:12]<-1-temp$Score[1:6]
temp$group<-rep(c("poz","neg"),each=6)
Run Code Online (Sandbox Code Playgroud)
coord_polar()用于从barplot制作饼图,然后facet_grid()制作六个小块图.theme()用于删除轴,刻面标签,网格线.
ggplot(temp,aes(x = factor(1),y=Score,fill=group)) +
geom_bar(width = 1, stat = "identity") + facet_grid(Exercise~Name)+
coord_polar(theta = "y") +
scale_fill_manual(values = c("black", "grey")) +
theme_bw() + scale_x_discrete("",breaks=NULL) + scale_y_continuous("",breaks=NULL)+
theme(panel.border=element_blank(),
strip.text=element_blank(),
strip.background=element_blank(),
legend.position="none",
panel.grid=element_blank())
Run Code Online (Sandbox Code Playgroud)
