fragte ich diese gleiche Frage, aber ich wollte nur data.table
verwenden, da es eine schnellere Lösung für die viel größere Datenmengen ist. Ich habe Notizen zu den Daten beigefügt, so dass diejenigen, die weniger erfahren sind und verstehen wollen, warum ich getan habe, was ich getan habe, dies leicht tun können. Hier ist, wie ich die mtcars
Datensatz manipuliert:
library(data.table)
library(scales)
library(ggplot2)
mtcars <- data.table(mtcars)
mtcars$Cylinders <- as.factor(mtcars$cyl) # Creates new column with data from cyl called Cylinders as a factor. This allows ggplot2 to automatically use the name "Cylinders" and recognize that it's a factor
mtcars$Gears <- as.factor(mtcars$gear) # Just like above, but with gears to Gears
setkey(mtcars, Cylinders, Gears) # Set key for 2 different columns
mtcars <- mtcars[CJ(unique(Cylinders), unique(Gears)), .N, allow.cartesian = TRUE] # Uses CJ to create a completed list of all unique combinations of Cylinders and Gears. Then counts how many of each combination there are and reports it in a column called "N"
Und hier ist der Anruf, der die Grafik erzeugt
ggplot(mtcars, aes(x=Cylinders, y = N, fill = Gears)) +
geom_bar(position="dodge", stat="identity") +
ylab("Count") + theme(legend.position="top") +
scale_x_discrete(drop = FALSE)
Und es erzeugt diese Grafik:
Außerdem wenn es kontinuierliche Daten gibt, so wie im diamonds
Datensatz (dank mnel):
library(data.table)
library(scales)
library(ggplot2)
diamonds <- data.table(diamonds) # I modified the diamonds data set in order to create gaps for illustrative purposes
setkey(diamonds, color, cut)
diamonds[J("E",c("Fair","Good")), carat := 0]
diamonds[J("G",c("Premium","Good","Fair")), carat := 0]
diamonds[J("J",c("Very Good","Fair")), carat := 0]
diamonds <- diamonds[carat != 0]
Dann mit CJ
würde auch funktionieren.
data <- data.table(diamonds)[,list(mean_carat = mean(carat)), keyby = c('cut', 'color')] # This step defines our data set as the combinations of cut and color that exist and their means. However, the problem with this is that it doesn't have all combinations possible
data <- data[CJ(unique(cut),unique(color))] # This functions exactly the same way as it did in the discrete example. It creates a complete list of all possible unique combinations of cut and color
ggplot(data, aes(color, mean_carat, fill=cut)) +
geom_bar(stat = "identity", position = "dodge") +
ylab("Mean Carat") + xlab("Color")
uns diese Grafik Giving:
nicht so einfach, wie ich hatte gehofft, konnte aber keine angemessene Antwort auf meiner Suche finden, so sollte ich es einige Nacharbeiten würde herausgefunden haben. Danke Joran. Ehrlich gesagt hat sehr gut geklappt +1 –
@TylerRinker, ich fühle mich wie 'stat_bin (drop = FALSE, geom = "bar", position = "ausweichen", ...)' _should_ dies tun; zumindest deutet die Dokumentation stark darauf hin, dass dies der Fall wäre. Ich wäre sehr neugierig, von erfahreneren Leuten auf der Mailingliste zu hören, warum es nicht so ist. – joran
Ich arbeite gerade an einem Projekt, aber ich werde es später auf die Liste werfen und hier berichten. –