ggplot使用小饼图作为geom_point的点 [英] ggplot use small pie charts as points with geom_point

查看:157
本文介绍了ggplot使用小饼图作为geom_point的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用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))

这段代码如何改变,以便接近下图?

解决方案

首先,修改原始数据框,原始得分,最后6行包含1减去原始得分。然后添加包含这两个组别级别的 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)

coord_polar()用于从barplot制作饼图,然后使用 facet_grid()制作6个小图。 theme()用于删除轴,facet标签,网格线。

  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,gray))+
theme_bw()+ scale_x_discrete(,breaks = NULL)+ scale_y_continuous (),break = NULL)+
theme(panel.border = element_blank(),
strip.text = element_blank(),
strip.background = element_blank(),
legend.position =none,
panel.grid = element_blank())


I would like to make a graph with ggplot as shown below. The idea is to plot "percentage matches" between two categorical variables. It is easy to come close by altering the size of points, but I wondered if it is possible to make these small pie charts...

An example code for plotting this with the size of points as a measure of the score.

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))

How can this code be altered to give something close to the figure below?

解决方案

First, modify your original data frame to have first 6 rows containing original score and last 6 rows contains 1 minus original score. Then added column group containing levels for those two groups.

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)

coord_polar() is used to make piecharts from barplot and then facet_grid() to make six small plots. theme() is used to remove axis, facet labels, gridlines.

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())

这篇关于ggplot使用小饼图作为geom_point的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆