我有以下示例
data(diamonds, package="ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1],200),]
ggpairs(diamonds.samp, columns=8:10,
upper=list(continuous='cor'),
lower=list(continuous = 'points'),
diag=list(continuous='density'),
axisLabels='show'
)
产生一个非常好的数字:
但我的问题是,在真实的数据集中,我有很多要点,我想更改点geom的参数。我想减小点大小并使用较低的alpha值。但是我不能用它适用于所有情节的“param”选项 - 而不仅仅是较低的情节:
ggpairs(diamonds.samp, columns=8:10,
upper=list(continuous='cor'),
lower=list(continuous = 'points'),
diag=list(continuous='density'),
params=c(alpha=1/10),
axisLabels='show'
)
导致这个情节:
是否有办法将参数仅应用于“较低”的图表 - 或者我是否必须按照主题How to adjust figure settings in plotmatrix?
中的建议使用创建自定义图的功能提前 - 谢谢!
答案 0 :(得分:7)
似乎没有任何优雅的方法可以做到这一点,但你可以通过编写一个函数来回避来自ggally_pairs()对象的现有子图调用,然后在最后一个括号之前压缩params。 [不是非常强大,它只适用于图表已经有效]
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1],200),]
g<-ggpairs(diamonds.samp, columns=8:10,
upper=list(continuous='cor'),
lower=list(continuous = 'points'),
diag=list(continuous='density'),
axisLabels='show'
)
add_p<-function(g,i,params){
side=length(g$columns) # get number of cells per side
lapply(i,function(i){
s<-as.character(g$plots[i]) # get existing call as a template
l<-nchar(s)
p<-paste0(substr(s,1,l-1),",",params,")") # append params before last bracket
r<-i%/%side+1 # work out the position on the grid
c<-i%%side
array(c(p,r,c)) # return the sub-plot and position data
})
}
rep_cells<-c(4,7,8)
add_params<-"alpha=0.3, size=0.1, color='red'"
ggally_data<-g$data # makes sure that the internal parameter picks up your data (it always calls it's data 'ggally_data'
calls<-add_p(g,rep_cells,params=add_params) #call the function
for(i in 1:length(calls)){g<-putPlot(g,calls[[i]][1],as.numeric(calls[[i]][2]),as.numeric(calls[[i]][3]))}
g # call the plot