gpt4 book ai didi

r - 差异图

转载 作者:行者123 更新时间:2023-12-04 06:37:01 25 4
gpt4 key购买 nike

我不知道这种情节的名称(欢迎对此发表评论)。本质上,它是一个带有字形的条形图,填充以指示损失/ yield 。字形像箭头一样编码有关方向、大小的信息,并允许看到下方的条形几何图形。

enter image description here

这看起来很有趣,但在ggplot2 中想不出怎么做(网格框架工作)。我们如何在 ggplot2/grid 框架中重新创建这个图(为了问题的完整性也欢迎基本解决方案)。特别是字形,而不是文本,因为这在 ggplot2 中已经很简单了。

下面是一些用于创建数据的代码以及传统的重叠和坐标翻转的躲避条形图和折线图,以显示可视化此类数据的典型方法。

set.seed(10)
x <- sample(30:60, 12)
y <- jitter(x, 60)

library(ggplot2)
dat <- data.frame(
year = rep(2012:2013, each=12),
month = rep(month.abb, 2),
profit = c(x, y)
)


ggplot() +
geom_bar(data=subset(dat, year==2012), aes(x=month, weight=profit)) +
geom_bar(data=subset(dat, year==2013), aes(x=month, weight=profit), width=.5, fill="red")

ggplot(dat, aes(x=month, fill=factor(year))) +
geom_bar(position="dodge", aes(weight=profit)) +
coord_flip

ggplot(dat, aes(x=month, y=profit, group = year, color=factor(year))) +
geom_line(size=1)

enter image description here

enter image description here

enter image description here

最佳答案

这是一个例子,也许还有其他方法,

dat <- data.frame(
year = rep(2012:2013, each=12),
month = factor(rep(1:12, 2), labels=month.abb),
profit = c(x, y)
)
dat2 <- reshape2::dcast(dat, month~ year, value.var = "profit")
names(dat2)[2:3] <- paste0("Y", names(dat2)[2:3])

ggplot(dat2) +
geom_bar(aes(x=month, y = Y2012), stat = "identity", fill = "grey80", width = 0.6) +
geom_segment(aes(x=as.numeric(month)-0.4, xend = as.numeric(month)+0.4, y = Y2013, yend = Y2013)) +
geom_segment(aes(x = month, xend = month, y = Y2013, yend = Y2012, colour = Y2013 < Y2012),
arrow = arrow(60, type = "closed", length = unit(0.1, "inches")), size = 1.5) +
theme_bw()

enter image description here

关于r - 差异图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26248671/

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