我有两个不同的数据框:P1和P2.这些数据帧中的每一个都有3个不同的列:N1,N2和值(mean_RMSE).N1和N2在15和120之间,对于线,N2总是低于N1.
如果我绘制P1,这就是我得到的:
p <- ggplot()
p <- (p
+ geom_tile(data=P1, aes(x=as.numeric(N1), y=as.numeric(N2), fill=mean_RMSE))
)
Run Code Online (Sandbox Code Playgroud)

但我的目标是在同一块情节上绘制P1和P2:
p <- ggplot()
p <- (p
+ geom_tile(data=P1, aes(x=as.numeric(N1), y=as.numeric(N2), fill=mean_RMSE))
+ geom_tile(data=P2, aes(x=as.numeric(N2), y=as.numeric(N1), fill=mean_RMSE))
)
Run Code Online (Sandbox Code Playgroud)

我不知道如何改变P2的颜色.例如,我想要P1的蓝色范围和P2的红色范围,以便轻松区分P1和P2.
scale_fill_gradient改变了P1和P2的颜色(我仍然无法区分它们),如果我在geom_tile中添加"color",我会有两个不同的轮廓:
p <- ggplot()
p <- (p
+ geom_tile(data=psSST_T[[1]], aes(x=as.numeric(N1), y=as.numeric(N2), fill=mean_RMSE, colour="red"))
+ geom_tile(data=psT_SST[[1]], aes(x=as.numeric(N2), y=as.numeric(N1), fill=mean_RMSE, colour="blue")))
Run Code Online (Sandbox Code Playgroud)

事实上,我想要P1和P2的两个不同的传说.
有人能帮我吗?
我假设你想为P1和P2数据帧使用不同的颜色方案.您可以通过合并实现这一fill=factor(data_frame)带alpha=mean_RMSE.然后你可以使用gridExtra和gtable包添加2个图例.
# making up data
P1 <- data.frame(N1=as.integer(runif(100, 0, 12))*10,
N2=as.integer(runif(100, 0, 12))*10,
mean_RMSE=rnorm(100, 0, 1),
data_frame=rep("P1", 100))
P2 <- data.frame(N1=as.integer(runif(100, 0, 12))*10,
N2=as.integer(runif(100, 0, 12))*10,
mean_RMSE=rnorm(100, 0, 1),
data_frame=rep("P2", 100))
d <- rbind(P1, P2)
library(ggplot2)
library(gridExtra)
library(gtable)
(g_main <- ggplot(d, aes(N1, N2, fill=data_frame, alpha=mean_RMSE)) + geom_tile() +
scale_fill_manual("", values = c("#CC0000", "#0000FF"), drop = FALSE))
## create dummy plots to create legends of 2 different color schemes
(g_dummy1 <- ggplot(subset(d, data_frame=="P1"), aes(N1, N2, fill=mean_RMSE)) + geom_tile() +
scale_fill_gradientn(name = "Mean RMSE \n(P1)", colours=c("#CC0000", "white")))
(g_dummy2 <- ggplot(subset(d, data_frame=="P2"), aes(N1, N2, fill=mean_RMSE)) + geom_tile() +
scale_fill_gradientn(name = "Mean RMSE \n(P2)", colours=c("#0000FF", "white")))
Run Code Online (Sandbox Code Playgroud)
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
legend
}
Run Code Online (Sandbox Code Playgroud)
现在使用此函数创建具有不同颜色方案的2个图例grob,然后用于grid.arrange将所有内容组合在一起:
legend1 <- g_legend(g_dummy1)
legend2 <- g_legend(g_dummy2)
grid.arrange(g_main+theme(legend.position = 'none'), legend1, legend2,
ncol=3, widths=c(4/6, 1/6, 1/6))
Run Code Online (Sandbox Code Playgroud)
