0

I am using ggforce to create a plot like this. this.

My goal is to facet this type of plot.

For background on how the chart was made, check out update 3 on this question. The only modification that I have made was adding a geom_segment between the x axis and the Y value positions.

The reason why I believe faceting this graph is either difficult, or even impossible, is because continuous value x coordinates are used to determine where the geom_arc_bar is positioned in space.

My only idea for getting this to work has been supplying each "characteristic" that I want to facet with a set of x coordinates (1,2,3). Initially, as I will demonstrate in my code, I worked with set of highly curated data. Ideally, I would like to scale this to a dataset with many variables.

In the example graph that I have provided, the Y value is from table8, filtered for rows with "DFT". The area of the half-circles is proportional to the values of DDFS and FDFS from table9. Ideally, I would like to be able to create a function allowing for the easy creation of these graphs, with perhaps 3 parameters, the data for the y value, and for both half circles.

Here is my data.

Here is the code that I have written thus far.

For making a single plot

#Filter desired Age and Measurement
table9 %>%
  filter(Age == "6-11" & Measurement != 'DFS' ) %>%
  select( SurveyYear, Total , Measurement ) %>%
  arrange(SurveyYear) %>%
  dplyr::rename(Percent  = Total) -> table9

#Do the same for table 8.
table8 %>%
  filter(Age == "6-11" & Measurement != "DS" & Measurement != "FS") %>%
  select(SurveyYear, Total) %>%
  dplyr::rename(Y  = Total)-> table8 

table8 <- table8 %>%
  bind_rows(table8) %>%
  arrange(Y) %>%
  add_column(start = rep(c(-pi/2, pi/2), 3), x = c(1,1,2,2,3,3)) 
table8_9 <- bind_cols(table8,table9) %>%
  select(-SurveyYear1) 

#Create the plot
ggplot(table8_9) + geom_segment( aes(x=x, xend=x, y=0, yend=Y), size = 0.5, linetype="solid") +
  geom_arc_bar(aes(x0 = x, y0 = Y, r0 = 0, r = sqrt((Percent*2)/pi)/20,
                   start = start, end = start + pi, fill = Measurement),
               color = "black") + guides(fill = guide_legend(title = "Type", reverse = T)) + 
  guides(fill = guide_legend(title = "Measurement", reverse = F)) +
  xlab("Survey Year") + ylab("Mean dfs") + coord_fixed() + theme_pubr() + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, 5.5)) + 
  scale_x_continuous(breaks = 1:3, labels = paste0(c("1988-1994", "1999-2004", "2011-2014"))) +
  scale_fill_discrete(labels = c("ds/dfs", "fs/dfs")) -> lolliPlot
lolliPlot

Attempt at many plots

#Filter for "DFS"
table8 <- table8 %>%
  filter(Measurement=="DFS")

#Duplicate DF vertically, and add column specifying the start point for the arcs. 
table8 <- table8 %>%
  bind_rows(table8) %>%
  add_column(start = rep(c(-pi/2, pi/2), length(.$SurveyYear)/2), x = rep(x = c(1,2,3),length(.$SurveyYear)/3)) %>%
  arrange(Age, x) 
#Bind two tables today, removing all of the characteristic columns from table 8.
table8_9 <- bind_cols(table8,table9) %>%
  select(-Age1, -SurveyYear1, -Measurement) %>%
  gather(key = Variable, value = Y, -x,-start,-Age, -SurveyYear, -Measurement1, -Total1, -Male1, -Female1, -'White, non-Hispanic1', -'Black, non-hispanic1', -'Mexican American1', -'Less than 100% FPG1', -'100-199% FPG1', -'Greater than 200% FPG1')

This is where I get stuck. I can't figure out a way to format the data so that I can facet the graph. If anybody has any ideas or advice, I would greatly appreciate it.

Mousa
  • 23
  • 5
  • I may have missed it, but on what criterion do you want to facet your plots? – teunbrand Jul 29 '19 at 06:54
  • @teunbrand No you’re right, I forgot to mention that! I would like to facet them by the `Age` and `Variable` variable (after using `gather`) ideally – Mousa Jul 29 '19 at 07:21

0 Answers0