使用ggpairs的传说 [英] Legend using ggpairs
问题描述
#加载所需的软件包
要求(GGally)
#加载数据集
数据(状态)
df< - data.frame(state.x77,
State = state.name,
Abbrev = state.abb,
Region = state.region,
Division = state.division
)
#创建散点图矩阵
p < - ggpairs( df,
#在矩阵中包含的列
列= c(3,5,6,7),
#在对角线上包含的内容
#list (连续=分数)来镜像
#空白以关闭
upper =blank,
legends = T,
#包含什么低于对角线
lower = list(continuous =points),
#在对角线中包含的内容
diag = list(continuous =密度),
#如何标记内部图块
#internal,none,show
axisLabels =none,
#其他aes()参数
color =Region,
title =状态散点图矩阵
)
#显示绘图
print(p)
我应该为每个图表获得一个带有图例的图像。
但是,我得到一个没有任何传奇。
为什么我得到的图像没有传说的任何提示?我的特殊情况需要它们!
我正在使用R v。3.2.2,并在RStudio和RGui中都尝试过。
预先感谢!
应该有一个标准的方法来找它,但我找不到一个。旧的打印功能仍然存在并且可以访问,但是与内部功能一样。试试这个,而不是打印:
GGally ::: print_ggpairs_old(p)
如果你添加了这个(感谢这篇文章的答案
Trying to follow an example made here, I was reproducing the following code:
# Load required packages
require(GGally)
# Load datasets
data(state)
df <- data.frame(state.x77,
State = state.name,
Abbrev = state.abb,
Region = state.region,
Division = state.division
)
# Create scatterplot matrix
p <- ggpairs(df,
# Columns to include in the matrix
columns = c(3,5,6,7),
# What to include above diagonal
# list(continuous = "points") to mirror
# "blank" to turn off
upper = "blank",
legends=T,
# What to include below diagonal
lower = list(continuous = "points"),
# What to include in the diagonal
diag = list(continuous = "density"),
# How to label inner plots
# internal, none, show
axisLabels = "none",
# Other aes() parameters
colour = "Region",
title = "State Scatterplot Matrix"
)
# Show the plot
print(p)
I was supposed to get an image with legend for each plot.
But instead, I am getting one without any legend.
Any tips of why the image I am getting does not have the legends? My particular case needs them!
I am using R v. 3.2.2, and tried both in RStudio and RGui.
Thanks in advance!
There should be a standard way to to it, but I could not find one. The old print function is still there and accessible but as in internal function. Try this instead of the print:
GGally:::print_ggpairs_old(p)
And if you add this (thanks to this post answer Legend using ggpairs )
colidx <- c(3,5,6,7)
for (i in 1:length(colidx)) {
# Address only the diagonal elements
# Get plot out of plot-matrix
inner <- getPlot(p, i, i);
# Add ggplot2 settings (here we remove gridlines)
inner <- inner + theme(panel.grid = element_blank()) +
theme(axis.text.x = element_blank())
# Put it back into the plot-matrix
p <- putPlot(p, inner, i, i)
for (j in 1:length(colidx)){
if((i==1 & j==1)){
# Move the upper-left legend to the far right of the plot
inner <- getPlot(p, i, j)
inner <- inner + theme(legend.position=c(length(colidx)-0.25,0.50))
p <- putPlot(p, inner, i, j)
}
else{
# Delete the other legends
inner <- getPlot(p, i, j)
inner <- inner + theme(legend.position="none")
p <- putPlot(p, inner, i, j)
}
}
}
You get something much better:
这篇关于使用ggpairs的传说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!