从两个不同的数据集和图层

时间:2017-10-20 20:40:18

标签: r ggplot2 legend

我将ggplot中的两个图层组合在一起,这两个图层是从两个不同的数据集创建的,并希望控制图例的显示顺序。

使用示例数据和代码:

base <- 
data.frame(idea_num = c(1, 2), 
           value = c(-50, 90), 
           it_cost = c(30, 10))

group <- 
data.frame(idea_num = c(1, 1, 2, 2), 
           group = c("a", "b", "a", "b"), 
           is_primary = c(TRUE, FALSE, FALSE, TRUE), 
           group_value = c(-40, -10, 20, 70))

base %>% 
left_join(group) %>%
arrange(desc(value)) %>%
mutate(idea_num = idea_num %>% factor(levels = unique(idea_num)), 
       is_primary = is_primary %>% factor(levels = c("TRUE", "FALSE"))) %>%
ggplot(aes(x = idea_num, y = group_value, fill = is_primary)) +
geom_bar(stat = "identity") +
geom_bar(data = base %>% 
             arrange(desc(value)) %>% 
             mutate(idea_num = idea_num %>% factor(levels = unique(idea_num))),
         aes(x = idea_num, y = it_cost, alpha = 0.1, fill = "it_cost"), 
         stat = "identity") +
scale_fill_manual(name = "Group", labels = c("TRUE" = "Primary", "FALSE" = "Secondary", "it_cost" = "IT Cost"), 
                  values = c("TRUE" = "blue", "FALSE" = "red",  "it_cost" = "black")) +
scale_alpha(guide = "none") +
theme(legend.position = "bottom")

我得到一个数字

enter image description here

但我希望图例按PrimarySecondaryIT Cost的顺序显示。

如果我试图绘制相同大数字的一部分的所有数字,我可以轻松地melt数据帧并将所有内容相加;但是,group$group_value的值需要与base$it_cost分开显示。

如果我只绘制第一层的值,即

base %>% 
left_join(group) %>%
arrange(desc(value)) %>%
mutate(idea_num = idea_num %>% factor(levels = unique(idea_num)), 
       is_primary = is_primary %>% factor(levels = c("TRUE", "FALSE"))) %>%
ggplot(aes(x = idea_num, y = group_value, fill = is_primary)) +
geom_bar(stat = "identity") +
scale_fill_manual(name = "Group", labels = c("TRUE" = "Primary", "FALSE" = "Secondary"), 
                  values = c("TRUE" = "blue", "FALSE" = "red")) +
theme(legend.position = "bottom")

我得到了一个我期待的数字

enter image description here

如何添加第二层并调整图例框的顺序?我不相信this questionthis question与我的完全相关,因为前者处理因子的水平而后者处理多个传说的排序。

我可以做我想做的事吗?有没有更好的方法来构建这个情节?

2 个答案:

答案 0 :(得分:0)

使用scale_fill_manual(..., limit=, ...)

... +
  scale_fill_manual(name = "Group",
                    labels = c("TRUE" = "Primary", "FALSE" = "Secondary", "it_cost" = "IT Cost"), 
                    limits = c("TRUE", "FALSE", "it_cost"), 
                    values = c("TRUE" = "blue", "FALSE" = "red",  "it_cost" = "black")) +
  ...

这给出了:

enter image description here

那就是说,我想你可能想要考虑几种不同的方法:

答:为什么要以如此复杂的方式创建数据,最终对同一创意数量的IT成本进行多次观察?我不知道你的数据,你可能有你的理由,但是有一个简单的数据集:

  idea_num value      type
1        1   -40   Primary
2        1   -10 Secondary
3        2    20 Secondary
4        2    70   Primary
5        1   -50   IT Cost
6        2    90   IT Cost

会简化一些事情。

B:你为什么要叠加/叠加这两个单独的条形图?我会position="dodge"代替单独的酒吧。

答案 1 :(得分:0)

df2 <- base %>% 
  left_join(group) %>% 
  mutate(is_primary=paste0("pri_", is_primary+0)) %>%
  spread(is_primary, group_value) %>%
  gather(yvar, y, it_cost, pri_0, pri_1)

df2$yvar <- factor(df2$yvar, levels=c("pri_0", "pri_1", "it_cost"), 
             labels=c("Primary", "Secondary", "IT Cost")) 
df2$idea_num <- factor(df2$idea_num, levels=c(2, 1))

ggplot(df2, aes(idea_num, y, fill=yvar)) + 
  geom_bar(stat="identity") +
  scale_fill_manual("Group", values=c("blue", "red", "black")) +
  scale_alpha(guide = "none") +
  theme(legend.position = "bottom")

enter image description here