hexSticker——绘制六边形贴纸

hexSticker——绘制六边形贴纸

该项目的地址为:GuangchuangYu/hexSticker,可以用来为自己的项目绘制六边形贴纸。

我也为自己的FMFE包绘制了一个哈:



安装

1
devtools::install_github("GuangchuangYu/hexSticker")

依赖

  1. 安装imageMagick
  2. 安装xquartz:
1
2
# 在终端运行
brew install homebrew/cask/xquartz

使用

添加基础图形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
library(hexSticker, help, pos = 2, lib.loc = NULL)
p <- function() {
plot(
cars, cex = 0.5, cex.axis = 0.5, mgp = c(0, 0.3, 0),
xlab = "", ylab = ""
)
}

sticker(expression(p()),
package = "BasePlot",
p_size = 8, # 控制文本的大小
s_x = 0.8, # 控制图片横向位置,越大越靠右
s_y = 0.7, # 控制图片纵向位置,越大越靠上
s_width = 1.5, # 控制图片的宽度
s_height = 1.2, # 控制图片的高度
filename = "baseplot.png" # 图片名称
)

添加lattice图形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
library(lattice)
counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12)
outcome <- gl(3, 1, 9)
treatment <- gl(3, 3)
bwplot <- bwplot(
counts ~ outcome | treatment,
xlab = NULL,
ylab = NULL,
cex = 0.5,
scales = list(cex = 0.5),
par.strip.text = list(cex = 0.5)
)

sticker(
bwplot,
package = "lattice",
p_size = 8,
s_x = 1.05,
s_y = 0.75,
s_width = 2,
s_height = 1.5,
h_fill = "#f9690e", # 填充色
h_color = "#f39c12", # 边缘的颜色
filename = "lattice.png"
)

添加ggplot2图片

这里我用ggplot2画了个蝶式期权交易策略的损益图,然后为FMFE制作了一个小贴纸:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# install.packages("hexSticker")
library(hexSticker)
library(ggplot2)
library(ggthemes)
K1 = 45 # 第一只期权(call)的执行价格
opt1 = 5 # 第一只期权的价值
K2 = 50 # 第二只期权(put)的执行价格
opt2 = 3 # 第二只期权的价值
K3 = 55 # 第三只期权(call)的执行价格
opt3 = 1 # 第三只期权的价值
x = seq(K1 - K1/4, K3 + K3/4, 0.01) # 股价序列
y1 = ifelse(x >= K1, x - K1 - opt1, -opt1) # 第一只期权的payoff
y3 = ifelse(x >= K3, x - K3 - opt3, -opt3) # 第三只期权的payoff
y2 = 2 * ifelse(x >= K2, K2 - x + opt2, opt2) # 第二只期权的payoff
y = y1 + y2 + y3 # 策略的payoff
df <- data.frame(
x, y1, y2, y3, y
)
library(reshape2)
df2 <- melt(df, id.vars = "x")
p <-
ggplot(df2, aes(x, value, group = variable, colour = variable)) +
geom_line() +
theme_bw(base_size = 15) +
scale_colour_hue(labels = c(
"买权1",
"卖权",
"买权2",
"组合")) +
theme(legend.position = "top",
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20)
) +
labs(title = "图:蝶式策略") +
xlab("股价") +
ylab("支付")

(q <-
ggplot(df2, aes(x, value, group = variable, colour = variable)) +
geom_line() +
theme_bw(base_size = 5) +
theme(legend.position = "top",
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank()
) +
theme(legend.position = "none") +
theme_transparent()
)

sticker(q,
package="FMFE",
p_size=8,
s_x=.95,
s_y=.7,
s_width=1,
s_height=1,
h_fill="#f9690e",
filename="baseplot.png",
h_color="#f39c12"
)

还可以用magick包的image_read()函数将它直接显示在图形框里:

1
2
library(magick)
image_read("FMFE.png")

添加任意图片

1
2
3
4
5
6
7
8
9
10
11
12
13
# 例如添加女票的美照
imgurl <- "http://www.czxa.top/photowall/xiaoxiao/20180326_IMG_3836.JPG"
sticker(
imgurl,
package = "MyGirl",
p_size = 8,
s_x = 1,
s_y = 0.75,
s_width = 0.6,
filename = "hexxiaoxiao.png",
h_fill="#FCB1AA",
h_color="#90CAAF"
)

# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了607.9k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×