gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:39:08 29 4
gpt4 key购买 nike

我正在寻找一种方法,使用 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com