Creating plot with geom_scatterpie and geom_scatterpie_legend with normalised values

Viewed 2418

I am trying to create a scatterpie plot with the scatterpie package in R. My data looks something like this

  EEE Innovation n equal negative positive     n_mod
   0          0 2    NA        2       NA 0.3162278
   0          1 6     4        2       NA 0.5477226
   0          2 1    NA        1       NA 0.2236068
   0          3 2    NA        2       NA 0.3162278
   0          5 1     1       NA       NA 0.2236068
   1          0 4     2        1        1 0.4472136
   1          1 14     4        5       5 0.5916080
   1          2  9     3        2       4 0.4743416
   1          3  1    NA        1      NA 0.1581139
   1          5  1    NA        1      NA 0.1581139
   2          1  3    NA        2       1 0.2738613
   3          0  1    NA        1      NA 0.1581139
   3          1  3     1        2      NA 0.2738613
   3          2  4    NA        2       2 0.3162278
   4          0  3     2        1      NA 0.2738613
   4          1 14     5        3       6 0.5916080
   4          2 14     4       NA      10 0.5916080

For creating my plot I use this command:

ggplot() + 
   geom_scatterpie(aes(x=EEE,y=Innovation, r = n_mod), data=pie_data, 
                   cols=c("equal","negative","positive")) +
   geom_scatterpie_legend((all_pie_data$n_mod), n=7, 
                          labeller= function(x) x=sort(unique(pie_data$n)))

I use n_mod which I got with

for (l in 1:17) {
all_pie_data$n_mod[l] <- sqrt(all_pie_data$n[l]/40)
}

instead of n as radius because the radii of the pies would be too large for my graph and smaller pies would be buried under the larger ones. For the legend I want to have the radii of the n_mod, but with the label of the "real" n values. When i try to create this plot I get the following error message:

Error in $<-.data.frame(*tmp*, "label", value = c(1L, 2L, 3L, 4L,  : 
replacement has 7 rows, data has 5

This error does not show up if I use anything lower than 24 in my n_mod creation:

for (l in 1:17) {
all_pie_data$n_mod[l] <- sqrt(all_pie_data$n[l]/24)
}

The pies generated by this are still to large for my graphs:

enter image description here

Does anyone have an idea how I can solve this problem or another way to create smaller pies?

P.S: This is my first question here, if I did something wrong with the formatting or any information is missing I am willing to improve!

1 Answers

You could set "r" to:

r = n_mod/2

This should make them look smaller.

ggplot() + 
geom_scatterpie(aes(x=EEE,y=Innovation, r = n_mod/2), data=pie_data, 
               cols=c("equal","negative","positive")) +
geom_scatterpie_legend((all_pie_data$n_mod), n=7, 
                      labeller= function(x) x=sort(unique(pie_data$n)))
Related