2012-08-11 13 views
5

Sto provando a creare un grafico a barre in pila usando ggplot 2. I miei dati nella sua forma ampia, assomigliano a questo. I numeri in ogni cella sono la frequenza delle risposte.Come creare un grafico a barre in pila da dati riepilogati in ggplot2

activity       yes no dontknow 
Social events      27 3 3 
Academic skills workshops   23 5 8 
Summer research     22 7 7 
Research fellowship    20 6 9 
Travel grants      18 8 7 
Resume preparation    17 4 12 
RAs        14 11 8 
Faculty preparation    13 8 11 
Job interview skills    11 9 12 
Preparation of manuscripts  10 8 14 
Courses in other campuses   5 11 15 
Teaching fellowships    4 14 16 
TAs        3 15 15 
Access to labs in other campuses 3 11 18 
Interdisciplinary research   2 11 18 
Interdepartamental projects  1 12 19 

ho sciolto questa tabella utilizzando reshape2 e

melted.data(wide.data,id.vars=c("activity"),measure.vars=c("yes","no","dontknow"),variable.name="haveused",value.name="responses") 

Questo è quanto posso ottenere. Voglio creare un grafico a barre in pila con le attività sull'asse x, frequenza di risposte nel dell'asse y, e ogni barra che mostra la distribuzione dei sì, nos e dontknows

Ho provato

ggplot(melted.data,aes(x=activity,y=responses))+geom_bar(aes(fill=haveused)) 

ma temo che non sia la soluzione giusta

Qualsiasi aiuto è molto apprezzato.

risposta

5

Non hai detto cosa non è giusto per la tua soluzione. Ma alcuni problemi che potrebbero essere interpretati come problemi e una possibile soluzione per ciascuno sono:

  • Le etichette del segno di spunta dell'asse x si susseguono. SOLUZIONE - ruotare le etichette dei segni di graduazione;
  • L'ordine in cui vengono visualizzate le etichette (e le relative barre corrispondenti) non sono uguali a quelle dell'ordine nel dataframe originale. SOLUZIONE: riordinare i livelli del fattore "attività";
  • Per posizionare testo all'interno delle barre impostare il parametro vjust in position_stack a 0,5

Di seguito potrebbe essere un inizio.

# Load required packages 
library(ggplot2) 
library(reshape2) 

    # Read in data 
df = read.table(text = " 
activity       yes no dontknow 
Social.events      27 3 3 
Academic.skills.workshops   23 5 8 
Summer.research     22 7 7 
Research.fellowship    20 6 9 
Travel.grants      18 8 7 
Resume.preparation    17 4 12 
RAs        14 11 8 
Faculty.preparation    13 8 11 
Job.interview.skills    11 9 12 
Preparation.of.manuscripts  10 8 14 
Courses.in.other.campuses   5 11 15 
Teaching.fellowships    4 14 16 
TAs        3 15 15 
Access.to.labs.in.other.campuses 3 11 18 
Interdisciplinay.research   2 11 18 
Interdepartamental.projects  1 12 19", header = TRUE, sep = "") 

    # Melt the data frame 
dfm = melt(df, id.vars=c("activity"), measure.vars=c("yes","no","dontknow"), 
    variable.name="haveused", value.name="responses") 

    # Reorder the levels of activity 
dfm$activity = factor(dfm$activity, levels = df$activity) 

    # Draw the plot 
ggplot(dfm, aes(x = activity, y = responses, group = haveused)) + 
geom_col(aes(fill=haveused)) + 
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + 
geom_text(aes(label = responses), position = position_stack(vjust = .5), size = 3) # labels inside the bar segments 
Problemi correlati