LoginSignup
0
0
アクセシビリティの知見を発信しよう!

【ggplot2】 legendが消えたまま出てこないぞ!

Posted at

【ggplot2】 legendが消えたまま出てこないぞ!

こんにちは!rのggplot2()で作図していたときに、以下のようにtheme()内でlegendをしっかり記述したのにも関わらず、legendが作図の上には出てこない時、ありますよね!(ないよー)

qiita.rb
theme(panel.background = element_rect(fill="white",color = NA), 
        plot.background = element_rect(fill = "white", color = NA),
        panel.grid.major =  element_line(color = "Gainsboro"),
       
        axis.title.x  = element_text(size =15,face="bold"),
        axis.title.y  = element_text(size =15,face="bold"),
        axis.text.y  = element_text(size =15),
        axis.text.x = element_text(colour="black",size=13,
           margin = margin(t = 0, r = 0, b = 0.5, l = 0,unit = "cm")),
    strip.text.x = element_text(size =16),
    legend.position=c(0.3,0.9),legend.justification=c(0.80,0.85),
        panel.border = element_blank(),
      legend.text=element_text(size=15),
      legend.title=element_text(size=15),
      legend.background = element_rect(fill="white",linewidth = 0.5, linetype = "solid", color = "Gainsboro"))

もし、theme()の中身に何も問題がないのなら、
大体は、breaks=c()の中身が間違っていることが原因です。
私の場合は

qiita.rb
      scale_color_viridis_d(option = "B", begin = .6, end = .9,
                  name="Number of Partners Changed",
                   breaks=c("zero","one","two","three"),
                 labels=c("0","1","2","3+")) 

上のbreaks=c()の中身を下に戻したらlegendが戻ってきました!

qiita.rb
            scale_color_viridis_d(option = "B", begin = .6, end = .9,
                  name="Number of Partners Changed",
                   breaks=c("0","1","2","3+"),
                 labels=c("0","1","2","3+")) 

誰もこんな見落とししないでしょうから誰得なんだろーって記事でした! ノシ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0