2012-02-07 28 views
5

के साथ व्यक्तिगत सलाखों पर संख्याओं का संरेखण मैं निम्नलिखित ग्राफ में व्यक्तियों के सलाखों पर संख्याएं दिखाना चाहता हूं।ggplot2

df <- structure(list(A = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 
3L), .Label = c("0-50,000", "50,001-250,000", "250,001-Over"), class = "factor"), 
    B = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("0-50,000", 
    "50,001-250,000", "250,001-Over"), class = "factor"), Freq = c(0.507713884992987, 
    0.258064516129032, 0.23422159887798, 0.168539325842697, 0.525280898876405, 
    0.306179775280899, 0.160958904109589, 0.243150684931507, 
    0.595890410958904)), .Names = c("A", "B", "Freq"), class = "data.frame", row.names = c(NA, 
-9L)) 

library(ggplot2) 
p <- ggplot(data=df, aes(x=A, y=Freq))+ 
      geom_bar(aes(fill=B), position=position_dodge()) + theme_bw() 
p <- p + scale_y_continuous(formatter="percent") 
p <- p + geom_text(aes(label = paste(sprintf("%.1f", Freq*100), "%", sep=""), y = Freq+0.015, x=A), size = 3) 
print(p) 

enter image description here

मैं समझ नहीं सकता कैसे x- अक्ष सही स्थिति में संख्या डाल करने के लिए। किसी भी मदद की अत्यधिक सराहना की जाएगी। धन्यवाद

उत्तर

8

आपको geom_text कॉल में position=position_dodge(width=0.9) जोड़ने की आवश्यकता है।

अपने कोड साफ़ एक छोटे से देता है:

p <- ggplot(data=df, aes(x=A, y=Freq))+ 
    geom_bar(aes(fill=B), position = position_dodge()) + 
    geom_text(aes(label = paste(sprintf("%.1f", Freq*100), "%", sep=""), 
        y = Freq+0.015, x=A), 
       size = 3, position = position_dodge(width=0.9)) + 
    scale_y_continuous(formatter = "percent") + 
    theme_bw() 

जो

enter image description here

+0

(+1) में जो परिणाम: मददगार जवाब के लिए धन्यवाद ब्रायन। – MYaseen208

+1

@ MYaseen208 डेटा सहित पूरी तरह से पुन: उत्पन्न प्रश्न के लिए धन्यवाद। जवाब देता है (ठीक है, यह जांच रहा है कि मेरा जवाब सही था) बहुत आसान है। –

+1

ggplot2 0.9.0 के लिए इस उत्तर में अद्यतन http://stackoverflow.com/a/10113480/892313 पर है –

 संबंधित मुद्दे