r combine ggRadar and facet_wrap

久未见 提交于 2021-02-19 02:36:29

问题


The code below illustrates my question. I want to use the ggRadar function in ggiraphExtra because it allows interaction with the graph. I want a separate spider graph for each nutrient and I want each spider graph to have a separate set of connected dots for each year. As I understand things now, in ggRadar, the code mapping = aes(colour = year) is what gives separate dots for each year. in facet_wrap the code facet_wrap(~ nutrient) is what determines how many separate spider graphs there are.

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

This code produces the following error message.

Error in combine_vars(data, params$plot_env, vars, drop = params$drop) : At least one layer must contain all variables used for facetting

Suggestions on a fix appreciated!


回答1:


for reference if someone needs it: just add argument facet=nurtient when specifying mapping in your code

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)  

edit: removed facet_wrap



来源:https://stackoverflow.com/questions/43920675/r-combine-ggradar-and-facet-wrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!