2012-05-25 17 views
43

समस्या यहां थोड़ा स्पष्ट है। मैं 'प्लॉटिंग क्षेत्र' के ऊपरी बाएं कोने में पौराणिक कथा (लॉक) रखना चाहता हूं। सी (0.1,0.13) आदि का उपयोग कई कारणों से एक विकल्प नहीं है।लीजेंड प्लेसमेंट, ggplot, साजिश क्षेत्र के सापेक्ष

क्या समन्वय के लिए संदर्भ बिंदु बदलने का कोई तरीका है ताकि वे साजिश क्षेत्र से संबंधित हों?

mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight")) 
ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.position = c(0, 1), title="Legend placement makes me sad") 

enter image description here

चीयर्स

+0

"साजिश क्षेत्र" से आपका क्या मतलब है? वह क्षेत्र जो भूरे रंग से भरा हुआ है? – kohske

+0

@ कोहस्के हां, ओपी मूल रूप से "डेटा क्षेत्र", या भूरे रंग के क्षेत्र के कोने में पौराणिक कथाओं को रखने के लिए 'legend.position' के लिए सही निर्देशांक निर्धारित करने में सक्षम होना चाहता है।जैसा कि मैंने चैट में उल्लेख किया है, मुझे संदेह है कि आपके जैसे किसी को ग्रिड समाधान के साथ वजन करना होगा। – joran

+0

@joran तो, यह डिफ़ॉल्ट व्यवहार है और आपको केवल सही औचित्य निर्धारित करना है। – kohske

उत्तर

42

अद्यतन: opts मान्य नहीं है। theme का उपयोग करें के बजाय, के रूप में वर्णित in this answer.

गाइड की नियुक्ति साजिश क्षेत्र के आधार पर है (यानी, क्षेत्र ग्रे द्वारा भरा) डिफ़ॉल्ट रूप से, लेकिन औचित्य केंद्रित है।

p <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
    opts(legend.position = c(0, 1), 
     legend.justification = c(0, 1), 
     legend.background = theme_rect(colour = "black"), 
     title="Legend placement makes me happy") 

gt <- ggplot_gtable(ggplot_build(p)) 
nr <- max(gt$layout$b) 
nc <- max(gt$layout$r) 
gb <- which(gt$layout$name == "guide-box") 
gt$layout[gb, 1:4] <- c(1, 1, nr, nc) 
grid.newpage() 
grid.draw(gt) 

:

ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
    opts(legend.position = c(0, 1), 
     legend.justification = c(0, 1), 
     legend.background = theme_rect(colour = NA, fill = "white"), 
     title="Legend placement makes me happy") 

enter image description here

आप पूरे डिवाइस क्षेत्र के खिलाफ गाइड रखना चाहते हैं, तो आप gtable उत्पादन tweak कर सकते हैं: तो तुम बाएं शीर्ष औचित्य स्थापित करने की आवश्यकता enter image description here

+0

धन्यवाद, यह 'हार्ड टॉप बाएं' नहीं है लेकिन दोनों 'किनारों' से दूर दूर होने वाला होगा :) – nzcoops

+13

ऑप्ट्स को बहिष्कृत कर दिया गया है। इसके बजाय विषय का प्रयोग करें। – papirrin

+0

यह एक त्रुटि उत्पन्न करता है: 'फ़ंक्शन नहीं मिला "opts"'। –

56

अद्यतन: opts को हटा दिया गया है। के रूप में वर्णित in this answer.

बस kohske के जवाब पर विस्तार करने के लिए बजाय theme का उपयोग करें, तो इसके लिए अगले व्यक्ति इस पर आते हैं करने के लिए थोड़ा और अधिक व्यापक है।

mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight")) 
library(gridExtra) 

a <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(0, 1), legend.position = c(0, 1), title="Legend is top left") 
b <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(1, 0), legend.position = c(1, 0), title="Legend is bottom right") 
c <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(0, 0), legend.position = c(0, 0), title="Legend is bottom left") 
d <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(1, 1), legend.position = c(1, 1), title="Legend is top right") 

grid.arrange(a,b,c,d) 

enter image description here

33

मैं इसी तरह जवाब की तलाश में की है। लेकिन पाया कि opts फ़ंक्शन अब ggplot2 पैकेज का हिस्सा नहीं है। कुछ और समय खोजने के बाद, मैंने पाया कि एक विकल्प के रूप में समान काम करने के लिए theme का उपयोग कर सकते हैं। इसलिए इस धागे को संपादित करना, ताकि दूसरों को कम करने के लिए।

नीचे समान कोड है जैसा कि nzcoops द्वारा लिखा गया है।

mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight")) 
library(gridExtra) 

a <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is top left") + 
theme(legend.justification = c(0, 1), legend.position = c(0, 1)) 

b <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is bottom right") + 
theme(legend.justification = c(1, 0), legend.position = c(1, 0)) 

c <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is bottom left") + 
theme(legend.justification = c(0, 0), legend.position = c(0, 0)) 

d <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is top right") + 
theme(legend.justification = c(1, 1), legend.position = c(1, 1)) 

grid.arrange(a,b,c,d) 

यह कोड बिल्कुल समान साजिश देगा।

4

ऊपर excellend जवाब पर विस्तार के लिए, आपको कथा और बॉक्स के बाहर के बीच गद्दी जोड़ना चाहते हैं, legend.box.margin का उपयोग करें:

# Positions legend at the bottom right, with 50 padding 
# between the legend and the outside of the graph. 
theme(legend.justification = c(1, 0), 
    legend.position = c(1, 0), 
    legend.box.margin=margin(c(50,50,50,50))) 

यह ggplot2 के नवीनतम संस्करण को जो v2 है पर काम करता है। लिखने के समय 2.1।

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