下面的代码说明了我的问题。我想在 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

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



对修复的建议表示赞赏!

最佳答案

供引用,如果有人需要它:只需在代码中指定 facet=nurtient 时添加参数 mapping

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)

r 结合 ggRadar 和 facet_wrap-LMLPHP
编辑:删除 facet_wrap

关于r 结合 ggRadar 和 facet_wrap,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43920675/

10-12 17:09