2012-04-04 40 views
28

द्वारा उत्पन्न साजिश के नीचे पाठ प्रदर्शित करना मैं ggplot2 में बनाई गई साजिश के नीचे डेटा के बारे में कुछ जानकारी प्रदर्शित करने का प्रयास कर रहा हूं। मैं प्लॉट के एक्स अक्ष समन्वय का उपयोग करके एन वैरिएबल प्लॉट करना चाहता हूं लेकिन वाई समन्वय स्क्रीन के नीचे से 10% होना चाहिए। वास्तव में, वांछित वाई निर्देशांक पहले ही डेटा फ्रेम में y_pos चर के रूप में हैं।ggplot2

1) वास्तविक भूखंड नीचे एक खाली भूखंड बनाएँ, एक ही पैमाने का उपयोग करें और उसके बाद खाली भूखंड पर डेटा प्लॉट करने के लिए geom_text का उपयोग करें:

मैं ggplot2 का उपयोग कर 3 दृष्टिकोण के बारे में सोच सकते हैं। This approach काम के प्रकार लेकिन बेहद जटिल है।

2) डेटा प्लॉट करने के लिए geom_text का उपयोग करें, लेकिन किसी भी तरह स्क्रीन के प्रतिशत के रूप में वाई समन्वय का उपयोग करें (10%)। यह संख्याओं को साजिश के नीचे प्रदर्शित करने के लिए मजबूर करेगा। मैं उचित वाक्यविन्यास नहीं समझ सकता।

3) टेक्स्ट प्रदर्शित करने के लिए grid.text का उपयोग करें। मैं आसानी से स्क्रीन के नीचे से 10% पर सेट कर सकता हूं लेकिन मुझे यह नहीं पता कि साजिश से मेल खाने के लिए एक्स समन्वय कैसे सेट किया गया है। मैंने शुरुआती एक्स स्थिति को पकड़ने के लिए grconvert का उपयोग करने की कोशिश की लेकिन वह काम करने के लिए भी नहीं मिल सका।

graphics.off()  # close graphics windows 

library(car) 
library(ggplot2) #load ggplot 
library(gridExtra) #load Grid 
library(RGraphics) # support of the "R graphics" book, on CRAN 

#create dummy data 
test= data.frame(
    Group = c("A", "B", "A","B", "A", "B"), 
    x = c(1 ,1,2,2,3,3), 
    y = c(33,25,27,36,43,25), 
    n=c(71,55,65,58,65,58), 
    y_pos=c(9,6,9,6,9,6) 
) 

#create ggplot 
p1 <- qplot(x, y, data=test, colour=Group) + 
    ylab("Mean change from baseline") + 
    geom_line()+ 
    scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1)) + 
    opts( 
     legend.position=c(.1,0.9)) 

#display plot 
p1 

विषयों की संख्या के नीचे स्थित संशोधित gplot, लेकिन वे साजिश में प्रदर्शित होती हैं:

नीचे डमी डेटा के साथ मूल कथानक है। वे वाई पैमाने को विस्तारित करने के लिए मजबूर करते हैं। मैं साजिश के नीचे इन नंबरों को प्रदर्शित करना चाहता हूं।

p1 <- qplot(x, y, data=test, colour=Group) + 
    ylab("Mean change from baseline") + 
    geom_line()+ 
    scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1)) + 
    opts(plot.margin = unit(c(0,2,2,1), "lines"), 
     legend.position=c(.1,0.9))+ 
    geom_text(data = test,aes(x=x,y=y_pos,label=n)) 

p1 

संख्याओं को प्रदर्शित करने का एक अलग दृष्टिकोण वास्तविक साजिश के नीचे एक डमी प्लॉट बनाना शामिल है। यहां कोड है:

graphics.off()  # close graphics windows 

library(car) 
library(ggplot2) #load ggplot 
library(gridExtra) #load Grid 
library(RGraphics) # support of the "R graphics" book, on CRAN 

#create dummy data 
test= data.frame(
    group = c("A", "B", "A","B", "A", "B"), 
    x = c(1 ,1,2,2,3,3), 
    y = c(33,25,27,36,43,25), 
    n=c(71,55,65,58,65,58), 
    y_pos=c(15,6,15,6,15,6) 
) 


p1 <- qplot(x, y, data=test, colour=group) + 
    ylab("Mean change from baseline") + 
    opts(plot.margin = unit(c(1,2,-1,1), "lines")) + 
    geom_line()+ 
    scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1)) + 
    opts(legend.position="bottom", 
     legend.title=theme_blank(), 
     title.text="Line plot using GGPLOT") 
p1 

p2 <- qplot(x, y, data=test, geom="blank")+ 
    ylab(" ")+ 
    opts( plot.margin = unit(c(0,2,-2,1), "lines"), 
      axis.line = theme_blank(), 
      axis.ticks = theme_segment(colour = "white"),   
      axis.text.x=theme_text(angle=-90,colour="white"), 
      axis.text.y=theme_text(angle=-90,colour="white"), 
      panel.background = theme_rect(fill = "transparent",colour = NA), 
      panel.grid.minor = theme_blank(),  
      panel.grid.major = theme_blank() 
      )+ 
    geom_text(data = test,aes(x=x,y=y_pos,label=n)) 
p2 

grid.arrange(p1, p2, heights = c(8.5, 1.5),    nrow=2) 
enter code here 

हालांकि, यह बहुत जटिल है और विभिन्न डेटा के लिए संशोधित करना मुश्किल होगा। आदर्श रूप में, मैं स्क्रीन के प्रतिशत के रूप में वाई निर्देशांक पास करने में सक्षम होना चाहता हूं। अग्रिम धन्यवाद!

+0

annotation_custom() और कोड [यहां से] (http://stackoverflow.com/questions/9690648/point-clipped-on-x-axis-in-ggplot) आपकी मदद कर सकता है। निचे देखो। –

+0

नीचे मेरा जवाब देखें। स्वीकार्य उत्तर से यह निश्चित रूप से सरल है। –

+0

ggplot2 के हाल के संस्करणों में अब यह कार्यक्षमता नीचे दिए गए उत्तर के अनुसार बनाई गई है: http://stackoverflow.com/a/36036479/170352 –

उत्तर

18

opts संपादित पदावनत किया गया है, theme द्वारा बदल दिया; element_blank ने theme_blank को बदल दिया है; और ggtitle()opts(title = ...

सैंडी के स्थान पर उपयोग किया जाता है- बहुत बहुत धन्यवाद !!!! यह वही करता है जो मैं चाहता हूं। मेरी इच्छा है कि हम geom.text या geom.annotate में क्लिपिंग को नियंत्रित कर सकें।

यदि कोई और दिलचस्पी लेता है तो मैंने निम्नलिखित प्रोग्राम को एक साथ रखा है।

rm(list = ls())  # clear objects 
graphics.off()  # close graphics windows 

library(ggplot2) 
library(gridExtra) 

#create dummy data 
test= data.frame(
    group = c("Group 1", "Group 1", "Group 1","Group 2", "Group 2", "Group 2"), 
    x = c(1 ,2,3,1,2,3), 
    y = c(33,25,27,36,23,25), 
    n=c(71,55,65,58,65,58), 
    ypos=c(18,18,18,17,17,17) 

) 


p1 <- qplot(x=x, y=y, data=test, colour=group) + 
    ylab("Mean change from baseline") + 
    theme(plot.margin = unit(c(1,3,8,1), "lines")) + 
    geom_line()+ 
    scale_x_continuous("Visits", breaks=seq(-1,3)) + 
    theme(legend.position="bottom", 
     legend.title=element_blank())+ 
    ggtitle("Line plot") 


# Create the textGrobs 
for (ii in 1:nrow(test)) 
{ 
    #display numbers at each visit 
    p1=p1+ annotation_custom(grob = textGrob(test$n[ii]), 
          xmin = test$x[ii], 
          xmax = test$x[ii], 
          ymin = test$ypos[ii], 
          ymax = test$ypos[ii]) 

    #display group text 
    if (ii %in% c(1,4)) #there is probably a better way 
     { 
    p1=p1+ annotation_custom(grob = textGrob(test$group[ii]), 
          xmin = 0.85, 
          xmax = 0.85, 
          ymin = test$ypos[ii], 
          ymax = test$ypos[ii]) 
    } 

    } 




    # Code to override clipping 
    gt <- ggplot_gtable(ggplot_build(p1)) 
    gt$layout$clip[gt$layout$name=="panel"] <- "off" 
    grid.draw(gt) 

enter image description here

+1

बहुत अच्छा !! अपना जवाब स्वीकार करना न भूलें। –

+0

यह ठीक नहीं दिखता है, क्या कोड अभी भी आपके लिए काम करता है? – PatrickT

13

अपडेट किया गयाopts()theme()

नीचे कोड में साथ प्रतिस्थापित किया गया, एक आधार साजिश साजिश के तल पर एक व्यापक मार्जिन के साथ तैयार की है,। टेक्स्टग्राब बनाया गया है, फिर annotation_custom() का उपयोग करके साजिश में डाला गया। पाठ को छोड़कर दृश्यमान नहीं है क्योंकि यह साजिश पैनल के बाहर है - आउटपुट पैनल पर फिसल गया है। लेकिन बैपटिस्ट के कोड from here का उपयोग करके, क्लिपिंग ओवरराइड किया जा सकता है। स्थिति डेटा इकाइयों के मामले में है, और दोनों पाठ लेबल केंद्रित हैं।

library(ggplot2) 
library(grid) 

# Base plot 
df = data.frame(x=seq(1:10), y = seq(1:10)) 
p = ggplot(data = df, aes(x = x, y = y)) + geom_point() + ylim(0,10) + 
    theme(plot.margin = unit(c(1,1,3,1), "cm")) 
p 

# Create the textGrobs 
Text1 = textGrob(paste("Largest x-value is", round(max(df$x), 2), sep = " ")) 
Text2 = textGrob(paste("Mean = ", mean(df$x), sep = "")) 

p1 = p + annotation_custom(grob = Text1, xmin = 4, xmax = 4, ymin = -3, ymax = -3) + 
     annotation_custom(grob = Text2, xmin = 8, xmax = 8, ymin = -3, ymax = -3) 
p1 

# Code to override clipping 
gt <- ggplotGrob(p1) 
gt$layout$clip[gt$layout$name=="panel"] <- "off" 
grid.draw(gt) 

enter image description here

या, grid कार्यों का उपयोग कर बना सकते हैं और लेबल स्थिति।

p 
grid.text((paste("Largest x-value is", max(df$x), sep = " ")), 
    x = unit(.2, "npc"), y = unit(.1, "npc"), just = c("left", "bottom"), 
    gp = gpar(fontface = "bold", fontsize = 18, col = "blue")) 

enter image description here

संपादित या, gtable कार्यों का उपयोग करके पाठ Grob जोड़ें।

library(ggplot2) 
library(grid) 
library(gtable) 

# Base plot 
df = data.frame(x=seq(1:10), y = seq(1:10)) 
p = ggplot(data = df, aes(x = x, y = y)) + geom_point() + ylim(0,10) 

# Construct the text grob 
lab = textGrob((paste("Largest x-value is", max(df$x), sep = " ")), 
    x = unit(.1, "npc"), just = c("left"), 
    gp = gpar(fontface = "bold", fontsize = 18, col = "blue")) 


gp = ggplotGrob(p) 

# Add a row below the 2nd from the bottom 
gp = gtable_add_rows(gp, unit(2, "grobheight", lab), -2) 

# Add 'lab' grob to that row, under the plot panel 
gp = gtable_add_grob(gp, lab, t = -2, l = gp$layout[gp$layout$name == "panel",]$l) 

grid.newpage() 
grid.draw(gp) 

enter image description here

+0

आपको बहुत बहुत धन्यवाद !!! मुझे एहसास है कि ग्रिड फ़ंक्शन डिवाइस पर कहीं भी टेक्स्ट रखने की अनुमति देते हैं। मैं बस सोच रहा हूं कि क्या साजिश के नीचे पाठ रखना संभव है, फिर भी किसी भी तरह से साजिश से एक्स निर्देशांक का उपयोग करें। i.e कुछ ऐसा है जो आप बेस आर –

+0

में पाठ फ़ंक्शन के साथ कर सकते हैं! मुझे त्रुटि मिल रही है: फ़ंक्शन "ggplot_gtable" नहीं मिला > gt $ लेआउट $ क्लिप [gt $ लेआउट $ name == "पैनल"] <- "ऑफ" –

+0

यह 'ggplot2' में एक फ़ंक्शन है। आप ggplot2 का किस संस्करण का उपयोग कर रहे हैं? आपको अपग्रेड करना पड़ सकता है। नवीनतम संस्करण 0.9.0 –

5

असल में सर्वश्रेष्ठ उत्तर और सबसे आसान समाधान cowplot पैकेज का उपयोग करने के लिए है।

cowplot पैकेज (सीआरएएन पर) संस्करण 0.5.0 add_sub फ़ंक्शन का उपयोग करके ggplot2 उपशीर्षक संभालता है।

इतना है कि यह प्रयोग करें: जो भूखंड नीचे एक टिप्पणी के प्रदर्शित करता है

diamondsCubed <-ggplot(aes(carat, price), data = diamonds) + 
    geom_point() + 
    scale_x_continuous(trans = cuberoot_trans(), limits = c(0.2, 3), 
        breaks = c(0.2, 0.5, 1, 2, 3)) + 
    scale_y_continuous(trans = log10_trans(), limits = c(350, 15000), 
        breaks = c(350, 1000, 5000, 10000, 15000)) + 
    ggtitle('Price log10 by Cube-Root of Carat') + 
    theme_xkcd() 


    ggdraw(add_sub(diamondsCubed, "This is an annotation.\nAnnotations can span multiple lines.")) 
12

वर्तमान संस्करण (> 2.1) एक + labs(caption = "text") है। यह विषयसंगत है (फ़ॉन्ट गुण, ... बाएं/दाएं गठबंधन)। उदाहरण के लिए https://github.com/hadley/ggplot2/pull/1582 देखें।