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

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

One of the groups not showing when using combined legend on ggplot2

问题

  1. 我正在尝试在两个图中绘制三个模型,但这两个图只有一个共同的模型(模型 A)。然而,当我尝试制作一个合并的图时,并没有显示所有三个模型。我正在寻找一个让图例包含所有三个模型(ABC)的方法。
  2. **代码:**
  3. cols=c("red","blue","green")
  4. df = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
  5. data.frame(model ="B",x=10:1,val=runif(n=10,min=0,max=1)))
  6. plot<-ggplot()+
  7. geom_point(df, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
  8. geom_line(df, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  9. scale_y_continuous(limits = c(0, 1))+
  10. 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"),
  11. values = c(cols[1],cols[2],cols[3]))
  12. gdf = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
  13. data.frame(model ="C",x=10:1,val=runif(n=10,min=0,max=1)))
  14. gplot<-ggplot()+
  15. geom_point(gdf, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
  16. geom_line(gdf, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  17. scale_y_continuous(limits = c(0, 1))+
  18. 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"),
  19. values = c(cols[1],cols[3],cols[2]))
  20. final_plot = ggarrange(plot,gplot,
  21. nrow=1,ncol=2,common.legend = TRUE, legend = "bottom")
  22. **图:**
  23. [![enter image description here][1]][1]
  24. **最新尝试:** 我尝试将数据合并为一个数据集然后进行子集但结果相同
  25. cols=c("red","blue","green")
  26. df = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
  27. 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)),
  28. data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)))
  29. df$ind=1
  30. df$ind[21:40]=0
  31. plot<-ggplot(df)+
  32. 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))+
  33. geom_line(data=df%>%subset(ind==1), mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  34. scale_y_continuous(limits = c(0, 1))+
  35. 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"),
  36. values = c(cols[1],cols[2],cols[3]))
  37. gplot<-ggplot(df)+
  38. 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))+
  39. geom_line(data=df%>%subset(ind==0), mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  40. scale_y_continuous(limits = c(0, 1))+
  41. 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"),
  42. values = c(cols[1],cols[3],cols[2]))
  43. final_plot = ggarrange(plot,gplot,
  44. 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:

  1. cols=c(&quot;red&quot;,&quot;blue&quot;,&quot;green&quot;)
  2. df = rbind(data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
  3. data.frame(model =&quot;B&quot;,x=10:1,val=runif(n=10,min=0,max=1)))
  4. plot&lt;-ggplot()+
  5. geom_point(df, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
  6. geom_line(df, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  7. scale_y_continuous(limits = c(0, 1))+
  8. 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;),
  9. values = c(cols[1],cols[2],cols[3]))
  10. gdf = rbind(data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
  11. data.frame(model =&quot;C&quot;,x=10:1,val=runif(n=10,min=0,max=1)))
  12. gplot&lt;-ggplot()+
  13. geom_point(gdf, mapping=aes(x=x, y=val, group=model, color=model,size=2),size=2)+scale_x_reverse(limits = c(10,0))+
  14. geom_line(gdf, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  15. scale_y_continuous(limits = c(0, 1))+
  16. 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;),
  17. values = c(cols[1],cols[3],cols[2]))
  18. final_plot = ggarrange(plot,gplot,
  19. 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

  1. cols=c(&quot;red&quot;,&quot;blue&quot;,&quot;green&quot;)
  2. df = rbind(data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
  3. 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)),
  4. data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)))
  5. df$ind=1
  6. df$ind[21:40]=0
  7. plot&lt;-ggplot(df)+
  8. 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))+
  9. geom_line(data=df%&gt;%subset(ind==1), mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  10. scale_y_continuous(limits = c(0, 1))+
  11. 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;),
  12. values = c(cols[1],cols[2],cols[3]))
  13. gplot&lt;-ggplot(df)+
  14. 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))+
  15. geom_line(data=df%&gt;%subset(ind==0), mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  16. scale_y_continuous(limits = c(0, 1))+
  17. 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;),
  18. values = c(cols[1],cols[3],cols[2]))
  19. final_plot = ggarrange(plot,gplot,
  20. nrow=1,ncol=2,common.legend = TRUE, legend = &quot;bottom&quot;)

答案1

得分: 1

我成功地用Paul的建议解决了这个问题:

  1. cols=c("red","blue","green")
  2. df = rbind(data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)),
  3. 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)),
  4. data.frame(model ="A",x=10:1,val=runif(n=10,min=0,max=1)))
  5. df$ind=1
  6. df$ind[21:40]=0
  7. plot<-ggplot(df)+
  8. 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))+
  9. geom_line(data=df, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  10. scale_y_continuous(limits = c(0, 1))+
  11. 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"),
  12. values = c(cols[1],cols[2],cols[3]))+facet_wrap(~ ind)

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

英文:

I managed to solve this using the suggestion by Paul:

  1. cols=c(&quot;red&quot;,&quot;blue&quot;,&quot;green&quot;)
  2. df = rbind(data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)),
  3. 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)),
  4. data.frame(model =&quot;A&quot;,x=10:1,val=runif(n=10,min=0,max=1)))
  5. df$ind=1
  6. df$ind[21:40]=0
  7. plot&lt;-ggplot(df)+
  8. 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))+
  9. geom_line(data=df, mapping=aes(x=x, y=val, group=model, color=model,size=1),size=1)+
  10. scale_y_continuous(limits = c(0, 1))+
  11. 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;),
  12. 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:

确定