R ggplot facet_grid_sc с cols - PullRequest
       67

R ggplot facet_grid_sc с cols

2 голосов
/ 17 июня 2019

Я пытаюсь использовать 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), scale = list (y = scale_y))), я получаю следующее сообщение об ошибке:

Ошибка в .subset2 (x, i, точный = точный): попытка выбрать меньше чем один элемент в get1index

Возможно, я пишу это неправильно, но я не могу найти в справке пакета, как написать это правильно. Может кто-нибудь помочь мне, пожалуйста?

Заранее спасибо!

Ната

1 Ответ

0 голосов
/ 17 июня 2019

Я не заставил ваше воображение 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 предлагает множество настроек для своих ярлыков, легенда должна быть изменена с помощью фиктивного сюжета.

...