ggplot geom_text फ़ॉन्ट आकार नियंत्रण


93

मैंने ggplot2कुछ इस तरह से अपने बार प्लॉट के लेबल के लिए फ़ॉन्ट को 10 में बदलने की कोशिश की :

ggplot(data=file,aes(x=V1,y=V3,fill=V2)) +
    geom_bar(stat="identity",position="dodge",colour="white") + 
    geom_text(aes(label=V2),position=position_dodge(width=0.9),
                                                 hjust=1.5,colour="white") +
    theme_bw()+theme(element_text(size=10))

ggsave(filename="barplot.pdf",width=4,height=4)

लेकिन परिणामी छवि में बार प्लॉट लेबल के लिए सुपर बिग फ़ॉन्ट आकार है।

फिर मैंने इसके geom_text()साथ संशोधन करने का सोचा :

geom_text(size=10,aes(label=V2),position=position_dodge(width=0.9),
                                                   hjust=1.5,colour="white")

लेबल फ़ॉन्ट और भी बड़ा है ...

मैं आकार geom_textको 3 की तरह कुछ में बदल सकता हूं और अब यह अक्ष लेबल के समान फ़ॉन्ट 10 जैसा दिखता है।

मैं सोच रहा हूँ क्या चल रहा है? क्या theme(text=element_text(size=10))लेबल पर लागू नहीं होता है?

और 10 का आकार इसमें geom_text()से भिन्न क्यों है theme(text=element_text())?

जवाबों:


141

पाठ / लेबल आकार बदलने के लिए यहां कुछ विकल्प दिए गए हैं

library(ggplot2)

# Example data using mtcars

a <- aggregate(mpg ~ vs + am , mtcars, function(i) round(mean(i)))

p <- ggplot(mtcars, aes(factor(vs), y=mpg, fill=factor(am))) + 
            geom_bar(stat="identity",position="dodge") + 
            geom_text(data = a, aes(label = mpg), 
                            position = position_dodge(width=0.9),  size=20)

sizeमें geom_textके आकार में परिवर्तन geom_textलेबल।

p <- p + theme(axis.text = element_text(size = 15)) # changes axis labels

p <- p + theme(axis.title = element_text(size = 25)) # change axis titles

p <- p + theme(text = element_text(size = 10)) # this will change all text size 
                                                             # (except geom_text)


इसके लिए और क्यों geom_text () में 10 का आकार थीम (पाठ = element_text ()) से अलग है?

हां, वे अलग हैं। मैंने एक त्वरित मैनुअल जांच की और वे geom_textआकार के themeआकार के लिए ~ (14/5) के अनुपात में प्रतीत होते हैं।

तो इस अनुपात से समान आकार के लिए एक भयानक निर्धारण है

geom.text.size = 7
theme.size = (14/5) * geom.text.size

ggplot(mtcars, aes(factor(vs), y=mpg, fill=factor(am))) + 
  geom_bar(stat="identity",position="dodge") + 
  geom_text(data = a, aes(label = mpg), 
            position = position_dodge(width=0.9),  size=geom.text.size) + 
  theme(axis.text = element_text(size = theme.size, colour="black")) 

इस पाठ्यक्रम की व्याख्या क्यों नहीं करता है ? और एक चिता है (और मुझे लगता है कि ऐसा करने के लिए एक अधिक समझदार तरीका है)


2
दिलचस्प है, आपने 14/5 अनुपात का पता लगाने के लिए क्या जाँच की थी?
— ओलला जूल

34
समझा। आप मुझे कुछ याद दिलाते हैं जो मैंने हाल ही में पढ़ा है, मुझे लगता है कि यह इकाइयों में अंतर है, 5 का geom_text डिफ़ॉल्ट 5 मिमी हो सकता है और थीम () आकार इकाई बिंदु है। 1 अंक 1/72 इंच = 0.35 मिमी है, इसलिए 1 में geom_text () 1 मिमी, 1 / 0.35 = ~ 14/5 :)
— ओलला जूल

3
agstudy का जवाब क्यों वर्णन करता stackoverflow.com/questions/17311917/ggplot2-the-unit-of-size
— user20650
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.