r结合ggRadar和facet_wrap

时间:2017-05-11 16:10:25

标签: r ggplot2 radar-chart

下面的代码说明了我的问题。我想在ggiraphExtra中使用ggRadar函数,因为它允许与图形交互。我想为每种营养素制作一个单独的蜘蛛图,我希望每个蜘蛛图每年都有一组独立的连接点。正如我现在所理解的那样,在ggRadar中,代码mapping = aes(colour = year)为每年提供单独的点。在facet_wrap中,代码facet_wrap(~ nutrient)决定了有多少单独的蜘蛛图。

require(ggiraphExtra)
require(ggplot2)
spiderData <- data.frame(year = c("2010", "2010", "2010", "2010", "2030", "2030", "2030", "2030", "2050", "2050", "2050", "2050"), 
                         beverages = c(0.07, 0.02, 0.02, 0.04, 0.09, 0.02, 0.03, 0.06, 0.15, 0.03, 0.05, 0.09), 
                         dairy = c(8.2, 6.46, 5.78, 0, 9.1, 7.16, 6.42, 0, 11.7, 9.21, 8.25, 0), 
                         fish = c(0, 0.01, 0.03, 0, 0, 0.02, 0.05, 0, 0, 0.05, 0.16, 0), 
                         nutrient = c("carbohydrate", "fat", "protein", "total_fiber", "carbohydrate", "fat", "protein", "total_fiber", "carbohydrate", "fat", "protein", "total_fiber"), stringsAsFactors = FALSE)
p <- ggRadar(data = spiderData, mapping = aes(colour = year), 
             rescale = FALSE, interactive = FALSE, use.label = TRUE, size = 2,
             legend.position = "right") + facet_wrap(~ nutrient)
p

此代码生成以下错误消息。

  

combine_vars出错(数据,参数$ plot_env,vars,drop = params $ drop)   :至少一个图层必须包含用于构图的所有变量

对修复的建议表示赞赏!

1 个答案:

答案 0 :(得分:1)

供参考,以供他人参考:在代码中指定kwic:summarize()时只需添加参数facet=nurtient

mapping

Renders as: 编辑:已删除require(ggiraphExtra) require(ggplot2) spiderData <- data.frame(year = c("2010", "2010", "2010", "2010", "2030", "2030", "2030", "2030", "2050", "2050", "2050", "2050"), beverages = c(0.07, 0.02, 0.02, 0.04, 0.09, 0.02, 0.03, 0.06, 0.15, 0.03, 0.05, 0.09), dairy = c(8.2, 6.46, 5.78, 0, 9.1, 7.16, 6.42, 0, 11.7, 9.21, 8.25, 0), fish = c(0, 0.01, 0.03, 0, 0, 0.02, 0.05, 0, 0, 0.05, 0.16, 0), nutrient = c("carbohydrate", "fat", "protein", "total_fiber", "carbohydrate", "fat", "protein", "total_fiber", "carbohydrate", "fat", "protein", "total_fiber"), stringsAsFactors = FALSE) p <- ggRadar(data = spiderData, mapping = aes(colour = year, facet=nutrient), rescale = FALSE, interactive = FALSE, use.label = TRUE, size = 2, legend.position = "right") p > devtools::session_info() version R version 3.4.4 (2018-03-15) ggplot2 * 3.0.0 2018-07-03 cran (@3.0.0) ggiraphExtra * 0.2.9 2018-07-22 CRAN (R 3.4.4)