r结合ggRadar和facet_wrap [英] r combine ggRadar and facet_wrap
问题描述
以下代码说明了我的问题.我想在ggiraphExtra中使用ggRadar函数,因为它允许与图形进行交互.我希望每种营养素都有一个单独的蜘蛛图,并且我希望每个蜘蛛图每年都具有一组单独的连接点.据我现在了解,在ggRadar中,代码 mapping = aes(colour = year)
是每年给出单独点的代码.在facet_wrap中,代码 facet_wrap(〜营养)
是确定有多少个单独的蜘蛛图的原因.
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.
Combine_vars中的错误(数据,params $ plot_env,vars,drop = params $ drop):至少一层必须包含用于构面的所有变量
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!
推荐答案
供他人参考:仅在代码中指定 mapping
时添加参数 facet = nurtient
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)
删除了 facet_wrap
这篇关于r结合ggRadar和facet_wrap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!