条形图由线连接/如何连接两个以grid.arrange排列的图形R/ggplot2 [英] Bar charts connected by lines / How to connect two graphs arranged with grid.arrange in R / ggplot2

查看:149
本文介绍了条形图由线连接/如何连接两个以grid.arrange排列的图形R/ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Facebook研究中,我发现了这些漂亮的条形图,这些条形图通过线相连以指示等级变化:

At Facebook research, I found these beautiful bar charts which are connected by lines to indicate rank changes:

https://research.fb.com/do-jobs-亲子游/

我想使用ggplot2创建它们.条形图部分很简单:

I would like to create them using ggplot2. The bar-chart-part was easy:

library(ggplot2)
library(ggpubr)
state1 <- data.frame(state=c(rep("ALABAMA",3), rep("CALIFORNIA",3)), 
                 value=c(61,94,27,10,30,77), 
                 type=rep(c("state","local","fed"),2),
                 cumSum=c(rep(182,3), rep(117,3)))
state2 <- data.frame(state=c(rep("ALABAMA",3), rep("CALIFORNIA",3)), 
                 value=c(10,30,7,61,94,27), 
                 type=rep(c("state","local","fed"),2),
                 cumSum=c(rep(117,3), rep(182,3)))
fill <- c("#40b8d0", "#b2d183", "#F9756D")

p1 <- ggplot(data = state1) +
  geom_bar(aes(x = reorder(state, value), y = value, fill = type), stat="identity") +
  theme_bw() + 
  scale_fill_manual(values=fill) + 
  labs(x="", y="Total budget in 1M$") +
  theme(legend.position="none", 
        legend.direction="horizontal", 
        legend.title = element_blank(),
        axis.line = element_line(size=1, colour = "black"),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.border = element_blank(), panel.background = element_blank()) +
  coord_flip() 

p2 <- ggplot(data = state2) +
  geom_bar(aes(x = reorder(state, value), y = value, fill = type), stat="identity") +
  theme_bw() + 
  scale_fill_manual(values=fill) + labs(x="", y="Total budget in 1M$") +
  theme(legend.position="none", 
        legend.direction="horizontal", 
        legend.title = element_blank(),
        axis.line = element_line(size=1, colour = "black"),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.border = element_blank(), 
        panel.background = element_blank()) +
  scale_x_discrete(position = "top") + 
  scale_y_reverse() +
  coord_flip()

p3 <- ggarrange(p1, p2, common.legend = TRUE, legend = "bottom")

但是我无法提出解决方案.添加行时靠左

But I couldn't come up with a solution to the line-part. When adding lines e.g. to the left side by

p3 + geom_segment(aes(x = rep(1:2, each=3), xend = rep(1:10, each=3), 
                   y = cumSum[order(cumSum)], yend=cumSum[order(cumSum)]+10), size = 1.2)

问题是线条将无法越过右侧. 看起来像这样:

The problem is that the lines will not be able to cross over to the right side. It looks like this:

基本上,我想将左侧的加利福尼亚"栏与右侧的加利福尼亚"栏连接起来.

Basically, I would like to connect the 'California' bar on the left with the Caifornia bar on the right.

我认为,要做到这一点,我必须以某种方式访问​​图的上级.我已经研究了视口,并能够用geom_segment制成的图表覆盖两个条形图,但是后来我找不到正确的线条布局:

To do that, I think, I have to get access to the superordinate level of the graph somehow. I've looked into viewports and was able to overlay the two bar charts with a chart made out of geom_segment but then I couldn't figure out the right layout for the lines:

subplot <- ggplot(data = state1) + 
  geom_segment(aes(x = rep(1:2, each=3), xend = rep(1:2, each=3), 
                   y = cumSum[order(cumSum)], yend =cumSum[order(cumSum)]+10), 
               size = 1.2)

vp <- viewport(width = 1, height = 1, x = 1, y = unit(0.7, "lines"), 
               just ="right", "bottom"))
print(p3)
print(subplot, vp = vp)

非常感谢帮助或指针.

推荐答案

这是一个非常有趣的问题.我使用patchwork库对其进行了近似,该库使您可以将ggplot一起添加,并为您提供一种简单的方法来控制其布局-我更喜欢它胜过基于grid.arrange的任何事情,并且在某些情况下它可以更好地工作比cowplot.

This is a really interesting problem. I approximated it using the patchwork library, which lets you add ggplots together and gives you an easy way to control their layout—I much prefer it to doing anything grid.arrange-based, and for some things it works better than cowplot.

我扩展了数据集只是为了在两个数据框中获得更多的值.

I expanded on the dataset just to get some more values in the two data frames.

library(tidyverse)
library(patchwork)

set.seed(1017)

state1 <- data_frame(
  state = rep(state.name[1:5], each = 3),
  value = floor(runif(15, 1, 100)),
  type = rep(c("state", "local", "fed"), times = 5)
)

state2 <- data_frame(
  state = rep(state.name[1:5], each = 3),
  value = floor(runif(15, 1, 100)),
  type = rep(c("state", "local", "fed"), times = 5)
)

然后我制作了一个数据框,该数据框根据原始数据框中的其他值(状态1或状态2)为每个状态分配等级.

Then I made a data frame that assigns ranks to each state based on other values in their original data frame (state1 or state2).

ranks <- bind_rows(
  state1 %>% mutate(position = 1),
  state2 %>% mutate(position = 2)
)  %>%
  group_by(position, state) %>%
  summarise(state_total = sum(value)) %>%
  mutate(rank = dense_rank(state_total)) %>%
  ungroup()

我做了一个快速主题,使事情保持在最低限度并掉落了轴标记:

I made a quick theme to keep things very minimal and drop axis marks:

theme_min <- function(...) theme_minimal(...) +
  theme(panel.grid = element_blank(), legend.position = "none", axis.title = element_blank())

凹凸图(中间)基于ranks数据框,并且没有标签.在位置和等级上使用因子而不是数值变量可以使我对间距有更多的控制,并使等级以与条形图中的状态名称匹配的方式与离散的1到5个值对齐.

The bump chart (the middle one) is based on the ranks data frame, and has no labels. Using factors instead of numeric variables for position and rank gave me a little more control over spacing, and lets the ranks line up with discrete 1 through 5 values in a way that will match the state names in the bar charts.

p_ranks <- ggplot(ranks, aes(x = as.factor(position), y = as.factor(rank), group = state)) +
  geom_path() +
  scale_x_discrete(breaks = NULL, expand = expand_scale(add = 0.1)) +
  scale_y_discrete(breaks = NULL) +
  theme_min()
p_ranks

对于左条形图,我按值对状态进行排序,然后将值变为负数以指向左侧,然后为其指定相同的最小主题:

For the left bar chart, I sort the states by value and turn the values negative to point to the left, then give it the same minimal theme:

p_left <- state1 %>%
  mutate(state = as.factor(state) %>% fct_reorder(value, sum)) %>%
  arrange(state) %>%
  mutate(value = value * -1) %>%
  ggplot(aes(x = state, y = value, fill = type)) +
    geom_col(position = "stack") +
    coord_flip() +
    scale_y_continuous(breaks = NULL) +
    theme_min() +
    scale_fill_brewer()
p_left

右边的条形图几乎相同,除了值保持正值并且我将x轴移到顶部(当我翻转坐标时正确):

The right bar chart is pretty much the same, except the values stay positive and I moved the x-axis to the top (becomes right when I flip the coordinates):

p_right <- state2 %>%
  mutate(state = as.factor(state) %>% fct_reorder(value, sum)) %>%
  arrange(state) %>%
  ggplot(aes(x = state, y = value, fill = type)) +
    geom_col(position = "stack") +
    coord_flip() +
    scale_x_discrete(position = "top") +
    scale_y_continuous(breaks = NULL) +
    theme_min() +
    scale_fill_brewer()

然后,因为我已经加载了patchwork,所以我可以将这些图加在一起并指定布局.

Then because I've loaded patchwork, I can add the plots together and specify the layout.

p_left + p_ranks + p_right +
  plot_layout(nrow = 1)

您可能需要更多地调整间距和边距,例如通过带有凹凸图的expand_scale调用.我还没有尝试过沿y轴使用轴标记(即翻转后的底部),但是我有种感觉,如果不将虚拟轴添加到行列中,事情可能会被甩掉.还有很多东西要摆弄,但这是您提出的一个很酷的可视化项目!

You may want to adjust spacing and margins some more, such as with the expand_scale call with the bump chart. I haven't tried this with axis marks along the y-axes (i.e. bottoms after flipping), but I have a feeling things might get thrown out of whack if you don't add a dummy axis to the ranks. Plenty still to mess around with, but it's a cool visualization project you posed!

这篇关于条形图由线连接/如何连接两个以grid.arrange排列的图形R/ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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