R ggplot facet_grid_sc与cols

时间:2019-06-17 08:19:54

标签: r ggplot2 facet-grid

我正在尝试使用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))),那么我会收到此错误消息:

  

.subset2(x,i,确切=准确)中的错误:尝试选择较少   比get1index中的一个元素

我可能写错了,但是在软件包的帮助下我找不到如何正确编写它。有人可以帮我吗?

谢谢!

Nath

1 个答案:

答案 0 :(得分:0)

我没有让您幻想facet_grid_sc上班,但是这里有一个使用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为其标签提供了很多可定制性,必须通过虚拟图来更改图例。