I would like to plot pie charts in two dimensions to show the composition of each point in terms of their composite "groups."
So far, I'm using label repel to label to highest scoring points but it is still not great. I've looked around and I haven't seen what I'm looking for.
ggplot(data=aggtmp2,aes(x=cluster,y=x,color=groups,shape=dataset)) +
geom_jitter() + facet_grid(datasubset~.) +
geom_text_repel(data=aggtmp2[aggtmp2$xnorm>.925,],aes(label=groups),size=2)
> str(aggtmp2)
'data.frame': 562 obs. of 7 variables:
$ group_name: chr "1_1_D1NF_lewisnegative" "1_1_D1NF_lewisnegative" "1_1_D1NF_lewisnegative" "1_1_D1NF_lewisnegative" ...
$ cluster : Factor w/ 39 levels "10of10","1of1",..: 30 24 11 18 25 18 30 11 25 24 ...
$ x : num 0.591 0.591 0.591 0.591 0.591 ...
$ xnorm : num 0.921 0.921 0.921 0.921 0.921 ...
$ groups : Factor w/ 43 levels "1_1","1_2","1_3",..: 1 1 1 1 1 2 2 2 2 2 ...
$ dataset : Factor w/ 2 levels "D1NF","D2NF": 1 1 1 1 1 1 1 1 1 1 ...
$ datasubset: Factor w/ 5 levels "all","lewisnegative",..: 2 2 2 2 2 2 2 2 2 2 ...
This answer comes close: ggplot use small pie charts as points with geom_point But I'm trying to get this done without facet_grid(). That way I can more naturally show composition in the xy-coordinate space I've set up.
Update:
If you want pie charts in particular, you're better off with package
scatterpie
, https://cran.r-project.org/web/packages/scatterpie/vignettes/scatterpie.html. My method below works with non-pie charts as well though.I was curious to see if it could be done, and I'm not sure how flexible this solution is, but here's what I came up with. It's worth stepping through this code chunk line by line, stopping before each
%>%
pipe to see what it's generating as it goes along. This first chunk generates some data: 5 random X and Y values. Then component labels and their values are generated and bound to the Xs and Ys. Then for proof-of-concept I created an additional column that shows the sum of the components for each X-Y pair.This chunk takes the first dataframe and for each unique
x1
-y1
-total
combination, generates a plain pie chart in a list-column calledsubplots
. Each value in that column is a list ofggplot
elements to generate a figure. Then it constructs another column that turns each of those subplots into a custom annotation in a column calledsubgrobs
. These annotations are what can be dropped into a larger figure.Here it just takes the 5 unique
x1
-y1
-total
combinations and plots them as a regularggplot
, but then also adds in the custom annotations we made, which are sized proportional tototal
. Then a fake legend is constructed from the original dataframedf
using blankgeom_col
s.A lot of the numeric constants for adjusting the sizes and
x
,y
scales would need to be changed by eye to fit your dataset.