在R中制作四象限比例面积图

Ant*_*idt 5 grid r ggplot2

我正在寻找一种使用ggplot2或网格的方法来制作像下面这样的图表。我可以在Tableau中重新创建它,但是不确定在R中从哪里开始(数据设置,程序包)。重新创建它的任何帮助都很棒!我希望将来使用这样的图表。

4象限平方面积比例图

Mar*_*dri 5

您可以尝试使用此功能。

four_quadrant <- function(x, col_quad="gray65", col_text="white") {    
  nx <- length(x)
  sqx <- sqrt(x) 
  df <- data.frame(x=c(sqx[1],-sqx[2],-sqx[3],sqx[4])/2, 
                   y=c(sqx[1],sqx[2],-sqx[3],-sqx[4])/2, 
                   size=sqx, label=x)
  mm <- max(df$size)*1.1

  ggplot(data=df, aes(x=x, y=y, width=size, height=size, 
            group=factor(size))) +
    geom_tile(fill=col_quad) +
    geom_text(aes(label=label), col=col_text, size=5) +
    geom_hline(aes(yintercept=0), size=0.8) +
    geom_vline(aes(xintercept=0), size=0.8) +
    coord_fixed() +
    xlim(c(-mm,mm)) + ylim(c(-mm,mm)) +
    theme_void() +
    theme(legend.position = "none")
}

x <- c(18, 54, 5, 15)
p1 <- four_quadrant(x)

x <- c(30, 17, 6, 34)
p2 <- four_quadrant(x, col_quad="salmon")

gridExtra::grid.arrange(p1, p2, nrow=1)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Ari*_*hod 5

你可以很容易地ggplot使用geom_rect. 我已经创建了第一个图表的模型数据来向您展示如何创建一个图。您可以重用它来创建其他人并将它们组合在一起使用grid(关于如何执行此操作的示例有很多)。

library(tidyverse)

df <- data.frame(perc = c(54, 18, 5, 15),
                 wall_policy = c("oppose", "favor", "oppose", "favor"),
                 dreamer_policy = c("favor", "favor", "oppose", "oppose"),
                 stringsAsFactors = FALSE)
df <- df %>% 
  mutate(xmin = if_else(wall_policy == "oppose", -sqrt(perc), 0),
         xmax = if_else(wall_policy == "favor", sqrt(perc), 0),
         ymin = if_else(dreamer_policy == "oppose", -sqrt(perc), 0),
         ymax = if_else(dreamer_policy == "favor", sqrt(perc), 0))
ggplot(df) + 
  geom_rect(aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill = "grey") +
  geom_text(aes(x = xmin + 0.5*sqrt(perc),
                y = ymin + 0.5*sqrt(perc),
                label = perc),
            color = "white", size = 10) +
  coord_equal() +
  geom_hline(yintercept = 0) +
  geom_vline(xintercept = 0) +
  labs(title = "Total") +
  theme_minimal() +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        panel.grid = element_blank(),
        plot.title = element_text(color="grey40", face="bold", 
                                  size=20, hjust = 0.5))
Run Code Online (Sandbox Code Playgroud)

例子