之前寫(xiě)過(guò)一篇文檔來(lái)介紹如何給標(biāo)簽添加特殊字符,近來(lái)有朋友需要畫(huà)圖正好又遇到更加復(fù)雜的需求豆巨,索性來(lái)一篇文檔對(duì)此進(jìn)行歸納總結(jié)
之前的方法通過(guò)bquote
函數(shù)進(jìn)行特殊字符格式的添加,如下所示
iris %>% as_tibble() %>%
ggplot(aes(Sepal.Length,Sepal.Width,fill=Species))+
geom_point(size=4,pch=21,color="white")+
xlab(bquote(Assimilation (mu~ mol ~CO[2]~ m^-2~s^-1)))+
theme_classic()
如果我們需要在圖中添加特殊字符文本該如何進(jìn)行操作,同時(shí)給不同字符加以不同顏色又該如何進(jìn)行設(shè)置捷枯,ggtext的出現(xiàn)很好的解決了這一問(wèn)題
通過(guò)字符編號(hào)添加特殊字符
iris %>% as_tibble() %>%
ggplot(aes(Sepal.Length,Sepal.Width,fill=Species))+
geom_point(size=4,pch=21,color="white")+
theme_classic()+
labs(x="Assimilation(\u03bc molCO<sub>2</sub>m<sup>-2s</sup>-1)",
y=NULL)+
theme(axis.title.x = element_markdown(color="#3B9AB2",vjust=0.5))
可以看到通過(guò)u03bc 此字符編號(hào)添加了特殊字符,那么有沒(méi)有其它的方法呢专执,當(dāng)然有請(qǐng)繼續(xù)往下看
iris %>% as_tibble() %>%
ggplot(aes(Sepal.Length,Sepal.Width,fill=Species))+
geom_point(size=4,pch=21,color="white")+
theme_classic()+
labs(title="TEST μ",
x="Assimilation(<span style='color:#F98400;'>
μ</span>molCO<sub>2</sub>m<sup>-2s</sup>-1)",
y="<span style='color:#F98400;'>αβ</span>")+
theme(plot.title=element_markdown(),
axis.title.x = element_markdown(color="#3B9AB2"),
axis.title.y = element_markdown())
此圖通過(guò)另外一種方式添加了特殊字符淮捆,也對(duì)部分字符顏色進(jìn)行了定義,那同樣我們也可以對(duì)字體大小等進(jìn)行定義
下面展示一個(gè)小細(xì)節(jié)本股,此處含有端倪
可以看到我們將Y軸文本移至了右邊争剿,因此標(biāo)簽也需要定義到右邊axis.title.y.right
ggplot(data.frame(x = c(-5, 5)), aes(x)) +
stat_function(fun = ~ .x*.x)+
labs(
x = "independent variable *x*",
y = "dependent variable *y* = *x*<sup>2</sup>"
) +
scale_y_continuous(position = "right") +
theme(
axis.title = element_text(color = "#0072B2",
size = rel(1)),
axis.title.x = element_markdown(),
axis.title.y.right = element_markdown()
)
經(jīng)上面的介紹特殊字符的添加已經(jīng)變得非常容易了,但是有沒(méi)有發(fā)現(xiàn)所展示的案例基本都是在圖形外部添加字符痊末,有時(shí)我們也需要在圖內(nèi)部添加這些特殊字符蚕苇,因此這一部分也很重要
內(nèi)部添加特殊字符
定義字符位置信息
df <- tibble(
label = c("SiO<sub>4</sub><sup>4-",
"NO<sup>2-","μ",
"αβ","R<sup>2</sup>=0.001",
"p_value< 2.1e<sup>-16</sup>"),
x = c(4.8,4.8,8,7,6.5,7.5),
y = c(4.1,4.3,2.3,4.2,4,4),
angle=c(0,10,0,0,0,0),
color= c("black","blue","black","red","green","black"))
添加文本
iris %>% as_tibble() %>%
ggplot(aes(Sepal.Length,Sepal.Width,fill=Species))+
geom_point(size=4,pch=21,color="white")+
theme_classic()+
labs(x="Assimilation(\u03bc molCO<sub>2</sub>m<sup>-2s</sup>-1)",
y=NULL)+
theme(axis.title.x = element_markdown(color="#3B9AB2",vjust=0.5))+
geom_richtext(data=df,aes(x,y,label=label,angle=angle,color=color),
fill=NA,label.color=NA,show.legend = F)
文本添加邊框
iris %>% as_tibble() %>%
ggplot(aes(Sepal.Length,Sepal.Width,fill=Species))+
geom_point(size=4,pch=21,color="white",show.legend = F)+
theme_classic()+
labs(x="Assimilation(\u03bc molCO<sub>2</sub>m<sup>-2s</sup>-1)",
y=NULL)+
theme(axis.title.x = element_markdown(color="#3B9AB2",vjust=0.5))+
geom_richtext(data=df,aes(x,y,label=label,angle=angle),
fill=NA,show.legend = F)
添加標(biāo)題
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 3) +
scale_color_manual(
name = NULL,
values = c(setosa = "#0072B2", virginica = "#009E73", versicolor = "#D55E00"),
labels = c(
setosa = "<i style='color:#0072B2'>I. setosa μ </i>",
virginica = "<i style='color:#009E73'>I. virginica μ </i>",
versicolor = "<i style='color:#D55E00'>I. versicolor μ </i>")
) +
labs(
title = "**Fisher's *Iris* dataset(test unicode symbol: μ)**
<span style='font-size:11pt'>Sepal width vs. sepal length for three *Iris*
species μ </span>") +
theme_minimal() +
theme(
plot.title = element_markdown(lineheight = 1.1),
legend.text = element_markdown(size = 11),
axis.title.x = element_markdown(hjust = 0.5),
axis.title.y = element_markdown(vjust = 0.5))
在上圖中我們對(duì)部分字體大小及字體類(lèi)型進(jìn)行了設(shè)置,并進(jìn)行了換行操作凿叠,此處用到了一些markdown語(yǔ)法
給標(biāo)題更豐富的花樣
base <- mtcars %>%
mutate(
transmission = ifelse(am == 1, "automatic", "manual")
) %>%
ggplot(aes(hp, mpg, color = transmission)) +
geom_point(size = 2) +
scale_color_manual(
values = c(automatic = "#0072B2", manual = "#D55E00"),
guide = "none"
) +
labs(
x = "Horse power",
y = "Miles per gallon (MPG)",
title = "Transmission type impacts fuel efficiency<br>
<span style = 'font-size:10pt;'>Miles per gallon (MPG) is on average higher for cars
with <span style = 'color:#0072B2;'>automatic transmission</span> than for cars with
<span style = 'color:#D55E00;'> manual transmission.</span> However, MPG generally
declines with increasing horse power.</span>"
) +
theme_bw() + theme(plot.title.position = "plot")
base +
theme(
plot.title = element_textbox_simple(
size = 14, lineheight = 1, padding = margin(0, 0, 5, 0)
)
)
給標(biāo)題添加邊框
base +
theme(
plot.title = element_textbox_simple(
size = 14, lineheight = 1,
linetype = 1,
box.color = "#748696",
fill = "#F0F7FF",
r = grid::unit(3, "pt"),
padding = margin(5, 5, 5, 5),
margin = margin(0, 0, 10, 0)
)
)
控制邊框位置
base +
theme(
plot.title = element_textbox_simple(
size = 14, lineheight = 1,
width = grid::unit(4, "in"),
hjust = 1,
linetype = 1,
box.color = "#748696",
fill = "#F0F7FF",
r = grid::unit(3, "pt"),
padding = margin(5, 5, 5, 5),
margin = margin(0, 0, 10, 0)
)
)
喜歡的小伙伴歡迎關(guān)注我的公眾號(hào)
R語(yǔ)言數(shù)據(jù)分析指南涩笤,持續(xù)分享數(shù)據(jù)可視化的經(jīng)典案例及一些生信知識(shí),希望對(duì)大家有所幫助