带动态标题的管道ggplot2 [英] pipe ggplot2 w/ dynamic title

查看:48
本文介绍了带动态标题的管道ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以获取数据并使用ggplot中的管道作为标题吗?

Can I get to the data and make a title with a pipe in a ggplot?

说我有这样的数据,

x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)

df2 <- reshape2::melt(df1, id.vars='day') %>% data.frame(x, y, day, company = 'inc. company name ')

df2 %>% ggplot(aes(x=day, y=value, fill=variable), xlab="Age Group") + 
  geom_bar(stat = "identity", position = "dodge", width = 0.5) 

但是我想做这样的事情

df2 %>% ggplot(aes(x=day, y=value, fill=variable), xlab="Age Group") + 
  geom_bar(stat = "identity", position = "dodge", width = 0.5) +
  + labs(title = paste0(.$company, "numbers")) # NOT WOKRING CODE

要达到这个目的,

推荐答案

您可以在单个表达式中围住整个ggplot2位,这使您可以在整个ggplot2调用中使用.代词.除了保存两次击键外,我看不到为什么要这样做,但是可以.

You can fence in the entire ggplot2 bit in a single expression, which allows you to use the . pronoun throughout ggplot2 calls. I don't see why you'd want to do this except for saving 2 keystrokes, but you could.

library(tidyverse)

x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)

df1 <- data.frame(x = x, y = y, day = day)

df2 <- reshape2::melt(df1, id.vars='day') %>% data.frame(x, y, day, company = 'inc. company name ')

df2 %>% {
  ggplot(., aes(x=day, y=value, fill=variable), xlab="Age Group") + 
    geom_bar(stat = "identity", position = "dodge", width = 0.5) +
    labs(title = paste0(.$company, "numbers"))
}

reprex软件包(v1.0.0)创建于2021-02-08 sup>

Created on 2021-02-08 by the reprex package (v1.0.0)

这篇关于带动态标题的管道ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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