संक्षिप्त उत्तर: ऐसा करने का एक सुरुचिपूर्ण या आसान तरीका प्रतीत नहीं होता है, लेकिन यहां एक कामकाज है।
मैंने ggpairs
स्रोत कोड ( GGally package source available from CRAN में) में डुबकी डालने के लिए यह देखने के लिए कि परिवर्तनीय लेबल वास्तव में कैसे खींचे जाते हैं। ggpairs.R
में प्रासंगिक फ़ंक्शन print.ggpairs
है। यह पता चला है कि परिवर्तनीय लेबल प्लॉट मैट्रिक्स के प्रत्येक सेल में ggplot
ऑब्जेक्ट्स का हिस्सा नहीं हैं - यानी वे अक्ष शीर्षक नहीं हैं, यही कारण है कि वे theme(axis.title.x = element_text(angle = 45)
या इसी तरह का उपयोग करके प्रभावित नहीं हैं।
इसके बजाय, उन्हें grid.text
(पैकेज 'grid'
में) का उपयोग करके टेक्स्ट एनोटेशन के रूप में खींचा जाना प्रतीत होता है। grid.text
x, y, hjust, vjust, rot
(जहां rot
रोटेशन का कोण है), साथ ही फ़ॉन्ट आकार, फ़ॉन्ट परिवार इत्यादि सहित gpar
(?grid.text
देखें) सहित तर्क लेता है, लेकिन ऐसा लगता है कि वर्तमान में उन पैरामीटर के विभिन्न मानों को पारित करने का कोई तरीका नहीं है print.ggpairs
- वे डिफ़ॉल्ट मानों पर तय हैं।
print.ggpairs
कोड के प्रासंगिक भाग के संशोधन का उपयोग करके आप अपने परिवर्तनीय लेबल को रिक्त स्थान से प्रारंभ करके और फिर बाद में अनुकूलित प्लेसमेंट, रोटेशन और स्टाइल के साथ जोड़कर इसके आसपास काम कर सकते हैं। मैं निम्नलिखित संशोधन के साथ आया था। यहाँ (संयोग से, क्योंकि मूल GGally
स्रोत कोड एक GPL-3 license के तहत जारी किया गया था, तो यह संशोधन है।)
customize.labels <- function(
plotObj,
varLabels = NULL, #vector of variable labels
titleLabel = NULL, #string for title
leftWidthProportion = 0.2, #if you changed these from default...
bottomHeightProportion = 0.1, #when calling print(plotObj),...
spacingProportion = 0.03, #then change them the same way here so labels will line up with plot matrix.
left.opts = NULL, #see pattern in left.opts.default
bottom.opts = NULL, #see pattern in bottom.opts.default
title.opts = NULL) { #see pattern in title.opts.default
require('grid')
vplayout <- function(x, y) {
viewport(layout.pos.row = x, layout.pos.col = y)
}
numCol <- length(plotObj$columns)
if (is.null(varLabels)) {
varLabels <- colnames(plotObj$data)
#default to using the column names of the data
} else if (length(varLabels) != numCol){
stop('Length of varLabels must be equal to the number of columns')
}
#set defaults for left margin label style
left.opts.default <- list(x=0,
y=0.5,
rot=90,
just=c('centre', 'centre'), #first gives horizontal justification, second gives vertical
gp=list(fontsize=get.gpar('fontsize')))
#set defaults for bottom margin label style
bottom.opts.default <- list(x=0,
y=0.5,
rot=0,
just=c('centre', 'centre'),#first gives horizontal justification, second gives vertical
gp=list(fontsize=get.gpar('fontsize')))
#set defaults for title text style
title.opts.default <- list(x = 0.5,
y = 1,
just = c(.5,1),
gp=list(fontsize=15))
#if opts not provided, go with defaults
if (is.null(left.opts)) {
left.opts <- left.opts.default
} else{
not.given <- names(left.opts.default)[!names(left.opts.default) %in%
names(left.opts)]
if (length(not.given)>0){
left.opts[not.given] <- left.opts.default[not.given]
}
}
if (is.null(bottom.opts)) {
bottom.opts <- bottom.opts.default
} else{
not.given <- names(bottom.opts.default)[!names(bottom.opts.default) %in%
names(bottom.opts)]
if (length(not.given)>0){
bottom.opts[not.given] <- bottom.opts.default[not.given]
}
}
if (is.null(title.opts)) {
title.opts <- title.opts.default
} else{
not.given <- names(title.opts.default)[!names(title.opts.default) %in%
names(title.opts)]
if (length(not.given)>0){
title.opts[not.given] <- title.opts.default[not.given]
}
}
showLabels <- TRUE
viewPortWidths <- c(leftWidthProportion,
1,
rep(c(spacingProportion,1),
numCol - 1))
viewPortHeights <- c(rep(c(1,
spacingProportion),
numCol - 1),
1,
bottomHeightProportion)
viewPortCount <- length(viewPortWidths)
if(!is.null(titleLabel)){
pushViewport(viewport(height = unit(1,"npc") - unit(.4,"lines")))
do.call('grid.text', c(title.opts[names(title.opts)!='gp'],
list(label=titleLabel,
gp=do.call('gpar',
title.opts[['gp']]))))
popViewport()
}
# viewport for Left Names
pushViewport(viewport(width=unit(1, "npc") - unit(2,"lines"),
height=unit(1, "npc") - unit(3, "lines")))
## new for axis spacingProportion
pushViewport(viewport(layout = grid.layout(
viewPortCount, viewPortCount,
widths = viewPortWidths, heights = viewPortHeights
)))
# Left Side
for(i in 1:numCol){
do.call('grid.text',
c(left.opts[names(left.opts)!='gp'],
list(label=varLabels[i],
vp = vplayout(as.numeric(i) * 2 - 1 ,1),
gp=do.call('gpar',
left.opts[['gp']]))))
}
popViewport()# layout
popViewport()# spacing
# viewport for Bottom Names
pushViewport(viewport(width=unit(1, "npc") - unit(3,"lines"),
height=unit(1, "npc") - unit(2, "lines")))
## new for axis spacing
pushViewport(viewport(layout = grid.layout(
viewPortCount, viewPortCount,
widths = viewPortWidths, heights = viewPortHeights)))
# Bottom Side
for(i in 1:numCol){
do.call('grid.text',
c(bottom.opts[names(bottom.opts)!='gp'],
list(label=varLabels[i],
vp = vplayout(2*numCol, 2*i),
gp=do.call('gpar',
bottom.opts[['gp']]))))
}
popViewport() #layout
popViewport() #spacing
}
और उस फ़ंक्शन को कॉल की एक उदाहरण है:
require('data.table')
require('GGally')
require('grid')
fake.data <- data.table(test.1=rnorm(50), #make some fake data for demonstration
test.2=rnorm(50),
test.3=rnorm(50),
test.4=rnorm(50))
g <- ggpairs(data=fake.data,
columnLabels=rep('', ncol(fake.data)))
#Set columnLabels to a vector of blank column labels
#so that original variable labels will be blank.
print(g)
customize.labels(plotObj=g,
titleLabel = 'Test plot', #string for title
left.opts = list(x=-0.5, #moves farther to the left, away from vertical axis
y=0.5, #centered with respect to vertical axis
just=c('center', 'center'),
rot=90,
gp=list(col='red',
fontface='italic',
fontsize=12)),
bottom.opts = list(x=0.5,
y=0,
rot=45, #angle the text at 45 degrees
just=c('center', 'top'),
gp=list(col='red',
fontface='bold',
fontsize=10)),
title.opts = list(gp=list(col='green',
fontface='bold.italic'))
)
(यह कुछ बहुत बदसूरत बना देता है लेबल - केवल प्रदर्शन के प्रयोजनों के लिए!)
मैंने आपके भूगर्भीय उदाहरण के रूप में कहीं और लेबल को रखने के साथ टिंकर नहीं किया - लेकिन मुझे लगता है कि आप "बाएं साइड" में vplayout
पर तर्कों को बदलकर ऐसा करेंगे और customize.labels
में कोड के "नीचे की ओर" टुकड़े। grid.text
में x
और y
निर्देशांक
pushViewport(viewport(layout = grid.layout(
viewPortCount, viewPortCount,
widths = viewPortWidths, heights = viewPortHeights
)))
कॉल vplayout
को निर्दिष्ट करता है जो ग्रिड के सेल प्रत्येक लेबल की स्थिति के लिए इस्तेमाल किया जा रहा है में एक व्यूपोर्ट है, जो एक ग्रिड में प्रदर्शन क्षेत्र बांटता के सापेक्ष परिभाषित कर रहे हैं।
मुझे लगता है कि इस 'GGally' में धकेल दिया जाना चाहिए, वर्तमान संस्करण के साथ 100% संगतता की अनुमति देने वाले डिफ़ॉल्ट के साथ 'ggpairs' में अतिरिक्त पैरामीटर जोड़ना। – mschilli