2017-04-18 2 views
1

meinen Code über Balkendiagramm Zeichnung ist hierProbleme in Zeichnen Sie ein Balkendiagramm mit ggplot2 unter Verwendung facet_grid Funktion

library(ggplot2) 

fig_num <- ggplot(data=branch.fig, aes(y=branch.fig$num, x=branch.fig$film.type, fill = branch.fig$film.type)) + 
    geom_bar(position=position_dodge(), stat="identity", width = 0.3, colour = "gray35") + 
    scale_fill_manual(values = colors) + 
    facet_grid(branch.type~.) + 
    geom_errorbar(aes(ymin=branch.fig$num, ymax=branch.fig$num +branch.fig$SE), position="dodge", width=0.1, col = "gray40") + 
    theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) + 
    labs(x = "Film Type",y = "Branch Number") + 
    ggtitle("DAS = 54") + coord_flip() 
fig_num 

Das Ergebnis sollte ich so sein zu wollen,

enter image description here

aber was ich bekomme ist so, die R kombinieren einen Teil der Daten, ich weiß nicht, warum das so erscheinen würde, es ist wirklich ein großes Problem für mich zu lösen, aufrichtig auf der Suche nach deiner Hilfe, vielen Dank!

enter image description here

finden Dataset

dput(subset(branch.fig)) 
structure(list(film.type = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L), .Label = c("black", "ck", "dark-gray", 
"green", "red", "white"), class = "factor"), branch.type = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("F", 
"V"), class = "factor"), num = c(14.6666666666667, 3.33333333333333, 
14, 2.66666666666667, 15.3333333333333, 2, 14.6666666666667, 
2.66666666666667, 16.6666666666667, 3.33333333333333, 20.3333333333333, 
1.66666666666667), SE = c(0.333333333333333, 0.333333333333333, 
1.15470053837925, 0.333333333333333, 0.881917103688197, 0, 0.881917103688197, 
0.666666666666667, 0.333333333333333, 0.666666666666667, 2.33333333333333, 
0.666666666666667), n = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L)), .Names = c("film.type", "branch.type", "num", "SE", 
"n"), row.names = c(NA, -12L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = list(film.type), drop = TRUE, indices = list(
    0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L, 
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
film.type = structure(1:6, .Label = c("black", "ck", "dark-gray", 
    "green", "red", "white"), class = "factor")), row.names = c(NA, 
-6L), class = "data.frame", vars = list(film.type), drop = TRUE, .Names = 
"film.type")) 
+1

Bitte geben Sie Ihre Daten in einer [reproduzierbaren Form] an (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Verknüpfen Sie keinen Screenshot Ihrer Daten, da andere dazu gezwungen werden müssten, diese zu transkribieren, bevor Sie mit Ihrem Problem helfen. –

+0

Vielen Dank für Ihre Erinnerung, ich habe meine Frage bearbeitet, um das Dataset darin zu kopieren ~ –

+0

Leider funktioniert die 'dput' Ausgabe nicht richtig, weil' dplyr' und 'dput' nicht ganz zurechtkommen. Versuchen Sie, die Ausgabe von 'dput (ungroup (branch.fig))' (von dieser Frage http://stackoverflow.com/questions/29039553/reproducible-example-and-dput-error) – bdemarest

Antwort

1

eine Reihe von Problemen mit Ihrem Code Es gibt keine.
1. Ihre dput hat einen Fehler generiert. Der folgende Befehl generiert die Daten.
2. Der Datenrahmen wird in data=branch.fig benannt. Keine Notwendigkeit, es wieder als Teil von aes() zu benennen.
3. Es gibt kein Ausweichen in diesem Diagramm, also lassen Sie die dodge Befehle sowohl in geom_bar als auch in geom_errorbar fallen.
4. Soweit ich sehen kann, gibt es kein colors Objekt. Ich habe einige Farben für die manuelle Farbskala eingestellt.

# Data 
branch.fig = structure(list(film.type = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L), .Label = c("black", "ck", "dark-gray", 
"green", "red", "white"), class = "factor"), branch.type = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("F", 
"V"), class = "factor"), num = c(14.6666666666667, 3.33333333333333, 
14, 2.66666666666667, 15.3333333333333, 2, 14.6666666666667, 
2.66666666666667, 16.6666666666667, 3.33333333333333, 20.3333333333333, 
1.66666666666667), SE = c(0.333333333333333, 0.333333333333333, 
1.15470053837925, 0.333333333333333, 0.881917103688197, 0, 0.881917103688197, 
0.666666666666667, 0.333333333333333, 0.666666666666667, 2.33333333333333, 
0.666666666666667), n = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L)), .Names = c("film.type", "branch.type", "num", "SE", 
"n"), row.names = c(NA, -12L), class = "data.frame") 


library(ggplot2) 

# Plot 
fig_num = ggplot(data = branch.fig, aes(y = num, x = film.type, fill = film.type)) + 
    geom_bar(stat = "identity", width = 0.3, colour = "gray35") + 
    scale_fill_manual(values = c("black", "orange", "grey", "green", "red", "white")) + 
    facet_grid(branch.type ~ .) + 
    geom_errorbar(aes(ymin = num, ymax = num + SE), width = 0.1, col = "gray40") + 
    theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) + 
    labs(x = "Film Type", y = "Branch Number") + 
    ggtitle("DAS = 54") + coord_flip() 

fig_num 
+0

Vielen Dank für die Antwort! Das Problem vielleicht, weil ich den Datenrahmen in meinem Code wiederholte, kam die Figur zurück, als ich den Code in aes() ~ änderte –

Verwandte Themen