在 facet_wrap 中命名方面
naming facets in facet_wrap
我在尝试命名使用 facet_wrap 功能创建的一组图时遇到问题。我特别想将标题包装成多行。我在堆栈溢出中广泛研究了这个问题,但找不到我正在生成的错误。代码如下。 a2$variable 是一列字符串(用于分组目的),a2$ma_3 和 a2$ma_12 是我试图绘制的移动平均线。生成的错误是:
as.integer(n) 错误:
无法将类型 'closure' 强制转换为类型 'integer'
的向量
p1=a2 %>%
ggplot(aes(x = date, color = variable)) +
geom_line(aes(y = ma_12), color = "aquamarine3", alpha = 0.5,size=.7) +
geom_line(aes(y = ma_3), color = "gray40", alpha = 0.5,size=.7) +
facet_wrap(~ variable, ncol = 3, scale = "free_y",label_wrap_gen(width=10))
提前致谢。
我建议在将变量发送到 ggplot 之前对其进行格式化,如下所示:
library(tidyverse)
mtcars %>%
rownames_to_column() %>%
head() %>%
mutate(carname = stringr::str_wrap(rowname, 10)) %>%
ggplot(aes(x = mpg, color = cly)) +
geom_point(aes(y = wt), color = "aquamarine3", alpha = 0.5,size=5) +
geom_point(aes(y = qsec), color = "gray40", alpha = 0.5,size=5) +
facet_wrap(~ carname, ncol = 3, scale = "free_y")
你很接近。要修改 facet_wrap
标签,我们使用 labeller
参数:
library(tibble)
library(ggplot2)
mtcars %>%
rownames_to_column() %>%
head() %>%
ggplot(aes(x = mpg, color = cly)) +
geom_point(aes(y = wt), color = "aquamarine3", alpha = 0.5,size=5) +
geom_point(aes(y = qsec), color = "gray40", alpha = 0.5,size=5) +
facet_wrap(~ rowname, ncol = 3, scale = "free_y",
labeller = label_wrap_gen(width = 10))
输出:
我在尝试命名使用 facet_wrap 功能创建的一组图时遇到问题。我特别想将标题包装成多行。我在堆栈溢出中广泛研究了这个问题,但找不到我正在生成的错误。代码如下。 a2$variable 是一列字符串(用于分组目的),a2$ma_3 和 a2$ma_12 是我试图绘制的移动平均线。生成的错误是:
as.integer(n) 错误: 无法将类型 'closure' 强制转换为类型 'integer'
的向量p1=a2 %>%
ggplot(aes(x = date, color = variable)) +
geom_line(aes(y = ma_12), color = "aquamarine3", alpha = 0.5,size=.7) +
geom_line(aes(y = ma_3), color = "gray40", alpha = 0.5,size=.7) +
facet_wrap(~ variable, ncol = 3, scale = "free_y",label_wrap_gen(width=10))
提前致谢。
我建议在将变量发送到 ggplot 之前对其进行格式化,如下所示:
library(tidyverse)
mtcars %>%
rownames_to_column() %>%
head() %>%
mutate(carname = stringr::str_wrap(rowname, 10)) %>%
ggplot(aes(x = mpg, color = cly)) +
geom_point(aes(y = wt), color = "aquamarine3", alpha = 0.5,size=5) +
geom_point(aes(y = qsec), color = "gray40", alpha = 0.5,size=5) +
facet_wrap(~ carname, ncol = 3, scale = "free_y")
你很接近。要修改 facet_wrap
标签,我们使用 labeller
参数:
library(tibble)
library(ggplot2)
mtcars %>%
rownames_to_column() %>%
head() %>%
ggplot(aes(x = mpg, color = cly)) +
geom_point(aes(y = wt), color = "aquamarine3", alpha = 0.5,size=5) +
geom_point(aes(y = qsec), color = "gray40", alpha = 0.5,size=5) +
facet_wrap(~ rowname, ncol = 3, scale = "free_y",
labeller = label_wrap_gen(width = 10))
输出: