使用ggplot2的组合图例时,其中一个组不显示。

huangapple go评论73阅读模式
英文:

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(&quot;red&quot;,&quot;blue&quot;,&quot;green&quot;)
df = rbind(data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model =&quot;B&quot;,x=10:1,val=runif(n=10,min=0,max=1)))
plot&lt;-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 = &quot;y axis&quot;, x=&quot;x axis&quot;,color=&quot;Model&quot;)+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;),
values = c(cols[1],cols[2],cols[3]))
gdf = rbind(data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model =&quot;C&quot;,x=10:1,val=runif(n=10,min=0,max=1)))
gplot&lt;-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 = &quot;y axis&quot;, x=&quot;x axis&quot;,color=&quot;Model&quot;)+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c(&quot;A&quot;,&quot;C&quot;,&quot;B&quot;),
values = c(cols[1],cols[3],cols[2]))
final_plot = ggarrange(plot,gplot,
nrow=1,ncol=2,common.legend = TRUE, legend = &quot;bottom&quot;)

Plot:
使用ggplot2的组合图例时,其中一个组不显示。

Latest attempt: I've tried to combine the data into one dataset and then subset, but I get the same result

cols=c(&quot;red&quot;,&quot;blue&quot;,&quot;green&quot;)
df = rbind(data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model =&quot;B&quot;,x=10:1,val=runif(n=10,min=0,max=1)), data.frame(model =&quot;C&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)))
df$ind=1
df$ind[21:40]=0
plot&lt;-ggplot(df)+
geom_point(data=df%&gt;%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%&gt;%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 = &quot;y axis&quot;, x=&quot;x axis&quot;,color=&quot;Model&quot;)+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;),
values = c(cols[1],cols[2],cols[3]))
gplot&lt;-ggplot(df)+
geom_point(data=df%&gt;%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%&gt;%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 = &quot;y axis&quot;, x=&quot;x axis&quot;,color=&quot;Model&quot;)+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c(&quot;A&quot;,&quot;C&quot;,&quot;B&quot;),
values = c(cols[1],cols[3],cols[2]))
final_plot = ggarrange(plot,gplot,
nrow=1,ncol=2,common.legend = TRUE, legend = &quot;bottom&quot;)

答案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)

使用ggplot2的组合图例时,其中一个组不显示。

英文:

I managed to solve this using the suggestion by Paul:

cols=c(&quot;red&quot;,&quot;blue&quot;,&quot;green&quot;)
df = rbind(data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model =&quot;B&quot;,x=10:1,val=runif(n=10,min=0,max=1)), data.frame(model =&quot;C&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)))
df$ind=1
df$ind[21:40]=0
plot&lt;-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 = &quot;y axis&quot;, x=&quot;x axis&quot;,color=&quot;Model&quot;)+ theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +scale_color_manual(labels = c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;),
values = c(cols[1],cols[2],cols[3]))+facet_wrap(~ ind)

使用ggplot2的组合图例时,其中一个组不显示。

huangapple
  • 本文由 发表于 2023年2月14日 19:05:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定