Объединение двух типов меток в facet_grid - PullRequest
0 голосов
/ 29 мая 2019

У меня есть следующий код facet_grid + ggplot2:

    ggplot(output,aes(x=as.factor(X_and_Color),y=Y_values,fill=as.factor(X_and_Color)))+
  geom_bar(stat="identity",position='dodge')+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+
  theme(text = element_text(size=18),legend.position = "bottom")+
  facet_grid(rows=vars(facet_rows),cols=vars(facet_columns),scales="free",labeller = label_bquote(cols=N[t0]==.(facet_columns)))

Когда получающиеся графики выглядят хорошо, но метки для строк должны быть обернуты текстом.

Я смотрел, используя label_wrap_gen(), но не удалось правильно вписать его в label_bquote() только для строк.

Редактировать 1:

output<-structure(list(X_and_Color = c(100, 1000, 5000, 100, 1000, 5000, 
100, 1000, 5000, 100, 1000), Y_values = c(0.867583920521508, 
0.124953765675481, 344.093547640026, 0.0134418163074479, 0.0890165925565165, 
155.778207495509, 86.4571212256961, 13.2165726083926, 0.395814237374253, 
33.928067237654, 175.145881111351), facet_rows = structure(c(2L, 
2L, 1L, 7L, 7L, 4L, 5L, 5L, 3L, 6L, 6L), .Label = c("Effective Number of Haplotypes", 
"Gene Diversity", "Nucleotide Diversity", "Number of Haplotypes", 
"Number of Heterozygotes", "Number of Polymorphic Sites", "Site by Site Heterozygosity"
), class = "factor"), Not_Needed = c(1e-04, 1e-04, 1e-04, 1e-04, 
1e-04, 1e-04, 1e-04, 1e-04, 1e-04, 1e-04, 1e-04), facet_columns = c(2, 
100, 2, 2, 100, 2, 2, 100, 2, 2, 100)), .Names = c("X_and_Color", 
"Y_values", "facet_rows", "Not_Needed", "facet_columns"), row.names = c(1L, 
7L, 13L, 19L, 25L, 31L, 37L, 43L, 49L, 55L, 61L), class = "data.frame")

Используя эти данные и приведенный выше код, я получаю это дляметки строк: enter image description here

1 Ответ

1 голос
/ 05 июня 2019

Мы можем использовать функцию str_wrap из stringr для строк facet_grid отдельно.Также настройте параметр size в соответствии с вашими предпочтениями / размером экрана, чтобы метки были видны.

library(ggplot2)
library(stringr)

ggplot(output,aes(x=as.factor(X_and_Color),y=Y_values,fill=as.factor(X_and_Color)))+
   geom_bar(stat="identity",position='dodge')+
   theme(axis.title.x=element_blank(),
         axis.text.x=element_blank(),
         axis.ticks.x=element_blank())+
   theme(text = element_text(size=10),legend.position = "bottom")+
   facet_grid(rows= vars(str_wrap(facet_rows, 2)),
              cols=vars(facet_columns),scales="free", 
              labeller = label_bquote(cols=N[t0]==.(facet_columns)))

enter image description here

...