में किंवदंती शीर्षक और लेबल के बीच की जगह बढ़ाएं मैं ggplot2
में किंवदंती शीर्षक और लेबल के बीच की जगह को बढ़ाने की कोशिश कर रहा हूं लेकिन वेब पर मिलने वाले सभी संभावित समाधानों को लागू करने में कोई भाग्य नहीं है। जैसा कि आप शीर्षक पाठ के नीचे पुनरुत्पादित उदाहरण में देख सकते हैं शीर्ष संख्या के बहुत करीब है। मैं मैन्युअल रूप से लाइन ब्रेक (\n
) जोड़ने के क्लंकी समाधान (# 6 नीचे) से बचना चाहता हूं क्योंकि यह अंतरण आकार को ट्विक करने की अनुमति नहीं देता है और मेरे मामले में एक पूर्ण रेखा बहुत अधिक है।ggplot2
मुझे colourbar
का उपयोग करने की आवश्यकता है। मुझे पता है कि vjust
आमतौर पर 0 और 1 के बीच मान लेता है लेकिन परिवर्तनों को आसानी से पहचानने के लिए मैं नीचे 2 का मान उपयोग कर रहा हूं। कथा का Grob संरचना में
library(reshape2)
library(ggplot2)
# Generate test data frame
df=reshape2::melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
# Declare theme
mytheme=theme_classic(base_size=15) +
theme(axis.title.x=element_blank(),axis.title.y=element_blank(),
axis.text.x=element_blank(),axis.text.y=element_blank(),
axis.ticks=element_blank()) +
theme(legend.position=c(0,1), legend.justification=c(0,1),
legend.title=element_text(size="12",face = "bold"))
# Plot
p=ggplot(data=df, aes_string(x="X1", y="X2")) +
geom_tile(aes(fill=value))+
scale_fill_gradient(low="yellow",high="red",guide="colourbar",name="Titleggplot") +
annotate("text",x=Inf,y=Inf,label="(a)" ,hjust=1.5, vjust=1.5, size=6) +
mytheme
p
#*** Things I tried (building on the defaults above) that do not work
# 1 - set "vjust" in theme
mytheme=mytheme+theme(legend.title=element_text(size="12",face = "bold",vjust=2))
p=p+mytheme
p
# Result: does nothing
# 2 - set "legend.title.align" in theme
mytheme=mytheme+theme(legend.title.align=4)
p=p+mytheme
p
# Result: adjusts horizontal position but does not change vertical position
# 3 - increase margins around title object
mytheme=mytheme+theme(legend.title=element_text(margin=margin(0,0,20,0),size="12",face="bold"))
p=p+mytheme
p
# Result: does nothing
# 4 - using "guide" in scale_fill_gradient
p=ggplot(data=df, aes_string(x="X1", y="X2")) +
geom_tile(aes(fill=value))+
scale_fill_gradient(low="yellow",high="red",guide=guide_colorbar(title="Titleggplot",title.vjust=2)) +
annotate("text",x=Inf,y=Inf,label="(a)" ,hjust=1.5, vjust=1.5, size=6) +
mytheme
p
# Result: does nothing
# 5 - using "guides" as separate element
p=p+guides(fill=guide_legend(title.vjust=2))
# Restult: does nothing
# 6 - I could manually add a line break (\n) to the title
p=ggplot(data=df, aes_string(x="X1", y="X2")) +
geom_tile(aes(fill=value))+
scale_fill_gradient(low="yellow",high="red",guide="colourbar",name="Titleggplot\n") +
annotate("text",x=Inf,y=Inf,label="(a)" ,hjust=1.5, vjust=1.5, size=6) +
mytheme
p
# Result: increases the space but I can't smoothly adjust the spacing and an entire blank line is in my case too much.
मैं 'element_text' काम हो सकता है में एक' margin' को परिभाषित करने में सोचा है, लेकिन ऐसा नहीं है। शायद एक बग? – Roland
@ रोलैंड अच्छा सुझाव! मैंने सफलता के बिना पहले भी कोशिश की लेकिन उपरोक्त मेरे विकल्पों में मार्जिन को विस्तारित करना शामिल करना भूल गया - मैंने इसे अभी जोड़ा। – Raphael
इस मुद्दे से संबंधित कम से कम दो गिथब अनुरोध हैं (किंवदंती में मार्जिन निर्दिष्ट करना)। हाल ही में, पैकेज के मुख्य लेखक ने कहा कि यह एक उच्च प्राथमिकता नहीं थी। शायद आप उनमें से एक पर टिप्पणी कर सकते हैं। – lmo