g = ggplot(df) + aes(x=y, y=n, fill=a) + geom_col(colour="black",position="stack") + scale_fill_manual(values=rev(mycolor)) + theme(panel.background=NULL,axis.title=element_blank(),axis.text=element_blank(),rect=element_blank(),line=element_blank()) plot(g)
library("lemon") # ... # plot(g) # を以下のコードで置き換える legend = lemon::g_legend(g) grid::grid.newpage() #png(file="legend_with_lemon.png", width=200, height=700, res=300) grid::grid.draw(legend) #dev.off()すると凡例だけが描画される。
library("gtable") #... # plot(g) # を以下のコードで置き換える legend = gtable::gtable_filter(ggplotGrob(g), "guide-box") grid::grid.newpage() #png(file="legend_with_gtable.png", width=200, height=700, res=300) grid::grid.draw(legend) #dev.off()
library("ggpubr") # ... # plot(g) # を以下のコードで置き換える legend = get_legend(g) #png(file="legend_with_ggpubr.png", width=200, height=700, res=300) as_ggplot(legend) #dev.off()