Seite 2 von 2

Re: back-to-back Barplot

Verfasst: Do Jan 03, 2019 2:30 pm
von EDi
Athomas hat geschrieben: Do Jan 03, 2019 1:55 pm
Both lattice and ggplot2 use grid to draw plots, but both can be used without directly encountering grid.
Ob man deswegen grid als "Basis" von ggplot2 bezeichnen sollte? Ich würde es nicht tun...
Und ob!

grid ist ein alternatives grafik system (zu base graphics) geschrieben von Paul Murrell.
lattice & ggplot bauen darauf auf. Wenn du eine plot mit ggplot2 erzeugst macht grid die Arbeit unter der Haube.

Siehe auch https://www.stat.auckland.ac.nz/~paul/u ... lides.html

grid & graphics zu mischen war früher fast unmöglich, mitlerweile aber im begrenzten Umfang machbar. Es ist aber definitiv weniger aufwändig, bei eoneder beiden Systeme zu bleiben.

Die backtoback barcharts sind mit beiden machbar.

Re: back-to-back Barplot

Verfasst: Do Jan 03, 2019 3:18 pm
von Athomas
Interessant! Dann habe ich ja schon wieder etwas dazugelernt :D !

Re: back-to-back Barplot

Verfasst: Do Jan 03, 2019 8:01 pm
von alex
Athomas hat geschrieben: Do Jan 03, 2019 2:08 pm DF.long <- melt(DF.wide, id.vars="ID", variable.name="Spaltenname")[/code]
Wow, vielen Dank! Damit habe ich es geschafft! :D

Für alle, die mal ein ähnliches Problem haben, hier mein Code und im Anhang das Schaubild:

tkd<-read.table("Mappe3.csv", header=TRUE, sep=";", dec=",", na.strings=c("NA", " "))

# load packages
library(dplyr)
library(ggplot2)

###############
##############
# Spalten in lange Liste umwandeln

tkd.long <- melt(tkd, id.vars="id", variable.name="Gruppe")

plotting_tkd.long <-
tkd.long %>%
group_by(id, Gruppe) %>%
# a trick!
mutate(value = if_else(Gruppe == "B", -value, value))

p <-
plotting_tkd.long %>%
ggplot(aes(x = id, y = value, group = Gruppe, fill = Gruppe)) +
geom_bar(stat = "identity", width = 0.75) +
coord_flip() +
labs(x = "id", y = "Anzahl") +
theme(legend.position = "right",
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "grey90")) +
# Minus-Zeichen entfernen
scale_y_continuous(breaks=seq(-12000,12000,2000),labels=abs(seq(-12000,12000,2000)))


print(p)

Re: back-to-back Barplot

Verfasst: Do Jan 03, 2019 8:52 pm
von EDi
Btw:
reshape2 is retired: only changes necessary to keep it on CRAN will be made. We recommend using tidyr instead.
Also besser tidyr::gather nutzen...

Re: back-to-back Barplot

Verfasst: Fr Jan 04, 2019 9:24 am
von Athomas
Also besser tidyr::gather nutzen...
Oder noch besser gleich data.table!
Für unsere jüngeren Mitstreiter: ein data.table ist ein data.frame mit ein paar schönen, zusätzlichen Eigenschaften - die bei größeren Datenmengen eine teilweise unglaubliche Beschleunigung bewirken! Kurz gesagt:
It (data.table!) is an ideal package for dataset handing in R.

Code: Alles auswählen

library(data.table)
DT.wide <- data.table(ID=LETTERS[1:10], matrix(runif(50), ncol=5, dimnames=list(NULL, letters[1:5])))
DT.long <- melt(DT.wide, id.vars="ID", variable.name="Spaltenname")
Das "melt" ist in dieser Konstellation nicht mehr aus dem Package "reshape2" - das ja wohl in Zukunft vernachlässigt wird - sondern melt.data.table!