gpt4 book ai didi

r - 如何在ggplot2 geom_boxplot中使用实心方 block 作为图例键

转载 作者:行者123 更新时间:2023-12-02 17:43:15 27 4
gpt4 key购买 nike

我正在尝试使用 R ggplot2 包制作箱线图。

但是我只能得到这样的图例。无论如何我可以将那些图例键更改为一个实心正方形,而不是使用那些带有中心线的小盒子吗?

我使用的代码是:

print(ggplot(mydata,aes(x=factor(sp),fill=factor(CommunityType),y=Abundance*100))+geom_boxplot(show_guide=FALSE)
+theme(axis.text = element_text(colour = "black",size=10))
+scale_y_continuous(" RA (%) ")+scale_x_discrete(limits=taxalist[1:5]," ")
+scale_fill_manual(name = "MY type", values = mycol[1:nmc])
+theme_bw() + guides(fill=guide_legend(title=NULL))+theme(legend.position=c(1,1),legend.justification=c(1,1))
+theme(legend.key = element_blank(),legend.key.size = unit(1.5, "lines"))
+theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) )

抱歉,我无法在这里描述我的问题。

最佳答案

这是显示为简单方 block 的图例,使用了我见过 Winston Change 使用的 hack 变体 here . (我正在使用钻石数据集。)

enter image description here

想法是绘制 geom_points(您可以控制其图例,并完全抑制箱线图的图例)

library(ggplot2)
p <- ggplot() + geom_point(data=diamonds, aes(x=cut, y=mean(depth), color=clarity), shape=15, size=5)
p <- p + guides(color=guide_legend(title=NULL))
p <- p + theme(legend.key = element_blank())
p <- p + geom_boxplot(data=diamonds,aes(x=cut,fill=factor(clarity),y=depth)) + guides(fill=FALSE)
p

另请查看 Hadley 的 Legend-Attributes page on github

关于r - 如何在ggplot2 geom_boxplot中使用实心方 block 作为图例键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17998646/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com