使用图像作为区域填充R图 [英] Use an image as area fill in an R plot

查看:119
本文介绍了使用图像作为区域填充R图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何做一个类似于这个图的图(美元图)?或者我可以使用哪些软件包?我有一个使用geom_area的(gg)阴谋。



Dollar graph image

解决方案

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

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

 库(jpeg)
库(网格)
库(ggplot2)
库)
theme_set(theme_classic())

#载入美元钞票的图像并转换为栅格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)

#假数据
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,color = 蓝色)+
geom_line(aes(y = y2 / 10),size = 1,color =red)+
coord_fixed(ratio = 1 / 2.5)+
scale_y_continuous(limits = c(0,100),expand = c(0,0),
sec.axis = sec_axis(〜。* 10,name =货币在循环中\\\
(十亿),labels = dollar))+
scale_x_continuous = c(1913,2009),expand = c(0,0))+
labs(x =,y =采购力量\(1913 = 100))+
主题(axis.text.y.right = element_text(color =red),
axis.title.y.right = element_text(color =red),
axis.text.y = element_text (color =blue),
axis.title.y = element_text(color =blue),
axis.title.x = element_blank())


How can I do a graph similar to this (the dollar image graph)? Or what packages can I do this with? I have a (gg)plot using geom_area.

Dollar graph image

解决方案

This is purely for novelty purposes, right?

In the code below, we cover up the portion of the dollar bill above the blue curve using 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())

这篇关于使用图像作为区域填充R图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆