GGally 中的灰度图矩阵
Grey scale plot matrix in GGally
我想在灰度中使用 GGally
创建一个绘图矩阵。我试过 scale_colour_grey
之类的函数,但 GGally
只能识别来自 ggplot2
的主题对象。下面提供了可重现的数据和代码:
Cluster = c(1, 2, 1, 3, 1, 1, 1, 1, 1, 2, 2, 3, 4, 4, 1, 4, 1, 1, 2, 1, 3, 4, 4, 4, 4, 2, 1, 4, 3, 4, 2, 4, 2, 2, 1, 2, 1, 1, 1, 4, 2, 2, 4, 1, 4, 4, 1, 3, 2, 2, 4, 4, 4, 4, 1, 4, 4, 1, 4, 4, 1, 2, 2, 4, 4, 4, 2, 1, 4, 4, 2, 2, 2, 2, 2, 4, 2, 2, 1, 2, 2, 4, 2, 2, 2, 4, 4, 4, 2, 3, 4, 1, 4, 2, 1, 2, 4, 1, 4, 1)
Dim1 = c(12.53, -17.16, 9.24, 16.86, 2.05, 8.02, 8.58, 9.07, 5.91, -10.68, -21.13, 16.9, -5.65, -2.19, 9.23, 1.58, 2.54, 9.51, -15.3, 6.02, 23.97, -1.21, -2.82, -6.35, -5.33, -15.19, 6.24, -3.35, 32, -2.54, -12.19, -3.41, -19.89, -9.16, 5.67, -23.93, 5.07, 10.38, 11.79, -3.41, -17.81, -28.21, -1.06, 5.56, -1.02, -2.65, 6.62, 15.14, -16.22, -21.33, -9.06, -9.22, -5.21, -9.73, 3.11, -9.67, -5.84, 4.7, -3.24, -8.37, 5.87, -11.69, -20.75, 0.2, -7.86, -3.21, -17.99, 7.68, -8.34, -0.47, -10.91, -17.97, -15.46, -16.31, -26.15, -7.07, -27.42, -10.37, 8.04, -14.22, -13.18, -1.64, -24.05, -15.28, -19, -2.78, -1.34, -8.02, -17.7, 19.67, -9.93, 2.94, -8.12, -15.92, 2.25, -18.54, -10.25, 1.14, -4.21, -0.53)
Dim2 = c(3.2, -4.12, 4.11, 6.81, 4.73, 3.36, 6.7, 1.69, 2.87, -1.59, -2.01, -0.28, -6.8, 1.32, 3.49, -1.92, 5.5, 5.65, -2.44, 1.91, 4.69, 4.79, 0.86, 1.33, -2.59, -1.68, 0.1, -2.86, 5.28, 0.15, -2.13, -4.22, -3.42, -6.25, 2.09, -4.95, -3.26, 5.4, 8.88, -0.02, -1.85, -4.48, 2.25, 5.95, 3.15, 3.39, 2.41, 7.08, 0.01, 0.07, 4.66, -2.14, 4.57, 3.75, -0.05, -0.27, 1.11, 1.78, -2.38, 2.92, 3.5, -1.02, -0.04, -0.9, -1.09, 1.39, -4.57, 3.34, 2, 1.55, 4.95, -3.38, 1.85, 2.76, -5.1, 0.52, -10.27, 0.19, 1.28, 3.9, -8.4, 5.01, -3.67, -4.29, -1.65, 1.44, 1.68, 0.04, -2.01, 4.95, -4.58, 2.29, -1.22, -3.31, -0.89, 5.39, 2.18, 2.03, -5.53, 6.6)
Dim3 = c(-0.1, 3.28, -5.6, -5.54, 0.96, -2.73, -3.03, -4.47, -7.55, 1.48, 2.88, -3.4, -8.23, -1.12, -1.32, 3.21, -0.8, -2.98, 5.98, -2.88, 2.11, -1.03, -0.64, -0.59, 2.61, 1.75, -2.69, -1.23, -4.72, 8.48, 2.67, -4.56, 5.34, -4.87, -7.56, 5.34, -1.55, -1.98, 0.88, -1.25, 7.6, 7.18, 4.21, -3.91, -1.49, 1.88, -3.48, -2.57, 6.7, 3.16, 4.21, -0.03, -5.08, 1.07, 1.39, -2.3, 0.07, 5.56, -2.58, 1.52, 6.77, 1.84, -0.52, 1.51, 10.43, 0.13, -2.12, 7.91, 2.05, 1.35, -4.66, 4.9, 11.9, 3.78, -1.83, 0.4, 14.4, -2.41, 0.93, 2.42, 12.32, -3.93, 2.06, 8.23, 11.85, 10.39, -1.51, 4.24, -1.92, -1.73, -1.72, -3.38, 9.51, -0.48, -1.97, -0.37, 1.82, -0.5, -8.59, -3.86)
Dim4 = c(-0.52, -0.83, -2.64, 1.25, -2.17, -0.56, 0.32, -0.06, 1.68, -1.64, 0.72, 0.07, -2.9, -0.55, 2.44, 0.6, -1.29, -2.2, -0.33, -0.43, -0.19, 2.62, -0.76, 0, 1.65, -1.71, 0.82, 0.06, 0.02, -1.6, 2.11, -0.95, -0.16, -3.13, -2.56, 0.21, -1.91, -0.39, 0.02, -1.03, 3.23, 1, 3.19, 2.27, -2.29, 0.79, -0.58, -1.63, -0.51, 0.65, 6.26, -0.28, -4.55, -1.38, -2.73, -0.16, 1.14, 1.35, 0.56, 0.04, -0.94, -1.78, -3.43, -2.12, 0.42, 0.65, -3.02, 1.87, -0.15, -1.74, -1.38, -0.11, 0.82, 7.25, -2.63, -2.47, 0.36, -2.82, 4.52, 5.42, -2.03, -1.91, -2.34, 0.46, -1.56, 0.41, 0.21, -1.15, -3.51, -1.29, 0.53, -1.96, -2.65, 4.01, 3.46, -1.16, -1.81, -0.46, 4.51, 1.15)
Dim5 = c(0.22, -0.31, -1.56, 2.68, 1.72, 0.83, 1.29, 0.75, 7.28, 0.45, 1.35, 0.37, -1.07, -0.7, 1.51, -0.53, 1.5, -1.68, -0.17, 4.25, -0.45, 0.46, -0.31, -0.81, -0.18, -0.17, 2.84, 1.17, -0.85, -1.74, 1.36, -2.39, -0.54, -1.75, -2.6, -1.49, -2.61, 0.17, 0.83, 3.44, 0.41, 0.51, 2.51, 1.85, 1.06, -0.04, 1.71, 1.96, -1.6, 0.32, 0.08, -1.02, -0.97, 0.25, -0.1, -1.31, 0.45, -1.74, 1.82, -0.12, -0.94, 2.14, -1.46, 1.47, -2.42, -0.3, 1.24, 0.44, -0.18, -3.18, 9.71, -0.38, -1.02, 2.76, -0.45, -2.52, -0.64, -1.04, -0.32, -1.25, -0.26, 0.72, 0.16, -0.5, -0.1, -1.72, 1.82, -0.89, -0.33, 4.48, -2.46, -0.36, -1.23, 3.22, 1.1, 3.11, -0.1, 0.66, 6.75, 0.93)
d <- cbind.data.frame(Cluster, Dim1, Dim2, Dim3, Dim4, Dim5)
d$Cluster <- as.factor(d$Cluster)
我正在使用以下函数生成绘图:
library(ggplot2)
library(extrafont)
library(GGally)
loadfonts(device = "win")
plotgraph_write <- function(d, filename, font="Times New Roman")
{
png(filename = filename, width = 7, height = 5, units="in", res = 600)
p <- ggpairs(d, columns = 2:6, ggplot2::aes(colour=Cluster), upper = "blank")+
ggplot2::theme_bw()+
ggplot2::theme(legend.position = "bottom") +
ggplot2::theme(text=element_text(family=font, colour = "black"), panel.grid.minor.y = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank())+
ggplot2::theme(axis.title.x = element_blank(), axis.title.y = element_blank())+
ggplot2::theme(axis.text.x = element_text(colour="black"), axis.text.y = element_text(colour="black"))
p_ <- GGally::print_if_interactive
p_(p)
dev.off()
}
p <- plotgraph_write(d, "tmp.png")
默认情况下,绘图矩阵使用红色、绿色、紫色等颜色。我尝试尝试更改颜色。但是,例如,在绘图对象中添加 ggplot2::scale_colour_discrete()
会使它抛出错误。我知道正在创建不止一个图,但我不知道在哪里将颜色定义为灰度。我怎样才能做到这一点?
感谢上面的评论, 已经提供了解决方案。我在我的函数中添加了以下代码,它现在以灰度模式工作。
for(i in 1:p$nrow) {
for(j in 1:p$ncol){
p[i,j] <- p[i,j] +
scale_colour_grey(start = 0.3, end = 0.8) +
scale_fill_grey(start = 0.3, end = 0.8)
}
}
我想在灰度中使用 GGally
创建一个绘图矩阵。我试过 scale_colour_grey
之类的函数,但 GGally
只能识别来自 ggplot2
的主题对象。下面提供了可重现的数据和代码:
Cluster = c(1, 2, 1, 3, 1, 1, 1, 1, 1, 2, 2, 3, 4, 4, 1, 4, 1, 1, 2, 1, 3, 4, 4, 4, 4, 2, 1, 4, 3, 4, 2, 4, 2, 2, 1, 2, 1, 1, 1, 4, 2, 2, 4, 1, 4, 4, 1, 3, 2, 2, 4, 4, 4, 4, 1, 4, 4, 1, 4, 4, 1, 2, 2, 4, 4, 4, 2, 1, 4, 4, 2, 2, 2, 2, 2, 4, 2, 2, 1, 2, 2, 4, 2, 2, 2, 4, 4, 4, 2, 3, 4, 1, 4, 2, 1, 2, 4, 1, 4, 1)
Dim1 = c(12.53, -17.16, 9.24, 16.86, 2.05, 8.02, 8.58, 9.07, 5.91, -10.68, -21.13, 16.9, -5.65, -2.19, 9.23, 1.58, 2.54, 9.51, -15.3, 6.02, 23.97, -1.21, -2.82, -6.35, -5.33, -15.19, 6.24, -3.35, 32, -2.54, -12.19, -3.41, -19.89, -9.16, 5.67, -23.93, 5.07, 10.38, 11.79, -3.41, -17.81, -28.21, -1.06, 5.56, -1.02, -2.65, 6.62, 15.14, -16.22, -21.33, -9.06, -9.22, -5.21, -9.73, 3.11, -9.67, -5.84, 4.7, -3.24, -8.37, 5.87, -11.69, -20.75, 0.2, -7.86, -3.21, -17.99, 7.68, -8.34, -0.47, -10.91, -17.97, -15.46, -16.31, -26.15, -7.07, -27.42, -10.37, 8.04, -14.22, -13.18, -1.64, -24.05, -15.28, -19, -2.78, -1.34, -8.02, -17.7, 19.67, -9.93, 2.94, -8.12, -15.92, 2.25, -18.54, -10.25, 1.14, -4.21, -0.53)
Dim2 = c(3.2, -4.12, 4.11, 6.81, 4.73, 3.36, 6.7, 1.69, 2.87, -1.59, -2.01, -0.28, -6.8, 1.32, 3.49, -1.92, 5.5, 5.65, -2.44, 1.91, 4.69, 4.79, 0.86, 1.33, -2.59, -1.68, 0.1, -2.86, 5.28, 0.15, -2.13, -4.22, -3.42, -6.25, 2.09, -4.95, -3.26, 5.4, 8.88, -0.02, -1.85, -4.48, 2.25, 5.95, 3.15, 3.39, 2.41, 7.08, 0.01, 0.07, 4.66, -2.14, 4.57, 3.75, -0.05, -0.27, 1.11, 1.78, -2.38, 2.92, 3.5, -1.02, -0.04, -0.9, -1.09, 1.39, -4.57, 3.34, 2, 1.55, 4.95, -3.38, 1.85, 2.76, -5.1, 0.52, -10.27, 0.19, 1.28, 3.9, -8.4, 5.01, -3.67, -4.29, -1.65, 1.44, 1.68, 0.04, -2.01, 4.95, -4.58, 2.29, -1.22, -3.31, -0.89, 5.39, 2.18, 2.03, -5.53, 6.6)
Dim3 = c(-0.1, 3.28, -5.6, -5.54, 0.96, -2.73, -3.03, -4.47, -7.55, 1.48, 2.88, -3.4, -8.23, -1.12, -1.32, 3.21, -0.8, -2.98, 5.98, -2.88, 2.11, -1.03, -0.64, -0.59, 2.61, 1.75, -2.69, -1.23, -4.72, 8.48, 2.67, -4.56, 5.34, -4.87, -7.56, 5.34, -1.55, -1.98, 0.88, -1.25, 7.6, 7.18, 4.21, -3.91, -1.49, 1.88, -3.48, -2.57, 6.7, 3.16, 4.21, -0.03, -5.08, 1.07, 1.39, -2.3, 0.07, 5.56, -2.58, 1.52, 6.77, 1.84, -0.52, 1.51, 10.43, 0.13, -2.12, 7.91, 2.05, 1.35, -4.66, 4.9, 11.9, 3.78, -1.83, 0.4, 14.4, -2.41, 0.93, 2.42, 12.32, -3.93, 2.06, 8.23, 11.85, 10.39, -1.51, 4.24, -1.92, -1.73, -1.72, -3.38, 9.51, -0.48, -1.97, -0.37, 1.82, -0.5, -8.59, -3.86)
Dim4 = c(-0.52, -0.83, -2.64, 1.25, -2.17, -0.56, 0.32, -0.06, 1.68, -1.64, 0.72, 0.07, -2.9, -0.55, 2.44, 0.6, -1.29, -2.2, -0.33, -0.43, -0.19, 2.62, -0.76, 0, 1.65, -1.71, 0.82, 0.06, 0.02, -1.6, 2.11, -0.95, -0.16, -3.13, -2.56, 0.21, -1.91, -0.39, 0.02, -1.03, 3.23, 1, 3.19, 2.27, -2.29, 0.79, -0.58, -1.63, -0.51, 0.65, 6.26, -0.28, -4.55, -1.38, -2.73, -0.16, 1.14, 1.35, 0.56, 0.04, -0.94, -1.78, -3.43, -2.12, 0.42, 0.65, -3.02, 1.87, -0.15, -1.74, -1.38, -0.11, 0.82, 7.25, -2.63, -2.47, 0.36, -2.82, 4.52, 5.42, -2.03, -1.91, -2.34, 0.46, -1.56, 0.41, 0.21, -1.15, -3.51, -1.29, 0.53, -1.96, -2.65, 4.01, 3.46, -1.16, -1.81, -0.46, 4.51, 1.15)
Dim5 = c(0.22, -0.31, -1.56, 2.68, 1.72, 0.83, 1.29, 0.75, 7.28, 0.45, 1.35, 0.37, -1.07, -0.7, 1.51, -0.53, 1.5, -1.68, -0.17, 4.25, -0.45, 0.46, -0.31, -0.81, -0.18, -0.17, 2.84, 1.17, -0.85, -1.74, 1.36, -2.39, -0.54, -1.75, -2.6, -1.49, -2.61, 0.17, 0.83, 3.44, 0.41, 0.51, 2.51, 1.85, 1.06, -0.04, 1.71, 1.96, -1.6, 0.32, 0.08, -1.02, -0.97, 0.25, -0.1, -1.31, 0.45, -1.74, 1.82, -0.12, -0.94, 2.14, -1.46, 1.47, -2.42, -0.3, 1.24, 0.44, -0.18, -3.18, 9.71, -0.38, -1.02, 2.76, -0.45, -2.52, -0.64, -1.04, -0.32, -1.25, -0.26, 0.72, 0.16, -0.5, -0.1, -1.72, 1.82, -0.89, -0.33, 4.48, -2.46, -0.36, -1.23, 3.22, 1.1, 3.11, -0.1, 0.66, 6.75, 0.93)
d <- cbind.data.frame(Cluster, Dim1, Dim2, Dim3, Dim4, Dim5)
d$Cluster <- as.factor(d$Cluster)
我正在使用以下函数生成绘图:
library(ggplot2)
library(extrafont)
library(GGally)
loadfonts(device = "win")
plotgraph_write <- function(d, filename, font="Times New Roman")
{
png(filename = filename, width = 7, height = 5, units="in", res = 600)
p <- ggpairs(d, columns = 2:6, ggplot2::aes(colour=Cluster), upper = "blank")+
ggplot2::theme_bw()+
ggplot2::theme(legend.position = "bottom") +
ggplot2::theme(text=element_text(family=font, colour = "black"), panel.grid.minor.y = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank())+
ggplot2::theme(axis.title.x = element_blank(), axis.title.y = element_blank())+
ggplot2::theme(axis.text.x = element_text(colour="black"), axis.text.y = element_text(colour="black"))
p_ <- GGally::print_if_interactive
p_(p)
dev.off()
}
p <- plotgraph_write(d, "tmp.png")
默认情况下,绘图矩阵使用红色、绿色、紫色等颜色。我尝试尝试更改颜色。但是,例如,在绘图对象中添加 ggplot2::scale_colour_discrete()
会使它抛出错误。我知道正在创建不止一个图,但我不知道在哪里将颜色定义为灰度。我怎样才能做到这一点?
感谢上面的评论,
for(i in 1:p$nrow) {
for(j in 1:p$ncol){
p[i,j] <- p[i,j] +
scale_colour_grey(start = 0.3, end = 0.8) +
scale_fill_grey(start = 0.3, end = 0.8)
}
}