2012-08-11 9 views
5

में संक्षेप में डेटा से स्टैक्ड बार चार्ट कैसे बनाएं I ggplot 2 का उपयोग करके एक स्टैक्ड बार ग्राफ़ बनाने की कोशिश कर रहा हूं। मेरा डेटा इसके विस्तृत रूप में दिखता है, ऐसा लगता है। प्रत्येक सेल में संख्या प्रतिक्रियाओं की आवृत्ति होती है।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 

मैं reshape2 और

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

जहाँ तक मैं प्राप्त कर सकते हैं यही कारण है कि का उपयोग कर इस तालिका पिघलाया जाता है। मैं एक्स अक्ष पर गतिविधियों, y अक्ष में प्रतिक्रियाओं की आवृत्ति, और हाँ के वितरण दिखा प्रत्येक बार, ओपन स्कूल के साथ एक स्टैक्ड बार चार्ट बनाना चाहते हैं और

मैं

ggplot(melted.data,aes(x=activity,y=responses))+geom_bar(aes(fill=haveused)) 
की कोशिश की है dontknows

लेकिन मुझे डर है कि सही समाधान नहीं है

किसी भी मदद की बहुत सराहना की जाती है।

उत्तर

5

आपने यह नहीं कहा है कि यह आपके समाधान के बारे में सही नहीं है। लेकिन कुछ मुद्दों को समस्या के रूप में समझा जा सकता है, और प्रत्येक के लिए एक संभावित समाधान है:

  • एक्स अक्ष टिक मार्क लेबल एक-दूसरे में चलते हैं। समाधान - टिक मार्क लेबल घुमाएं;
  • ऑर्डर जिसमें लेबल (और उनके संबंधित बार) दिखाई देते हैं, मूल डेटाफ़्रेम में ऑर्डर के समान नहीं होते हैं। समाधान - कारक 'गतिविधि' के स्तर को पुन: व्यवस्थित करें; अंदर सलाखों 0,5

करने के लिए position_stack में vjust पैरामीटर सेट निम्नलिखित एक शुरुआत हो सकता है

  • पाठ की स्थिति के लिए।

    # 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