Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot use small pie charts as points with geom_point

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?

example chart

like image 356
midtiby Avatar asked Jan 08 '13 13:01

midtiby


2 Answers

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

enter image description here

like image 92
Didzis Elferts Avatar answered Oct 06 '22 00:10

Didzis Elferts


Using a plot as the shape for a point is tricky. However, you can side step the issue and get very close to your mockup using 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)

enter image description here

like image 41
orizon Avatar answered Oct 05 '22 22:10

orizon