Seite 1 von 1

NA in Legende

Verfasst: Mi Dez 30, 2020 12:37 pm
von cacadu
Hallo zusammen,
ich habe meine Daten mithilfe von gglpot2 in einem Raster dargestellt. Die NAs sind dabei rot eingefärbt, nun würde ich diese auch gerne in der Legende angezeigt haben (z.b. einfach als kleines Rotes Kästchen,...), habe dazu aber leider noch keine Lösung, da sie ja in meinem Datensatz drin sind und keine eigene Struktur haben, und würde mich freuen, wenn jemand eine Idee dafür hätte.

Meine Daten:

Code: Alles auswählen

Profile<-structure(list(Profil = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 
5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 
11, 11, 12, 12, 12), Tiefe = c(0.1, 0.3, 0.5, 0.1, 0.3, 0.5, 
0.1, 0.3, 0.5, 0.1, 0.3, 0.5, 0.1, 0.3, 0.5, 0.1, 0.3, 0.5, 0.1, 
0.3, 0.5, 0.1, 0.3, 0.5, 0.1, 0.3, 0.5, 0.1, 0.3, 0.5, 0.1, 0.3, 
0.5, 0.1, 0.3, 0.5), Wert = c(438.4, 156.2, 210.6, 1, 0.5, 1.3, 
1.1, 0.5, NA, NA, NA, 1000, 906.47, 24.17, 0.22, 0, 0.34, 0.51, 
0.45, 0, 0.08, 0.03, 2.76, 0.29, 13.03, 65.33, 0.01, 0.34, 0.09, 
0.02, 0.02, 2.23, 0.21, NA, NA, 2.21), Linie = c(0.15, 0.15, 
0.15, 0.24, 0.24, 0.24, 0.13, 0.13, 0.13, 0.45, 0.45, 0.45, 0.31, 
0.31, 0.31, 0.21, 0.21, 0.21, 0.35, 0.35, 0.35, 0.5, 0.5, 0.5, 
0.42, 0.42, 0.42, 0.43, 0.43, 0.43, 0.1, 0.1, 0.1, 0.3, 0.3, 
0.3)), class = "data.frame", row.names = c(NA, -36L))
Mein Code:

Code: Alles auswählen

ggplot(Profile, aes(Profil, Tiefe))+geom_raster(aes(fill=Wert), interpolate=TRUE)+facet_wrap(~Profil, scales = "free")+scale_fill_continuous(name= "Value",breaks=c(0,200,400,600,800,1000), na.value="red",labels=c("0","200", "400", "600", "800", "1000"))
Sessioninfo:

Code: Alles auswählen

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.3.2

loaded via a namespace (and not attached):
 [1] fansi_0.4.1      digest_0.6.27    withr_2.3.0      assertthat_0.2.1 crayon_1.3.4     grid_4.0.3       R6_2.5.0         lifecycle_0.2.0 
 [9] gtable_0.3.0     magrittr_2.0.1   scales_1.1.1     pillar_1.4.7     cli_2.2.0        rlang_0.4.9      farver_2.0.3     rstudioapi_0.13 
[17] vctrs_0.3.6      ellipsis_0.3.1   labeling_0.4.2   tools_4.0.3      glue_1.4.2       munsell_0.5.0    compiler_4.0.3   pkgconfig_2.0.3
Vielen Dank schon einmal,
Cacadu

Re: NA in Legende

Verfasst: Mi Dez 30, 2020 3:45 pm
von bigben
Hi!

Offiziell scheint es dazu nicht viel zu geben: https://ggplot2.tidyverse.org/reference ... dient.html

Aber es gibt immer Leute die es schaffen, ggplot zu einem vernünftigen Verhalten zu überreden: https://stackoverflow.com/a/42380983/6503141

LG,
Bernhard

Re: NA in Legende

Verfasst: Do Dez 31, 2020 10:07 pm
von EDi
ggplot zu einem vernünftigen Verhalten zu überreden
Meiner Meinung nach ist das Verhalten so schon vernünftig (unvernünftig ist es eine kontinuierliche Skala zu diskretisieren).

Ich würde einfach einen text schreiben: "NA values are marked in red color"

Code: Alles auswählen

labs(caption =  "NA values are marked in red color") 

Re: NA in Legende

Verfasst: Do Dez 31, 2020 10:26 pm
von EDi
Mann kann aber auch "tricksen" und eine rote box hinzufügen:

Code: Alles auswählen

ggplot(Profile, aes(Profil, Tiefe))+
  geom_raster(aes(fill = Wert), interpolate=TRUE) +
  facet_wrap(~Profil, scales = "free") +
  scale_fill_continuous(name= "Value",breaks=c(0,200,400,600,800,1000), na.value="red",labels=c("0","200", "400", "600", "800", "1000")) +
  # here starts the magic
  geom_point(aes(size = "NA", shape = NA), color = "red") +
  guides(size = guide_legend(title = NULL, override.aes = list(shape = 15, size = 8))) +
  labs(caption = "NA values are red.")
Idee ist unsichtbare, farbige (shape = NA) Punkt hinzuzufügen (das bringt eine extra Legende) und dann noch bissl anpassen...

Re: NA in Legende

Verfasst: Sa Jan 02, 2021 5:24 pm
von cacadu
Ihr seid super, danke euch!
Dann probier ich es mal mir EDis Tipp, das sieht vielversprechend aus.
Liebe Grüße,
Cacadu