英文:
One of the groups not showing when using combined legend on ggplot2
问题
我正在尝试在两个图中绘制三个模型,但这两个图只有一个共同的模型(模型 A)。然而,当我尝试制作一个合并的图时,并没有显示所有三个模型。我正在寻找一个让图例包含所有三个模型(A、B、C)的方法。
**代码:**
cols=c("red","blue","green")
df = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="B",x=10:1,val=runif(n=10,min=0,max=1)))
plot<-ggplot()+
geom_point(df, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(df, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y 轴", x="x 轴",color="模型")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","B","C"),
values = c(cols[1],cols[2],cols[3]))
gdf = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="C",x=10:1,val=runif(n=10,min=0,max=1)))
gplot<-ggplot()+
geom_point(gdf, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(gdf, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y 轴", x="x 轴",color="模型")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","C","B"),
values = c(cols[1],cols[3],cols[2]))
final_plot = ggarrange(plot,gplot,
nrow=1,ncol=2,common.legend = TRUE, legend = "bottom")
**图:**
[![enter image description here][1]][1]
**最新尝试:** 我尝试将数据合并为一个数据集,然后进行子集,但结果相同
cols=c("red","blue","green")
df = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="B",x=10:1,val=runif(n=10,min=0,max=1)), data.frame(model ="C",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)))
df$ind=1
df$ind[21:40]=0
plot<-ggplot(df)+
geom_point(data=df%>%subset(ind==1), mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(data=df%>%subset(ind==1), mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y 轴", x="x 轴",color="模型")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","B","C"),
values = c(cols[1],cols[2],cols[3]))
gplot<-ggplot(df)+
geom_point(data=df%>%subset(ind==0), mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(data=df%>%subset(ind==0), mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y 轴", x="x 轴",color="模型")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","C","B"),
values = c(cols[1],cols[3],cols[2]))
final_plot = ggarrange(plot,gplot,
nrow=1,ncol=2,common.legend = TRUE, legend = "bottom")
英文:
I'm trying to plot 3 models over 2 plots, but the plots only have 1 model in common (model A). However, when I try to make a combined plot, it doesn't show all 3 models. I'm looking for a way for the legend to have all 3 models (A,B,C).
Code:
cols=c("red","blue","green")
df = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="B",x=10:1,val=runif(n=10,min=0,max=1)))
plot<-ggplot()+
geom_point(df, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(df, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y axis", x="x axis",color="Model")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","B","C"),
values = c(cols[1],cols[2],cols[3]))
gdf = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="C",x=10:1,val=runif(n=10,min=0,max=1)))
gplot<-ggplot()+
geom_point(gdf, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(gdf, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y axis", x="x axis",color="Model")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","C","B"),
values = c(cols[1],cols[3],cols[2]))
final_plot = ggarrange(plot,gplot,
nrow=1,ncol=2,common.legend = TRUE, legend = "bottom")
Latest attempt: I've tried to combine the data into one dataset and then subset, but I get the same result
cols=c("red","blue","green")
df = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="B",x=10:1,val=runif(n=10,min=0,max=1)), data.frame(model ="C",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)))
df$ind=1
df$ind[21:40]=0
plot<-ggplot(df)+
geom_point(data=df%>%subset(ind==1), mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(data=df%>%subset(ind==1), mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y axis", x="x axis",color="Model")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","B","C"),
values = c(cols[1],cols[2],cols[3]))
gplot<-ggplot(df)+
geom_point(data=df%>%subset(ind==0), mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(data=df%>%subset(ind==0), mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y axis", x="x axis",color="Model")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","C","B"),
values = c(cols[1],cols[3],cols[2]))
final_plot = ggarrange(plot,gplot,
nrow=1,ncol=2,common.legend = TRUE, legend = "bottom")
答案1
得分: 1
我成功地用Paul的建议解决了这个问题:
cols=c("red","blue","green")
df = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="B",x=10:1,val=runif(n=10,min=0,max=1)), data.frame(model ="C",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)))
df$ind=1
df$ind[21:40]=0
plot<-ggplot(df)+
geom_point(data=df, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(data=df, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y axis", x="x axis",color="Model")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","B","C"),
values = c(cols[1],cols[2],cols[3]))+facet_wrap(~ ind)
英文:
I managed to solve this using the suggestion by Paul:
cols=c("red","blue","green")
df = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="B",x=10:1,val=runif(n=10,min=0,max=1)), data.frame(model ="C",x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)))
df$ind=1
df$ind[21:40]=0
plot<-ggplot(df)+
geom_point(data=df, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
geom_line(data=df, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
scale_y_continuous(limits = c(0, 1))+
labs(y = "y axis", x="x axis",color="Model")+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c("A","B","C"),
values = c(cols[1],cols[2],cols[3]))+facet_wrap(~ ind)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论