2016-07-19 2 views
1

Hier ist mein Code für das angeschlossene Grundstück:ggplot: Wie füllt man 4 Bereiche, die durch 3 Zeilen getrennt sind?

require(ggplot2) 
gpt <- ggplot(res, aes(x = x, y = y, color = group)) + 
    geom_ribbon(aes(ymin = y, ymax = 1, linetype = NA, 
        fill = group), 
       alpha = 0.4) + 
    geom_line(size = 1) + 
    scale_x_reverse(limits = c(1, 0.1), breaks = seq(0.1, 1, by = 0.1))+ 
    scale_y_continuous(limits = c(0, 1), 
        breaks = seq(0, 1, by = 0.1)) + 
    theme_bw() 
print(gpt) 

enter image description here

ich auch zwischen der blauen Linie des Bereich füllen möchte und Null horizontale Linie. Wie könnte ich das erreichen? Wie könnte ich die ersten drei Standardfarben in ggplot für verschiedene Gruppen manuell angeben?

Vielen Dank für Ihre Hilfe!

Im Folgenden finden Sie die Daten:

> dput(res) 
structure(list(y = c(1, 0.9986, 0.9976, 0.997, 0.9964, 0.996, 
0.996, 0.9958, 0.9952, 0.9942, 0.9938, 0.9934, 0.9928, 0.9924, 
0.992, 0.9918, 0.9918, 0.9916, 0.9912, 0.9904, 0.9898, 0.9892, 
0.9888, 0.9882, 0.9878, 0.9872, 0.9866, 0.9858, 0.9846, 0.9836, 
0.983, 0.9826, 0.9822, 0.981, 0.9802, 0.98, 0.9792, 0.978, 0.9772, 
0.9758, 0.975, 0.9746, 0.9738, 0.9726, 0.972, 0.9714, 0.9704, 
0.97, 0.969, 0.9688, 0.9682, 0.9668, 0.9654, 0.9642, 0.9632, 
0.962, 0.9606, 0.959, 0.9584, 0.9576, 0.9556, 0.9546, 0.9536, 
0.952, 0.9492, 0.9482, 0.9466, 0.9444, 0.943, 0.942, 0.9398, 
0.9388, 0.9366, 0.9352, 0.9338, 0.9318, 0.9294, 0.928, 0.9258, 
0.9248, 0.9232, 0.9224, 0.9196, 0.9172, 0.9146, 0.911, 0.9092, 
0.9068, 0.9032, 0.8998, 0.8946, 0.891, 0.8848, 0.8806, 0.8734, 
0.868, 0.8606, 0.8528, 0.8462, 0.8366, 1, 0.9986, 0.9986, 0.998, 
0.9974, 0.9972, 0.997, 0.997, 0.997, 0.9964, 0.996, 0.9958, 0.9952, 
0.995, 0.9946, 0.9944, 0.9938, 0.9936, 0.9932, 0.9926, 0.992, 
0.992, 0.9916, 0.991, 0.9906, 0.9898, 0.9896, 0.9888, 0.9884, 
0.9882, 0.9874, 0.9868, 0.986, 0.9852, 0.985, 0.984, 0.9828, 
0.9822, 0.9812, 0.9808, 0.9794, 0.979, 0.978, 0.9772, 0.9764, 
0.9758, 0.9754, 0.9748, 0.9744, 0.9732, 0.9724, 0.971, 0.9704, 
0.97, 0.9692, 0.9676, 0.9672, 0.965, 0.964, 0.9622, 0.9614, 0.9602, 
0.9598, 0.9592, 0.9576, 0.9554, 0.9544, 0.952, 0.9496, 0.9486, 
0.9466, 0.9454, 0.943, 0.9418, 0.9398, 0.9382, 0.9366, 0.9348, 
0.934, 0.932, 0.9294, 0.9284, 0.9268, 0.9246, 0.9228, 0.92, 0.9186, 
0.915, 0.9126, 0.9086, 0.9058, 0.9002, 0.8958, 0.8898, 0.8846, 
0.8768, 0.8712, 0.8648, 0.8572, 0.8478, 1, 0.999, 0.9968, 0.996, 
0.9932, 0.9916, 0.9882, 0.9842, 0.9784, 0.9696, 0.9604, 0.9496, 
0.9358, 0.9202, 0.893, 0.862, 0.8298, 0.7918, 0.749, 0.7014, 
0.6452, 0.5862, 0.5234, 0.4524, 0.3784, 0.2998, 0.2096, 0.1282, 
0.054, 0.01, 4e-04, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), x = c(1, 0.990909090909091, 
0.981818181818182, 0.972727272727273, 0.963636363636364, 0.954545454545454, 
0.945454545454545, 0.936363636363636, 0.927272727272727, 0.918181818181818, 
0.909090909090909, 0.9, 0.890909090909091, 0.881818181818182, 
0.872727272727273, 0.863636363636364, 0.854545454545455, 0.845454545454545, 
0.836363636363636, 0.827272727272727, 0.818181818181818, 0.809090909090909, 
0.8, 0.790909090909091, 0.781818181818182, 0.772727272727273, 
0.763636363636364, 0.754545454545455, 0.745454545454545, 0.736363636363636, 
0.727272727272727, 0.718181818181818, 0.709090909090909, 0.7, 
0.690909090909091, 0.681818181818182, 0.672727272727273, 0.663636363636364, 
0.654545454545455, 0.645454545454546, 0.636363636363636, 0.627272727272727, 
0.618181818181818, 0.609090909090909, 0.6, 0.590909090909091, 
0.581818181818182, 0.572727272727273, 0.563636363636364, 0.554545454545455, 
0.545454545454546, 0.536363636363636, 0.527272727272727, 0.518181818181818, 
0.509090909090909, 0.5, 0.490909090909091, 0.481818181818182, 
0.472727272727273, 0.463636363636364, 0.454545454545455, 0.445454545454545, 
0.436363636363636, 0.427272727272727, 0.418181818181818, 0.409090909090909, 
0.4, 0.390909090909091, 0.381818181818182, 0.372727272727273, 
0.363636363636364, 0.354545454545455, 0.345454545454546, 0.336363636363636, 
0.327272727272727, 0.318181818181818, 0.309090909090909, 0.3, 
0.290909090909091, 0.281818181818182, 0.272727272727273, 0.263636363636364, 
0.254545454545455, 0.245454545454545, 0.236363636363636, 0.227272727272727, 
0.218181818181818, 0.209090909090909, 0.2, 0.190909090909091, 
0.181818181818182, 0.172727272727273, 0.163636363636364, 0.154545454545455, 
0.145454545454546, 0.136363636363636, 0.127272727272727, 0.118181818181818, 
0.109090909090909, 0.1, 1, 0.990909090909091, 0.981818181818182, 
0.972727272727273, 0.963636363636364, 0.954545454545454, 0.945454545454545, 
0.936363636363636, 0.927272727272727, 0.918181818181818, 0.909090909090909, 
0.9, 0.890909090909091, 0.881818181818182, 0.872727272727273, 
0.863636363636364, 0.854545454545455, 0.845454545454545, 0.836363636363636, 
0.827272727272727, 0.818181818181818, 0.809090909090909, 0.8, 
0.790909090909091, 0.781818181818182, 0.772727272727273, 0.763636363636364, 
0.754545454545455, 0.745454545454545, 0.736363636363636, 0.727272727272727, 
0.718181818181818, 0.709090909090909, 0.7, 0.690909090909091, 
0.681818181818182, 0.672727272727273, 0.663636363636364, 0.654545454545455, 
0.645454545454546, 0.636363636363636, 0.627272727272727, 0.618181818181818, 
0.609090909090909, 0.6, 0.590909090909091, 0.581818181818182, 
0.572727272727273, 0.563636363636364, 0.554545454545455, 0.545454545454546, 
0.536363636363636, 0.527272727272727, 0.518181818181818, 0.509090909090909, 
0.5, 0.490909090909091, 0.481818181818182, 0.472727272727273, 
0.463636363636364, 0.454545454545455, 0.445454545454545, 0.436363636363636, 
0.427272727272727, 0.418181818181818, 0.409090909090909, 0.4, 
0.390909090909091, 0.381818181818182, 0.372727272727273, 0.363636363636364, 
0.354545454545455, 0.345454545454546, 0.336363636363636, 0.327272727272727, 
0.318181818181818, 0.309090909090909, 0.3, 0.290909090909091, 
0.281818181818182, 0.272727272727273, 0.263636363636364, 0.254545454545455, 
0.245454545454545, 0.236363636363636, 0.227272727272727, 0.218181818181818, 
0.209090909090909, 0.2, 0.190909090909091, 0.181818181818182, 
0.172727272727273, 0.163636363636364, 0.154545454545455, 0.145454545454546, 
0.136363636363636, 0.127272727272727, 0.118181818181818, 0.109090909090909, 
0.1, 1, 0.990909090909091, 0.981818181818182, 0.972727272727273, 
0.963636363636364, 0.954545454545454, 0.945454545454545, 0.936363636363636, 
0.927272727272727, 0.918181818181818, 0.909090909090909, 0.9, 
0.890909090909091, 0.881818181818182, 0.872727272727273, 0.863636363636364, 
0.854545454545455, 0.845454545454545, 0.836363636363636, 0.827272727272727, 
0.818181818181818, 0.809090909090909, 0.8, 0.790909090909091, 
0.781818181818182, 0.772727272727273, 0.763636363636364, 0.754545454545455, 
0.745454545454545, 0.736363636363636, 0.727272727272727, 0.718181818181818, 
0.709090909090909, 0.7, 0.690909090909091, 0.681818181818182, 
0.672727272727273, 0.663636363636364, 0.654545454545455, 0.645454545454546, 
0.636363636363636, 0.627272727272727, 0.618181818181818, 0.609090909090909, 
0.6, 0.590909090909091, 0.581818181818182, 0.572727272727273, 
0.563636363636364, 0.554545454545455, 0.545454545454546, 0.536363636363636, 
0.527272727272727, 0.518181818181818, 0.509090909090909, 0.5, 
0.490909090909091, 0.481818181818182, 0.472727272727273, 0.463636363636364, 
0.454545454545455, 0.445454545454545, 0.436363636363636, 0.427272727272727, 
0.418181818181818, 0.409090909090909, 0.4, 0.390909090909091, 
0.381818181818182, 0.372727272727273, 0.363636363636364, 0.354545454545455, 
0.345454545454546, 0.336363636363636, 0.327272727272727, 0.318181818181818, 
0.309090909090909, 0.3, 0.290909090909091, 0.281818181818182, 
0.272727272727273, 0.263636363636364, 0.254545454545455, 0.245454545454545, 
0.236363636363636, 0.227272727272727, 0.218181818181818, 0.209090909090909, 
0.2, 0.190909090909091, 0.181818181818182, 0.172727272727273, 
0.163636363636364, 0.154545454545455, 0.145454545454546, 0.136363636363636, 
0.127272727272727, 0.118181818181818, 0.109090909090909, 0.1), 
    group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor")), .Names = c("y", 
"x", "group"), row.names = c(NA, -300L), class = "data.frame") 
+0

Um die Gruppe Farben zu ändern, verwenden Sie 'scale_fill_manual'. '+ scale_fill_manual (Werte = c (" A "=" grün "," B "=" braun ", " C "=" orange "))' – Sumedh

Antwort

1

Hier ist eine Idee auf einen neuen Datenrahmen basierend Werte für einen Polygon für den jeweiligen Bereich enthält:

D <- res[res$group == "C" & res$y != 0, 1:2] 
D <- rbind(D, data.frame(y = 0, x = 1)) 

Sie müssen nur die Daten Argument bewegen aus ggplot() und fügen geom_polygon:

ggplot() + 
    geom_polygon(data = D, aes(x = x, y = y)) + 
    geom_ribbon(data = res, aes(x = x, ymin = y, ymax = 1, linetype = NA, 
        fill = group), 
       alpha = 0.4) + 
    geom_line(data = res, aes(x = x, y = y, color = group), size = 1) + 
    scale_x_reverse(limits = c(1, 0.1), breaks = seq(0.1, 1, by = 0.1))+ 
    scale_y_continuous(limits = c(0, 1), 
        breaks = seq(0, 1, by = 0.1)) + 
    theme_bw() 

enter image description here

Wenn Sie die Füllfarbe und umfassen den neuen Bereich in der Legende ändern möchten Sie tun könnten:

ggplot() + 
    geom_polygon(data = D, aes(x = x, y = y, fill = "D"), alpha = 0.4) + 
    geom_ribbon(data = res, aes(x = x, ymin = y, ymax = 1, linetype = NA, 
           fill = group), 
       alpha = 0.4) + 
    geom_line(data = res, aes(x = x, y = y, color = group), size = 1, show.legend = FALSE) + 
    scale_x_reverse(limits = c(1, 0.1), breaks = seq(0.1, 1, by = 0.1))+ 
    scale_y_continuous(limits = c(0, 1), 
        breaks = seq(0, 1, by = 0.1)) + 
    theme_bw() + 
    scale_fill_manual(values = c("blue", "orange", "red", "yellow")) 
+0

Wow, vielen Dank! Noch eine Frage, wie könnte ich die Legende basierend auf den gefüllten Bereichen darstellen? Ich denke, es macht wenig Sinn, die Legende von 3 Zeilen in diesem neuen Grundstück zu zeigen. –

+0

verstehe ich nicht ganz, also wollen Sie eine Legende, die alle 4 gefüllten Bereiche enthält? – beetroot

+0

Ja das ist was ich meine –

Verwandte Themen