Seite 1 von 1

Panels in xyplot Lattice und geom_point ggplot2 zusammenfügen

Verfasst: Sa Okt 24, 2020 6:57 pm
von Martina1909
Hallo,

ich möchte folgendes für Datensatz CO2 (package="datasets") plotten:
Es sollen jeweils für die Pflanzen in Quebec und Mississippi (No 1-3) die non-chilled bzw. chilled-Varianten der jeweiligen Nummer in 1 Panel anstatt in 2 Panels dargestellt werden. Das soll den Vergleich zwischen der CO2-Aufnahme von der gekühlten und ungekühlten Version derselben Pflanze ermöglichen.

Ich habe derzeit folgenden Code - die Grafiken sind am Ende eingefügt:

Code: Alles auswählen

library("lattice")
library("ggplot2")

data("CO2", package = "datasets")
CO = CO2

str(CO)

levels(CO$Plant) = c("Mn1","Mn2","Mn3","Mc1","Mc2","Mc3",
                     "Qn1","Qn2","Qn3","Qc1","Qc2","Qc3")

xyplot(uptake ~ conc | Plant, groups = Treatment, data = CO,
       auto.key = list(space="top", columns = 2,cex = 0.8, font = 2),
       xlab = list(label = "CO2-Konzentration in mL/L",
                   cex = 0.8, font = 2),
       ylab = list(label = "CO2-Aufnahme in umol/m²",
                   cex = 0.8, font = 2),
       scales = list(x = list(cex = 0.7, at = c(200,500,800)),  # x-Achse
                     y = list(cex = 0.7)),  # y-Achse
       par.strip.text = list(col="white",cex = 0.8),  # Panel-Schrift
       par.settings =
       list(superpose.symbol = list(pch = c(3,1), col = c(2,4)), # pch-Typ & -Farbe
            strip.background = list(col="black")))  # Hintergrund Panelbeschriftung

ggplot(data = CO, aes(x = conc, y = uptake, colour = Treatment)) + 
  geom_point(aes(shape = Treatment, color = Treatment)) + 
  facet_grid(~Plant) +
  xlab("CO2-Konzentration in mL/L") +
  ylab("CO2-Aufnahme in umol/m²") +
  theme(axis.title.x = 
        element_text(face = "bold", margin=margin(15,0,0,0)),
        axis.title.y = 
        element_text(face = "bold", margin=margin(0,15,0,0)),
        legend.title = element_blank()) +  # kein Legendentitel
  scale_colour_manual(values = c("nonchilled" = 2,"chilled" = 4)) +  # Punktfarbe
  scale_shape_manual(values = c(1,3))   # Punktsymbol
 
D.h. was jetzt noch bei beiden Grafiken fehlt, ist das "Zusammenschieben" der zusammengehörenden Panels (also bpsw. Mn1 + Mc1, usw...).
Die Panel-Überschrift soll dann jeweils nur den Pflanzennamen ohne c bzw. n enthalten (also bspw. M1, usw...)
Bei der ggplot-Version möchte ich außerdem noch die y-Achsen-Ansicht stauchen, sodass die Werte nicht so weit auseinandergezogen sind.
(Die Anzahl der Ticks auf der x-Achse müssen wahrscheinlich ebenso noch angepasst werden).

Weiß jemand, wie ich das erreichen kann?

Danke euch!

LG, Martina

Re: Panels in xyplot Lattice und geom_point ggplot2 zusammenfügen

Verfasst: So Okt 25, 2020 10:36 am
von Athomas
Ich finde es überflüssig (und eher irritierend), neben der Farbe auch noch die Form der Punkte zu variieren:

Code: Alles auswählen

CO2$Nr <- as.integer(substr(as.character(CO2$Plant), 3, 3))

ggplot(data = CO2) + 
  theme_bw() +
  geom_point(aes(x = conc, y = uptake, colour = Treatment)) + 
  facet_grid(Type ~ Nr) +
  xlab("CO2-Konzentration in mL/L") +
  ylab("CO2-Aufnahme in \u00B5mol/m²") +
  theme(axis.title.x = 
          element_text(face = "bold", margin=margin(15,0,0,0)),
        axis.title.y = 
          element_text(face = "bold", margin=margin(0,15,0,0)),
        legend.title = element_blank()) +  # kein Legendentitel
  scale_colour_manual(values = c("nonchilled" = 2,"chilled" = 4)) 

Re: Panels in xyplot Lattice und geom_point ggplot2 zusammenfügen

Verfasst: Mi Okt 28, 2020 6:18 pm
von Martina1909
Danke für die Grafik, funktioniert wunderbar =)
Und ja, hast Recht, gleiche Punktarten sind fürs Auge wesentlich besser...

Ich hab noch zwei Fragen:
- Hast du vl. noch einen Tipp, was ich im xy-Plot ändern muss, damit ich dieselbe Grafik erhalte?
Ich hab schon mehrere Varianten mit der Nr-Variable probiert, aber ich komme da irgendwie nicht hin...
- Ich hab die Beschriftung der y-Achse bzw. der Panels angepasst:

Code: Alles auswählen

ggplot(data = CO, aes(x = conc, y = uptake, colour = Treatment)) + 
  geom_point(aes(shape = Treatment, color = Treatment)) + 
  facet_grid(Type ~ Nr, switch = "y") +
  xlab("CO2-Konzentration in mL/L") +
  ylab("CO2-Aufnahme in umol/m²") +
  theme(axis.title.x = 
        element_text(face = "bold", margin=margin(15,0,0,0)),
        axis.title.y = 
        element_text(face = "bold", margin=margin(0,15,0,0)),
        legend.title = element_blank()) +  # kein Legendentitel
  scale_colour_manual(values = c("nonchilled" = 2,"chilled" = 4)) +  # Punktfarbe
  scale_shape_manual(values = c(20,20)) +  # Punktsymbol
  scale_y_continuous(position = "right")  # Verschiebung Ticks
Jetzt möchte ich aber noch den y-Achsentitel links von der Grafik anzeigen anstatt derzeit rechts.
Das lässt sich aber nicht ändern, ohne die Ticks auch wieder nach links zu verschieben... Gibts da eine Möglichkeit,
sodass die Ticks rechts, und der Achsentitel (CO2-Aufnahme...) links angezeigt wird?

Danke dir!

Re: Panels in xyplot Lattice und geom_point ggplot2 zusammenfügen

Verfasst: Do Okt 29, 2020 12:42 pm
von EDi
Da du position="right" verwendest, probier mal

axis.title.y.right anstatt axis.title.y