2016-08-31 12 views
5

Mam ułożony wykres słupkowy, gdzie kolory reprezentują kategorię, a ja dostosowałem alfę, aby podzielić je na 2. Legenda pokazuje alfę (w odcieniach szarości) i kolory. Jednak chciałbym stworzyć legendę zawierającą kombinacje.Jak mogę łączyć alfa i kolor w jednej legendzie

Aby je połączyć, przyjrzałem się this question, ale nie mogę połączyć alfy i wypełnienia. Oto powtarzalne postać, która nie działa:

mtcars %>% 
ggplot(aes(gear, mpg, fill = as.factor(vs), alpha = as.factor(am)))+ 
geom_bar(stat = "identity")+ 
scale_fill_manual(name = "legend", 
        values = c(
         "0" = "red", 
         "1" = "blue", 
         "0"="red", 
         "1"="blue" 
        ), 
        labels = c("V-engine, automatic", 
          "V-engine, manual", 
          "Straight-engine, automatic", 
          "Straight-engine, manual") 
       )+ 
scale_alpha_manual(name = "legend", 
        values = c(
         "0" = 1, 
         "1"=2/5, 
         "0"=1, 
         "1"=2/5 
         ), 
        labels = c(
         "V-engine, automatic", 
         "V-engine, manual", 
         "Straight-engine, automatic", 
         "Straight-engine, manual" 
         ) ) 

image

Odpowiedz

4

podszedłem Twoje pytanie nie używając aes(alpha) ale traktując jako fill.colouralpha z kombinacji vs i am.

mtcars %>% 
    ggplot(aes(gear, mpg, fill = interaction(as.factor(vs), as.factor(am)))) + 
    geom_bar(stat = "identity") + 
    scale_fill_manual(name = "legend", 
         values = c(
          "0.0" = alpha("red", 1), 
          "1.0" = alpha("blue", 1), 
          "0.1" = alpha("red", 2/5), 
          "1.1" = alpha("blue", 2/5) 
         ), 
         labels = c("V-engine, automatic", 
           "V-engine, manual", 
           "Straight-engine, automatic", 
           "Straight-engine, manual") 
        ) 

enter image description here

+0

To ra eally świetny pomysł! Spróbuję złożyć raport! –