具有不同data.frames的两个geom_bar的不同图例 [英] Different Legends for two geom_bar with different data.frames

查看:58
本文介绍了具有不同data.frames的两个geom_bar的不同图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这使我的脑袋呆了好一阵子...

this one boggles my mind for quite a while ...

我想为具有不同data.frames的两个不同几何(geom_bar)显示两个不同的图例.

I want to show two different legends for two different geoms (geom_bar) with different data.frames.

第一个图例应具有标题"border"(由df.1中的边框填充),第二个图例应具有标题"product"(由df.2中的product填充).这两个data.frame都有column = category共同.

The first legend should have the title "border" (filled by border from df.1) and the second should have the title "product" (filled by product from df.2). Both data.frames have the column=category in common.

你能阐明一些想法吗?

这里是例子

#library(ggplot2)

df.1 <- data.frame(category=c("A","A","A","B","B","B"),
border=c("I","II","III","I","II","III"),
value=c(1,2,1,2,1,2)
)

df.2 <- data.frame(category=c("A","A","A","B","B","B"),
product=c("P1","P2","P3","P1","P2","P3"),
value=c(1,2,3,3,1,2)
)

ggplot()+
geom_bar(aes(x=category, y=value, fill=border), data=df.1, width=.3)+
geom_bar(aes(x=category, y=value, fill=product), data=df.2, position="dodge", width=.25)

推荐答案

一种美学->一个传说是ggplot中的一项基本设计原则.您可以(某种程度上)解决它,但这很困难.看起来还不错的一件事是:

One aesthetic -> one legend is sort of a fundamental design principle in ggplot. You can (sort of) get around it, but it's difficult. One thing to try, that doesn't look too bad, is this:

ggplot()+
    geom_bar(aes(x=category, y=value, fill=border), data=df.1, width=.3)+
    geom_bar(aes(x=category, y=value, colour=product), data=df.2, position="dodge", width=.25,alpha = 0.5)

这篇关于具有不同data.frames的两个geom_bar的不同图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆