R ggplot facet_grid_sc 与 cols

R ggplot facet_grid_sc with cols

我正在尝试使用 facet_grid_sc 来操纵 y 轴,但是通过按列而不是按行绘制面板。我有以下数据框:

    test2 <- structure(list(stream = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Feed", "Cells 1-4", 
"Cells 5-8", "Cells 9-12", "Totalconcentrate", "Tailings"), class = "factor"), 
    mineral = c("Calcite", "Calcite", "Calcite", "Calcite", "Scheelite", 
    "Scheelite", "Scheelite", "Scheelite", "Calcite", "Calcite", 
    "Calcite", "Calcite", "Scheelite", "Scheelite", "Scheelite", 
    "Scheelite", "Calcite", "Calcite", "Calcite", "Calcite", 
    "Scheelite", "Scheelite", "Scheelite", "Scheelite", "Calcite", 
    "Calcite", "Calcite", "Calcite", "Scheelite", "Scheelite", 
    "Scheelite", "Scheelite"), shapefactor = structure(c(3L, 
    1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 
    3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 
    4L), .Label = c("Angularity", "Circularity", "Formfactor", 
    "Roundness"), class = "factor"), mean = c(0.191074267258554, 
    1.57871188864644, 4.98640988695014, 0.709748496492633, 0.255307602333514, 
    1.41318627525434, 4.48236746482907, 0.787906844284224, 0.2370993275776, 
    1.59011418196729, 5.00866589220356, 0.708099932389451, 0.379279621962832, 
    1.41798512797767, 4.49174029724501, 0.803054249581329, 0.188107140488459, 
    1.58446664800185, 4.99394785197469, 0.720664938740251, 0.261663000285933, 
    1.33457686608134, 4.2649277507168, 0.809433325901688, 0.204386468447994, 
    1.55129002878455, 4.88754754288822, 0.761051008277419, 0.432222746956355, 
    1.22012862228623, 3.87276933395819, 0.861599941934953)), .Names = c("stream", 
"mineral", "shapefactor", "mean"), row.names = c(73L, 74L, 75L, 
76L, 93L, 94L, 95L, 96L, 125L, 126L, 127L, 128L, 145L, 146L, 
147L, 148L, 177L, 178L, 179L, 180L, 197L, 198L, 199L, 200L, 281L, 
282L, 283L, 284L, 301L, 302L, 303L, 304L), class = "data.frame")

我使用以下代码绘制它:

scales_y <- list(
  "Angularity" = scale_y_continuous(limits = c(0.5,2)),
  "Circularity" = scale_y_continuous(limits = c(2,5.5)),
  "Formfactor" = scale_y_continuous(limits = c(0,0.5)),
  "Roundness" = scale_y_continuous(limits = c(0.6,0.9))
)

g <- ggplot(test2, aes(x=stream, y=mean, color=mineral, group=mineral))
g <- g + geom_point()
g <- g + geom_line()
g <- g + theme_bw()
g <- g + theme(axis.text.x = element_text(size =8),
               axis.ticks.x=element_blank(),
               legend.position="bottom")
g <- g + scale_color_brewer(palette = "Paired")
g <- g + facet_grid_sc(rows = vars(shapefactor), scales = list(y = scales_y))
print(g)

这很好用。但是,如果我想在列而不是行中绘制形状因子(所以写 facet_grid_sc(cols = vars(shapefactor), scales = list(y = scales_y))),那么我会得到这个错误留言:

Error in .subset2(x, i, exact = exact) : attempt to select less than one element in get1index

我可能写错了,但是我在包的帮助下找不到如何正确地写它。谁能帮帮我?

提前致谢!

纳斯

我没有让你喜欢的 facet_grid_sc 工作,但这里有一个替代的,有点 hack-ey 的解决方案,使用 cowplot:


library(tidyverse)
library(cowplot)


# split, not group by for the labels
out <- test2 %>% split(.,.$shapefactor) %>% 
  map( ~ggplot(.,aes(x=stream, y=mean, color=mineral, group=mineral)) +
         geom_point() +
         geom_line() +
         theme_bw() +
         theme(axis.text.x = element_text(size =8),
               axis.ticks.x=element_blank(),
               legend.position='none') +
         scale_color_brewer(palette = "Paired") +
         scales_y[[.$shapefactor[1]]])

# create Dummy for legend
dummy <- ggplot(test2,aes(x=stream, y=mean, color=mineral, group=mineral)) +
  geom_point() +
  geom_line()+
  scale_color_brewer(palette = "Paired") +
  theme(legend.position = 'bottom',legend.justification = 'center')

# add legend to list
out$' ' <- cowplot::get_legend(dummy)
cowplot::plot_grid(plotlist = out, ncol = 1,labels = names(out),axis = 'r', align = 'h')

它显然需要一些格式化,但你明白了。 plot_grid 为其标签提供了大量可定制性,必须通过 dummy-plot 更改图例。