0

I have a simple question to which I can't find a solution

here is my dataset

df <- structure(list(var = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 
                             4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 
                             5), key = structure(c(4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
                                                   6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 7L, 
                                                   7L, 7L, 7L, 7L, 3L, 3L, 3L, 3L, 3L), levels = c("D", "E", "G", 
                                                                                                   "A", "B", "C", "F"), class = "factor"), value = c(71.5862363478496, 
                                                                                                                                                     37.8203087607194, 21.8481932821862, 14.3252994774026, 43.9673840073536, 
                                                                                                                                                     0.970554134149992, 10.2169427632967, 0.482211443593141, 27.1986646339779, 
                                                                                                                                                     0, 7.53079829570694, 2.99274245246759, 0, 1.18270390573516, 36.5445689931181, 
                                                                                                                                                     7.34657218944321, 31.9825027122446, 65.498610956116, 19.4131071594402, 
                                                                                                                                                     3.29473510264364, 0.524263480063698, 1.59031872629643, 0.901030900406403, 
                                                                                                                                                     0.342812345032694, 0.225194597739533, 0.122185299688452, 3.60412401295608, 
                                                                                                                                                     5.07823210446615, 18.5641921443332, 0, 11.9193902530981, 11.7930605720191, 
                                                                                                                                                     6.19172131323217, 18.9732203340783, 15.9681172991452), median = c(0.249672472831354, 
                                                                                                                                                                                                                       0.0119657156700395, -0.581064311371051, 0.167134320110767, 0.0950594757449505, 
                                                                                                                                                                                                                       0.249672472831354, 0.0119657156700395, -0.581064311371051, 0.167134320110767, 
                                                                                                                                                                                                                       0.0950594757449505, 0.249672472831354, 0.0119657156700395, -0.581064311371051, 
                                                                                                                                                                                                                       0.167134320110767, 0.0950594757449505, 0.249672472831354, 0.0119657156700395, 
                                                                                                                                                                                                                       -0.581064311371051, 0.167134320110767, 0.0950594757449505, 0.249672472831354, 
                                                                                                                                                                                                                       0.0119657156700395, -0.581064311371051, 0.167134320110767, 0.0950594757449505, 
                                                                                                                                                                                                                       0.249672472831354, 0.0119657156700395, -0.581064311371051, 0.167134320110767, 
                                                                                                                                                                                                                       0.0950594757449505, 0.249672472831354, 0.0119657156700395, -0.581064311371051, 
                                                                                                                                                                                                                       0.167134320110767, 0.0950594757449505)), row.names = c(NA, -35L
                                                                                                                                                                                                                       ), class = "data.frame")

I want to create a single figure with "var" on the x-axis and "median" on the y-axis, and instead of the geom_point I want to use "value" piecharts. I give you the example codes

# Note that I want the final figure to be reordered
df %>% ggplot(aes(x=reorder(var, median), y=median)) + 
  geom_point()

and


df %>% filter(var == 1) %>%
  ggplot(aes(x="", y=value, fill=key)) + 
  geom_bar(stat="identity", color="white") +
  coord_polar("y", start=0) +
  theme_void() + theme(legend.position="none")

So I would like to have 5 piecharts in my figure at the "median" level (so they will not be at the same level). I imagined creating an empty ggplot and putting the piecharts at x, y coordinates for example (something like that but there may be simpler ways). I would also like this figure to be a unique legend of "key" (or not).

Thank you very much

thomas leon
  • 153
  • 11
  • 1
    This is an interesting question! My only thought is that you may need to create the pie charts and save them to a file then place those on the plot area at the median values. Maybe this post can help: https://stackoverflow.com/questions/9917049/inserting-an-image-to-ggplot2 – dandrews Apr 03 '23 at 02:44
  • 1
    I think @dandrews idea is a good one. Just make sure to increase the resolution when you save the plots so that they don't like blurry in the final composition. The question is somewhat similar to [this](https://stackoverflow.com/a/32380396/5221626) one, and there might be some ideas there to incorporate to create a fully reproducible workflow, but as you can see any such solution would end up being quite hackish. – Phil Apr 03 '23 at 02:48
  • Thanks for your comments. I've been told that my post is duplicated but I can't find the answer in the other posts ..... Would it be possible to use scatterpie and put x and y in spatial coordinates? – thomas leon Apr 03 '23 at 06:39
  • I think you're on the right track, it seems like the use of the `scatterpie` package is exactly what you are looking for. I agree the linked question in the that is provided in the "duplicate post" box is not what you are looking for but rather seems to be more viable for faceting. It seems though that like me you tracked down the scatterpie solution through the link in the comment to the scatterpie package, which may be why the question was closed as duplicate. https://cran.r-project.org/web/packages/scatterpie/vignettes/scatterpie.html – dandrews Apr 03 '23 at 10:58

0 Answers0