Ich habe eine Frage zum Füllfeld in geom_bar
des ggplot2-Pakets.bestellen und füllen mit 2 verschiedenen Variablen geom_bar ggplot2 R
Ich mag würde meinen geom_bar mit einer Variablen (im folgende Beispiel wird die Variable aufgerufen wird var_fill
) füllen, aber die geom_plot
mit einer anderen Variablen (genannt clarity
im Beispiel) bestellen.
Wie kann ich das tun?
Vielen Dank!
Das Beispiel:
rm(list=ls())
set.seed(1)
library(dplyr)
data_ex <- diamonds %>%
group_by(cut, clarity) %>%
summarise(count = n()) %>%
ungroup() %>%
mutate(var_fill= LETTERS[sample.int(3, 40, replace = TRUE)])
head(data_ex)
# A tibble: 6 x 4
cut clarity count var_fill
<ord> <ord> <int> <chr>
1 Fair I1 210 A
2 Fair SI2 466 B
3 Fair SI1 408 B
4 Fair VS2 261 C
5 Fair VS1 170 A
6 Fair VVS2 69 C
ich diese Bestellung der Boxen möchte [Klarheit]:
library(ggplot2)
ggplot(data_ex) +
geom_bar(aes(x = cut, y = count, fill=clarity),stat = "identity", position = "fill", color="black")
mit dieser Füllung (Farbe) der Kästen [var_fill] :
ggplot(data_ex) +
geom_bar(aes(x = cut, y = count, fill=var_fill),stat = "identity", position = "fill", color="black")
EDIT1: Antwort von mißbrauchen gefunden:
p1 <- ggplot(data_ex) + geom_bar(aes(x = cut, y = count, group = clarity, fill = var_fill), stat = "identity", position = "fill", color="black")+ ggtitle("var fill")
p2 <- ggplot(data_ex) + geom_bar(aes(x = cut, y = count, fill = clarity), stat = "identity", position = "fill", color = "black")+ ggtitle("clarity")
library(cowplot)
cowplot::plot_grid(p1, p2)
EDIT2: Jetzt habe ich versucht, dies mit Hilfe von mißbrauchen mit ggmosaic Erweiterung zu tun
rm(list=ls())
set.seed(1)
library(ggplot2)
library(dplyr)
library(ggmosaic)
data_ex <- diamonds %>%
group_by(cut, clarity) %>%
summarise(count = n()) %>%
ungroup() %>%
mutate(residu= runif(nrow(.), min=-4.5, max=5)) %>%
mutate(residu_classe = case_when(residu < -4~"< -4 (p<0.001)",(residu >= -4 & residu < -2)~"[-4;-2[ (p<0.05)",(residu >= -2 & residu < 2)~"[-2;2[ non significatif",(residu >= 2 & residu < 4)~"[2;4[ (p<0.05)",residu >= 4~">= 4 (p<0.001)")) %>%
mutate(residu_color = case_when(residu < -4~"#D04864",(residu >= -4 & residu < -2)~"#E495A5",(residu >= -2 & residu < 2)~"#CCCCCC",(residu >= 2 & residu < 4)~"#9DA8E2",residu >= 4~"#4A6FE3"))
ggplot(data_ex) +
geom_mosaic(aes(weight= count, x=product(clarity, cut)), fill = data_ex$residu_color, na.rm=T)+
scale_y_productlist() +
theme_classic() +
theme(axis.ticks=element_blank(), axis.line=element_blank())+
labs(x = "cut",y="clarity")
Aber ich möchte diese Legende hinzufügen (unten) auf der rechten Seite der Handlung, aber ich weiß nicht, wie ich es tun konnte, weil die Füllung Feld außerhalb aes ist so scale_fill_manual funktioniert nicht ...
Danke, aber ich denke, Ordnung oder abgeschrieben werden wird? es funktioniert nicht mit mir mit der tatsächlichen Version von ggplot2 auf github – antuki
sicherlich, dev_tools Version könnte das haben, aber Sie können auch Versionen zurück, wenn Sie nur diese benötigen (google ist dein Freund auf Versionen zurück) – ike