ggplot2, Boxplots

Wie erstelle ich Grafiken, was ist zu beachten?

Moderatoren: EDi, jogo

Antworten
Fanny
Beiträge: 14
Registriert: Fr Mär 05, 2021 3:34 pm

ggplot2, Boxplots

Beitrag von Fanny »

Hallo,

ich habe leider Probleme beim Darstellen meiner Daten: (
Daten für R.xlsx
(10.25 KiB) 87-mal heruntergeladen
)

Ich möchte gerne meine Ergebnisse durch Boxplots darstellen.
Dabei geht es um drei verschiedene Behandlungen, auf zwei verschiedenen Standorten.

Boden3boxplot <- ggplot(data=Boden3, aes(x=Variante,y=infiziert, color=Feld))+geom_boxplot()+theme_test()+
stat_boxplot(geom="errorbar", width=0.8)
Boden3boxplot

Schaut ja so schon ok aus, aber ich will kein Kreuz in der Box und bekomme es nicht raus.
Dazu würde ich gerne die Farben ändern, in klassische Grautöne. Dabei sollen die Datend der gleichen Felder/Standorte gleich bleiben
und sich von dem anderen Feld absetzen.

Kann mir jemand Helfen, für ewige Dankbarkeit?
Vielen Dank fürs Anschauen,
Fanny
Dateianhänge
EPNs, S und B.jpeg
Benutzeravatar
student
Beiträge: 674
Registriert: Fr Okt 07, 2016 9:52 am

Re: ggplot2, Boxplots

Beitrag von student »

Hallo Fanny,

Du weißt doch sicher, dass das "Kreuz" ein wesentlicher Bestandteil des Boxplots ist (es visualisiert den Median)? Ohne diese Verteilungsinformation ist der Boxplot kein Boxplot. Ich kenne Deine Daten natürlich nicht, aber diese Form der Darstellung sieht für mich nicht optimal aus. Vielleicht gibt es eine optimalere Darstellungsform für die vorliegende Verteilung?
Viele Grüße,
Student
-----------------------------------------------------------------------------------------------------------------------
faes.de, Datenanalyse mit R & das Ad-Oculos-Projekt
Das Ad-Oculos-Projekt auf YouTube

Habe Mut, dich deines eigenen Verstandes zu bedienen! (Kant)
bigben
Beiträge: 2771
Registriert: Mi Okt 12, 2016 9:09 am

Re: ggplot2, Boxplots

Beitrag von bigben »

Hallo,

für alle die mitmachen wollen, hier sind die Daten in Code:

Code: Alles auswählen

boden <- structure(list(Variante = c("Kontrolle", "Kontrolle", "Kontrolle", 
                            "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", 
                            "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", 
                            "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", 
                            "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", 
                            "Kontrolle", "Kontrolle", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", 
                            "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", 
                            "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", 
                            "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", 
                            "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", "Kontrolle", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", "1 Mrd. EPNs/ha", 
                            "1 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha", 
                            "5 Mrd. EPNs/ha", "5 Mrd. EPNs/ha"), Feld = c("Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", "Stove", 
                                                                          "Stove", "Stove", "Stove", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee", 
                                                                          "Bocksee", "Bocksee", "Bocksee", "Bocksee", "Bocksee"), infiziert = c(0L, 
                                                                                                                                                0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 2L, 3L, 1L, 3L, 1L, 3L, 2L, 
                                                                                                                                                3L, 5L, 2L, 4L, 5L, 4L, 1L, 4L, 1L, 3L, 3L, 2L, 4L, 3L, 0L, 4L, 
                                                                                                                                                4L, 4L, 1L, 2L, 1L, 3L, 4L, 5L, 5L, 2L, 4L, 3L, 2L, 0L, 1L, 5L, 
                                                                                                                                                5L, 3L, 5L, 2L, 4L, 3L, 0L, 3L, 4L, 5L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                0L, 0L, 0L, 1L, 5L, 1L, 1L, 1L, 0L, 0L, 0L, 2L, 1L, 0L, 1L, 0L, 
                                                                                                                                                1L, 0L, 5L, 0L, 0L, 0L, 2L, 2L, 0L, 5L, 0L, 0L, 0L, 1L, 2L, 1L, 
                                                                                                                                                0L, 0L, 5L, 0L, 2L, 1L, 0L, 1L, 1L, 1L, 0L, 2L, 0L, 2L, 1L, 2L, 
                                                                                                                                                0L, 1L, 2L, 5L, 1L)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                          -150L))

str(boden)
und die Kernfrage dazu lautet, was an der Darstellung ohne Errorbar auszusetzen ist:

Code: Alles auswählen

library(ggplot2)
Boden3boxplot <- ggplot(data=boden, aes(x=Variante,y=infiziert, color=Feld)) +
  geom_boxplot() +
  theme_test()
print(Boden3boxplot)
So sehen Boxplots in ggplot2 nunmal aus und so ist in der Mitte auch kein Kreuz. Geht es einfach nur darum, dass die Whisker am Ende einen Querstrich brauchen, wie in base graphics, also bsp.

Code: Alles auswählen

boxplot(infiziert ~ Variante + Feld, data = boden)
Die Farben und Füllungen der Boxplots in ggplot2 kannst Du über scale_fill_manual() selbst vorgeben, also beispielsweise so:

Code: Alles auswählen

Boden3boxplot <- ggplot(data=boden, aes(x=Variante,y=infiziert, fill=Feld)) +
  geom_boxplot() +
  theme_test() +
  scale_fill_manual(values = c("darkgrey", "lightgrey"))
print(Boden3boxplot)
HTH,
Bernhard
---
Programmiere stets so, dass die Maxime Deines Programmierstils Grundlage allgemeiner Gesetzgebung sein könnte
Antworten