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

标签 r ggplot2 grid

我正在寻找一种方法,使用 ggplot2 或网格来制作如下所示的图表。我可以在 Tableau 中重新创建此内容,但不确定从哪里开始(数据设置、包)在 R 中执行此操作。任何重新创建此内容的帮助都会很棒!我希望将来能使用这样的图表。

4 quadrant square area proportion chart

最佳答案

您可以尝试使用此功能。

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)

enter image description here

关于r - 在R中制作四象限比例面积图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57923246/

相关文章:

html - 使用 Bootstrap 显示 3 列数据?数据显示为一行

html - 将文本对齐到 div 的底部 : am I confused about CSS or about blueprint?

javascript - 如何在网格的列中添加 extjs 按钮以显示另一个窗口?

r - 在月轴上准确放置 geom_text 标签

r - gsub ("\n"中的错误,br(),a,已修复 = TRUE)

r - 指数风险模型系数的相反方向(与 survreg 和 glm 泊松)

r - R中的更新功能不更新模型

css - 使用 renderPlot() 删除 R Shiny ggplot 中 geom_sf() 对象周围的白边

r - Plotly 中的金字塔图

r - 在R中以列表形式存储结果以便以后通过名称(而不是索引)进行访问的一种好策略是什么?