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())
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With