R中的Facet_Wrap标签 [英] Facet_Wrap labels in R

查看:1371
本文介绍了R中的Facet_Wrap标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用facet更改ggplot图的标签,并且使用不是facet的数据框中的变量。代码如下 -

  iris%>%
group_by(种类)%>%
总结(lbl = mean(Petal.Length))%>%
select(lbl)%>%
unlist() - > lbls
lbls< - map2(unique(iris $ Species),lbls,paste,sep =\\\

iris%>%
ggplot(aes(x = Sepal .Length,y = Sepal.Width,group = 1))+
geom_point()+
facet_wrap(〜Species,labeller = lbls)

然而,这给了我以下错误 -

  Error in cbind(labels = list(),list(`{`,if(!is.null(.rows)|| 
!is.null(.cols)){:
矩阵行数必须匹配(参见参数2)

我已经看过了贴标签功能和不同的选项,但大部分它们似乎是用于包含在方面的变量,有没有一种方法可以做到这一点?任何导致赞赏,谢谢!

解决方案

p>您可以即时创建一个新的小平面列,并避免 labeller 痛:

 <$ (Species)%>%
mutate(label = paste0(Species,\\\
,round(mean(Petal.Length ),2)))%>%
ggplot(aes(x = Sepal.Length,y = Sepal.Width,group = 1))+
geom_point()+
facet_wrap 〜label)


I'm trying to change labels for ggplot graph with facets, and am using a variable from the dataframe which is not faceted. The code is as belows-

iris %>%
  group_by(Species) %>%
  summarise(lbl = mean(Petal.Length)) %>%
  select(lbl) %>%
  unlist() -> lbls
lbls <- map2(unique(iris$Species), lbls, paste, sep = "\n")
iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, group = 1)) +
  geom_point() +
  facet_wrap(~ Species, labeller = lbls)

However, this gives me the following error-

Error in cbind(labels = list(), list(`{`, if (!is.null(.rows) || 
!is.null(.cols)) { : 
 number of rows of matrices must match (see arg 2)

I've looked at the labeller function and different options but most of them seem to be for variables which are included in facets. Is there a way this can be done? Any leads appreciated, thanks!

解决方案

You could create a new facetting column on the fly and avoid labeller pain:

iris %>%
  group_by(Species) %>% 
  mutate(label = paste0(Species, "\n", round(mean(Petal.Length),2))) %>% 
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, group = 1)) +
    geom_point() +
    facet_wrap(~ label)

这篇关于R中的Facet_Wrap标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆