2017-10-21 246 views

回答

1

我使用的是iris作爲例子,因爲只有3個組(而不是6個類的mpg),並且沒有涉及mpg示例中由「2seater」創建的奇怪問題。 (i)創建彙總統計數據。在這種情況下,正數:

ann_text <- iris %>% 
      group_by(Species) %>% 
      summarise(n=n()) 

(二)創建自定義標籤

sp.labels <- c(
    setosa = paste0("setosa\n(n=", ann_text[ann_text$Species=="setosa", "n"],")"), 
    versicolor = paste0("versicolor\n(n=", ann_text[ann_text$Species=="versicolor", "n"],")"), 
    virginica = paste0("virginica\n(n=", ann_text[ann_text$Species=="virginica", "n"],")") 
) 

(三)情節與自定義標籤:

ggplot(iris, aes(Petal.Length, Sepal.Length)) + 
    geom_point() + 
    facet_wrap(~Species, 
      labeller=labeller(
       label_wrap_gen(width=10), 
       Species=sp.labels)) 

enter image description here

待辦事項參考construct labelling specification作爲進一步的例子。

+2

如果將計數包括在分面列中,則可以避免使用標註器功能:例如:'iris%>% group_by(種類)%>% mutate(lab = paste0(Species,「\ n ())+ geom_point()+ facet_wrap(〜lab)'% – eipi10