![](/img/trans.png)
[英]facets as in facet_grid, and axis.line as for 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))
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.