首页 > 解决方案 > 面对用ggforce制作的比例半面积图?

问题描述

我正在使用 ggforce 创建这样的情节。这个.

我的目标是刻画这种类型的情节。

有关如何制作图表的背景信息,请查看有关此问题的更新 3。我所做的唯一修改是在 x 轴和 Y 值位置之间添加一个。geom_segment

我认为刻面这个图是困难的,甚至是不可能的,因为连续值 x 坐标用于确定geom_arc_bar空间中的位置。

我让这个工作的唯一想法是为我想用一组 x 坐标 (1,2,3) 刻面的每个“特征”提供。最初,正如我将在我的代码中演示的那样,我使用了一组高度精选的数据。理想情况下,我想将其扩展到具有许多变量的数据集。

在我提供的示例图中,Y 值来自 table8,过滤了带有“DFT”的行。半圆的面积与表 9 中 DDFS 和 FDFS 的值成正比。理想情况下,我希望能够创建一个函数,以便轻松创建这些图形,可能有 3 个参数、y 值的数据以及两个半圆。

这是我的数据

这是我迄今为止编写的代码。

用于制作单个情节

#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

尝试许多地块

#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')

这就是我卡住的地方。我想不出一种格式化数据的方法,以便我可以对图表进行刻面。如果有人有任何想法或建议,我将不胜感激。

标签: rggplot2data-visualizationfacetggforce

解决方案


推荐阅读