2016-12-07 11 views
6

मैं किंवदंती शीर्षक sex किंवदंती बॉक्स के क्षैतिज केंद्र के लिए थोड़ा सा स्थान ले जाना चाहता हूं। मैंने theme और guide_legend की कोशिश की लेकिन असफल रहा। दोनों तरीकों से किंवदंती शीर्षक की स्थिति नहीं बदलेगी।ggplot2 में किंवदंती बॉक्स के बीच में किंवदंती शीर्षक को कैसे संरेखित करना है?

# example data from http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/ 
df1 <- data.frame(
    sex = factor(c("Female","Female","Male","Male")), 
    time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")), 
    total_bill = c(13.53, 16.81, 16.24, 17.42) 
) 

library(ggplot2) 
p <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + 
    geom_line() + geom_point() 

# no change 
p + theme(legend.title = element_text(hjust = 0.5)) 
p + guides(color=guide_legend(title.hjust=0.5)) 

इसके अतिरिक्त, मैं ggplot2_2.2.0 का उपयोग कर रहा हूं।

उत्तर

6

आप legend.title.align बजाय legend.title की जरूरत है:

p + theme(legend.title.align=0.5) 

enter image description here

+0

धन्यवाद। यह मेरी समस्या हल हो गई। – mt1022

+2

जाहिर है यह [लंबी किंवदंती शीर्षकों] के लिए काम नहीं करता है। (Https://stackoverflow.com/questions/48000292/center-align-legend-title-and-legend-keys-in-ggplot2-for-long-legend -titles) –

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