geom_tile绘图中的多个图例 [英] Multiple legends in a plot with geom_tile
问题描述
如果我绘制P1,这就是我得到的结果:
p < - ggplot()
p < - (p
+ geom_tile(data = P1,aes(x = as.numeric(N1),y = as.numeric(N2),fill = mean_RMSE))
)
但是我的目标是在同一图上绘制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))
)
我不知道如何改变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,color =red))
+ geom_tile(data = psT_SST [[1]],aes(x = as.numeric(N2),y = as.numeric(N1) mean_RMSE,color =blue)))
$ b
$ b
有人可以帮我吗?
我假设您要使用不同的颜色用于P1和P2数据帧的方案。您可以通过将 fill = factor(data_frame)
与 alpha = mean_RMSE
结合来实现。然后
,您可以使用gridExtra和gtable包添加2个图例。
#构成数据
P1 < - data.frame(N1 = as.integer(runif(100, (100,0,12))* 10,
mean_RMSE = rnorm(100,0,1),
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 (g1,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))
##创建虚拟图来创建2种不同配色方案的图例
(g_dummy1 < - ggplot(子集(d,data_frame ==P1),aes(N1 ,N2,fill = mean_RMSE))+ geom_tile()+
scale_fill_gradient n(name =Mean RMSE \\\
(P1),colors = 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 \\\
(P2),colors = c(#0000FF,white )))
<$ c $ (gpplot){
tmp< - ggplot_gtable(ggplot_build(a.gplot))
leg< - which(sapply(tmp $ grobs,function(x)x
传奇< - tmp $ grobs [[leg]]
传奇
}
现在使用这个函数创建两个不同配色方案的图例库,然后使用 grid.arrange
来放置一切都在一起:
legend1 < - g_legend(g_dummy1)
legend2 < - g_legend(g_dummy2)
grid.arrange(g_m ain + theme(legend.position ='none'),legend1,legend2,
ncol = 3,widths = c(4/6,1 / 6,1 / 6))
I have two different data frame: P1 and P2. Each of these data frame have 3 different columns: N1, N2 and a value (mean_RMSE). N1 and N2 are between 15 and 120 and for a line, N2 is always lower than N1.
If I plot P1, this is what I get:
p <- ggplot()
p <- (p
+ geom_tile(data=P1, aes(x=as.numeric(N1), y=as.numeric(N2), fill=mean_RMSE))
)
But my aim is to plot P1 and P2 on the same plot:
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))
)
I don't know how to change the color for P2. For example, I want a blue color range for P1 and a red color range for P2, to easily distinguish P1 from P2.
scale_fill_gradient change the color of P1 and P2 (I still can't distinguish them) and if I add "colour" in geom_tile, I juste have two different contours:
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")))
In fact, I want two different legends for P1 and P2.
Can someone help me?
I'm assuming that you want to use different color schemes for P1 and P2 data frames. You can achieve this by combining fill=factor(data_frame)
with alpha=mean_RMSE
. Then
you can add 2 legends using gridExtra and gtable packages.
# 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")))
This is a function found in this post to create a legend grob.
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
}
Now use this function to create 2 legend grobs with different color schemes, then use grid.arrange
to put everything together:
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))
这篇关于geom_tile绘图中的多个图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!