在ggplot2中图例,删除级别 [英] Legend in ggplot2, remove level
本文介绍了在ggplot2中图例,删除级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据:
df < - data.frame(sp = c(LETTERS [1:8]),
tr = c(NS,LS,NS,LS,LS,HS,HS,HS),
bv = c ,5,11,5.6,21,5.4,2,4.8),
av = c(0.0,14,21,48.4,15,55.6,37,66.2))
我做棒图
图书馆(重新定形2)
df1 < - melt(df,id.vars = c(sp,tr))
ggplot(aes(sp,value,fill = variable) ,data = df1)+ theme_classic()+
geom_bar(aes(lty = tr),lwd = 1.2,data = df1,stat =identity,color =black,width = .8)+
主题(legend.position =bottom)+
scale_linetype_discrete(name =ja)
输出
我不喜欢的是传说。我想从第二部分 ja
中输入行类型,并删除变量
部分。我希望在图例框中有白色背景,而不是灰色背景。 您可以移除<$通过在指南
中设置 fill = FALSE
,c $ c> variable 在 guide_legend
(也位于 guides
中)的
ggplot(df1,aes(sp,value,fill = variable))+
geom_bar( aes(lty = tr),lwd = 1.2,stat =identity,color =black,width = .8)+
scale_linetype_discrete(name =ja)+
guides(fill = FALSE,
lty = guide_legend(override.aes = list(lty = c('dotted','dashed','solid'),
fill =white)))+
theme_classic()+
主题(legend.position =bottom)
如下图所示:
My data:
df <- data.frame(sp = c(LETTERS[1:8]),
tr = c("NS", "LS", "NS", "LS", "LS", "HS", "HS", "HS"),
bv = c(14, 5, 11, 5.6, 21, 5.4, 2, 4.8),
av = c(0.0, 14, 21, 48.4, 15, 55.6, 37, 66.2))
I do the bar plot
library(reshape2)
df1 <- melt(df, id.vars = c("sp", "tr"))
ggplot(aes(sp, value, fill = variable) , data = df1) + theme_classic() +
geom_bar(aes(lty = tr), lwd = 1.2, data = df1, stat = "identity", colour = "black", width =.8) +
theme(legend.position = "bottom" ) +
scale_linetype_discrete(name = "ja")
Output
What I does not like is the legend. I'd like to have just the lines type from the second part "ja
" and remove "variable
" part. I'd like to have the white background in the legend boxes, not the grey one.
解决方案
You can remove the variable
legend by setting fill = FALSE
in guides
and you change the backgroundcolor with override.aes
in guide_legend
(also inside guides
) as follows:
ggplot(df1, aes(sp, value, fill = variable)) +
geom_bar(aes(lty = tr), lwd = 1.2, stat = "identity", colour = "black", width =.8) +
scale_linetype_discrete(name = "ja") +
guides(fill = FALSE,
lty = guide_legend(override.aes = list(lty = c('dotted', 'dashed', 'solid'),
fill = "white"))) +
theme_classic() +
theme(legend.position = "bottom")
this results in the following plot:
这篇关于在ggplot2中图例,删除级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文