小果带你认识R数据可视化之图例设置(一)
在R中,可以使用不同的函数和参数来设置图例的样式和位置,为了图片的美观,我们通常在不同的图片中选择使用不同的图例。那么接下来小果会从图例设置的多个方面来讲述,让小伙伴们可以学习到五彩斑斓的图例设置,每次作图时都能够选择出自己喜欢的图例。在这之前小果还是先强烈推荐一下自己的工具平台(http://www.biocloudservice.com/home.html),那接下来废话不多说,直接开始吧。
1. 图例符号
每种图形类型(如点图、线图、条形图等)都有对应的draw_key_()函数,用于绘制图例中的标记符号。可以使用key_glyph函数来设置不同的draw_key_()函数,以实现自定义的标记符号。
小果来举个简单的例子:
> p <- ggplot(economics, aes(date, psavert, color = "xiao guo"))
> p1 <- p + geom_line()> p2 <- p + geom_line(key_glyph = "timeseries")
> p3 <- p + geom_line(key_glyph = draw_key_timeseries)
> p4 <- p + geom_line(key_glyph = draw_key_rect)
> plot_grid(p1, p2, p3, p4, labels = LETTERS[1:4],nrow = 2)
除了timeseries之外,key_glyph还有其他几种类型,包括color,bar、legend和label,小果在这就不一一展示了。
2. 颜色条
颜色条是用于表示数据范围和对应颜色的图形元素。它通常用于数据可视化中,用于显示数据的变化趋势或不同程度的差异。
小果再来举几个例子:
df <- expand.grid(X1 = 1:20, X2 = 1:20)
$value <- df$X1 * df$X2 df
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
"colourbar") p1 + scale_fill_continuous(guide =
#或者
p1 + scale_fill_continuous(guide = guide_colourbar())
#或者
p1 + guides(fill = guide_colourbar())
小伙伴们要知道guide_colourbar 函数的参数是特别多的,小果接下来主要介绍一些常用的。
> # 设置大小
> p2 <- p1 + guides(fill = guide_colourbar(barwidth = 1, barheight = 20))
> # 删除标签
> p3 <- p1 + guides(fill = guide_colourbar(label = FALSE))
> # 调整标签位置
> p4 <- p1 + guides(fill = guide_colourbar(label.position = "left"))
> # 设置标签主题
> p5 <- p1 + guides(fill = guide_colourbar(label.theme = element_text(colour = "purple", angle = 0)))
> # 颜色条的分箱个数,越大越平滑
> p6 <- p1 + guides(fill = guide_colourbar(nbin = 3))
> p7 <- p1 + guides(fill = guide_colourbar(nbin = 100))
> plot_grid(p1, p2, p3, p4, p5 , p6, p7, labels = LETTERS[1:7], nrow =
4)
3. 基本图例设置
在ggplot2中,guide_legend()函数用于创建一个自定义的图例(legend)。图例是用于解释图形中使用的颜色、形状、大小等可视化属性的图形元素。guide_legend()函数可以用于修改图例的各种属性,包括标题、标记符号、标签、位置等。以下是guide_legend()函数的一些常用参数:
1.title:图例的标题。
2.label:图例的标签。可以使用override.aes参数来自定义标签的属性,例如颜色、字体大小等。
3.keywidth和keyheight:图例标记符号的宽度和高度。
4.direction:图例的排列方向。可以是”horizontal”(水平排列)或”vertical”(垂直排列)。
5.position:图例的位置。可以是”top”、”bottom”、”left”或”right”,或者是一个坐标值。
6.nrow和ncol:图例的行数和列数(仅在position参数为”top”、”bottom”、”left”或”right”时有效)。
接下来小果给大家举几个例子,首先我们先绘制一个简单的图例。
df <- expand.grid(X1 = 1:20, X2 = 1:20)
$value <- df$X1 * df$X2 df
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1 + scale_fill_continuous(guide = guide_legend())
我们再来设置各种样式。
> # 设置图例标题的位置
> p2 <- p1 + guides(fill = guide_legend(title = "xiao guo", title.position = "left"))
> # 使用 element_text 设置图例标题的样式
> p3 <- p1 + guides(fill =
+ guide_legend(title = "xiao guo",
+ title.theme = element_text(
+ size = 20,
+ face = "italic",
+ colour = "purple",
+ angle = 0
+ )
+ )
+ )
> # 设置标签的位置
> p4 <- p1 + guides(fill = guide_legend(title = "xiao guo",label.position = "left", label.hjust = 1))
> # 设置标签的样式
> p5 <- p1 + scale_fill_continuous(breaks = c(5, 10, 15),
+ labels = paste("long", c(5, 10, 15)),+ guide = guide_legend(title = "xiao guo",
+ direction = "horizontal",
+ title.position = "top",
+ label.position = "bottom",
+ label.hjust = 0.5,
+ label.vjust = 1,
+ label.theme = element_text(angle = 90)
+ )
+ )
> plot_grid(p2, p3, p4, p5, labels = LETTERS[1:4], nrow = 2)
由于时间原因,小果今天就先讲到这里啦,对于R数据可视化之图例设置的后半部分内容,小果会在后续继续更新小伙伴们记得继续关注小果的文章哦,最后提醒小伙伴们一定记得要关注小果的工具平台呀
(http://www.biocloudservice.com/home.html),小伙伴们明天见啦。
小果还提供思路设计、定制生信分析、文献思路复现;有需要的小伙伴欢迎直接扫码咨询小果,竭诚为您的科研助力!
/7_B2jkicacLzPjw4xMSqVvoucZjonVDA.jpg)
定制生信分析
服务器租赁
扫码咨询小果
/8_7TvAyUibd1TPhJmZfialJtv6tu2SEg.png)
/9_9N02aBaau07RufXeXRLmvAHsrFZxtw.jpg)
往期回顾
01 |
02 |
03 |
04 |