带动态标题的管道 ggplot2
pipe ggplot2 w/ dynamic title
我可以获取数据并在 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
要做到这一点,
我们可以使用
df2 %>% ggplot(aes(x=day, y=value, fill=variable), xlab="Age Group") +
geom_bar(stat = "identity", position = "dodge", width = 0.5) +
labs(title = paste0(first(df2$company), "numbers"))
-输出
您可以在单个表达式中围栏整个 ggplot2 位,这样您就可以在整个 ggplot2 调用中使用 .
代词。除了节省 2 次击键之外,我不明白你为什么要这样做,但你可以。
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 package (v1.0.0)
于 2021-02-08 创建
我可以获取数据并在 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
要做到这一点,
我们可以使用
df2 %>% ggplot(aes(x=day, y=value, fill=variable), xlab="Age Group") +
geom_bar(stat = "identity", position = "dodge", width = 0.5) +
labs(title = paste0(first(df2$company), "numbers"))
-输出
您可以在单个表达式中围栏整个 ggplot2 位,这样您就可以在整个 ggplot2 调用中使用 .
代词。除了节省 2 次击键之外,我不明白你为什么要这样做,但你可以。
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 package (v1.0.0)
于 2021-02-08 创建