gpt4 book ai didi

r - 在 R 图中使用图像作为区域填充

转载 作者:行者123 更新时间:2023-12-03 21:57:52 25 4
gpt4 key购买 nike

我怎样才能做一个类似于这个的图(美元图像图)?或者我可以用什么包来做到这一点?我有一个使用 geom_area 的 (gg) 图。

美元图图像

Dollar graph image

最佳答案

这纯粹是为了新奇的目的,对吧?

在下面的代码中,我们使用 geom_ribbon 覆盖了蓝色曲线上方的美元钞票部分。 .

library(jpeg)
library(grid)
library(ggplot2)
library(scales)
theme_set(theme_classic())

# Load image of dollar bill and convert to raster grob
download.file("https://2marks.files.wordpress.com/2013/07/george-washington-on-one-dollar-bill.jpg",
"dollar_bill.jpg")
db = readJPEG("dollar_bill.jpg")
db = rasterGrob(db, interpolate=TRUE)

# Fake data
set.seed(3)
dat = data.frame(x=1913:2009)
dat$y2 = seq(5,950, length=nrow(dat)) + rnorm(nrow(dat), 0, 5)
dat$y1 = seq(100,5,length=nrow(dat)) + c(0, -0.5, rnorm(nrow(dat) - 2, 0, 2))

ggplot(dat, aes(x, y1)) +
annotation_custom(db, xmin=1913, xmax=2009, ymin=0, ymax=100) +
geom_ribbon(aes(ymin=y1, ymax=100), fill="white") +
geom_line(size=1, colour="blue") +
geom_line(aes(y=y2/10), size=1, colour="red") +
coord_fixed(ratio=1/2.5) +
scale_y_continuous(limits=c(0,100), expand=c(0,0),
sec.axis=sec_axis(~.*10, name="Currency in Circulation\n(billions)", labels=dollar)) +
scale_x_continuous(limits=c(1913,2009), expand=c(0,0)) +
labs(x="", y="Purchasing Power\n(1913=100)") +
theme(axis.text.y.right=element_text(colour="red"),
axis.title.y.right=element_text(colour="red"),
axis.text.y=element_text(colour="blue"),
axis.title.y=element_text(colour="blue"),
axis.title.x=element_blank())

enter image description here

关于r - 在 R 图中使用图像作为区域填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45777519/

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