没有ggplot这个函数_gggibbous包:让ggplot点变成月有阴晴圆缺,更有诗情画意

aadf351dc50606722835cb982cdd933f.png
gggibbous包:让ggplot点变成月有阴晴圆缺,更有诗情画意​mp.weixin.qq.com
c112e8b4bee9a4c5101f76cb04dcd29c.png

写在前面

虽然微生信生物写过许多的东西,但是确实做好很细心,很认真,并且考虑到大家的运行环境的教程并不多,随着我的成长,这些方面的东西,也慢慢在大家的反馈中得到了注意,并逐渐提高。比如今天这篇:有的话请留言,会有人运行不出来吗?

月牙

虽然ggpot中支出20多种电点形状,但是却没有这样一个点向月亮一样引人注目,今天这个包来了,为了让我们都的图形像月亮一样引人注目。

实战

安装载入依赖R包

#--下载安装包#----
install.packages("gggibbous")

#-载入包#——-------
library(gggibbous)
library(ggplot2)

构造数据 基本月牙绘制

#> Loading required package: ggplot2
dat <- data.frame(x = 1:5, y = 1, size = 2^(0:4))

dat
#----使用geom_moon函数绘制月牙
ggplot(data = dat, aes(x, y, size = size)) +
  geom_moon()

———-图

371142fb86c0e30719d92efd5b62e12c.png

下面我们比对一下moom和geom_point的大小关系

#--对比月牙和普通原点的大小关系#---
ggplot(data = dat, aes(x, y, size = size)) +
  geom_moon() +
  geom_point(y = 2) +
  lims(x = c(0.5, 5.5), y = c(0.5, 2.5)) +
  scale_size(range = c(5, 10))

28c3096c4f2ab0a5d5bb9ed09c879b1f.png

填充不同的颜色

#------设置填充为黑色#-------
dat = data.frame(x = 1:5, y = 0, ratio = 0:4 * 0.25)
dat
ggplot(data = dat, aes(x = x, y = y)) +
  geom_moon(aes(ratio = ratio), size = 20, fill = "black") +
  geom_text(aes(y = y + 1, label = ratio)) +
  lims(x = c(0.5, 5.5), y = c(-1, 1.4)) +
  theme_void()

66c1b6e2748bd20d54accc10dce9cd24.png

如何绘制不同方向的月牙

key_glyph:参数,默认朝左

tidymoons <- data.frame(
x = rep(1:3, 6),
y = rep(rep(3:1, each = 3), 2),
ratio = c(1:9 / 10, 9:1 / 10),
right = rep(c(TRUE, FALSE), each = 9)
)

##--如何话朝向右边的月牙#-------
ggplot() +
  geom_moon(
    data = subset(tidymoons, !right),aes(x, y, ratio = ratio, right = right), fill = "turquoise3",
    key_glyph = draw_key_moon_left
  ) +
  lims(x = c(0.5, 3.5), y = c(0.5, 3.5))

f99544abdc42df72d1381bbd8c9d8a80.png

对月牙两个部分都进行填充

#-如何设置左右两个部分的颜色#-----------

ggplot(tidymoons) +
  geom_moon(aes(x, y, ratio = ratio, right = right, fill = right)) +
  lims(x = c(0.5, 3.5), y = c(0.5, 3.5))

#------左右颜色,同时映射大小#-------

ggplot(tidymoons, aes(x, y, ratio = ratio, right = right, size = 2^x)) +
  geom_moon(data = subset(tidymoons, right), fill = "violetred") +
  geom_moon(
    data = subset(tidymoons, !right), fill = "turquoise3",
    key_glyph = draw_key_moon_left
  ) +
  lims(x = c(0.5, 3.5), y = c(0.5, 3.5)) +
  scale_size("size", range = c(5, 10), breaks = 2^(1:3))

8a49aba6a232ed22d3c65829ebce85b1.png
ggplot(tidymoons) +
  geom_moon(
    aes(x, y, ratio = ratio, right = right, fill = right, size = 2^x),
    key_glyph = draw_key_full_moon
  ) +
  lims(x = c(0.5, 3.5), y = c(0.5, 3.5)) +
  scale_size("size", range = c(5, 10), breaks = 2^(1:3)) +
  scale_fill_manual(values = c("firebrick1", "dodgerblue2")) +
  theme(legend.box = "horizontal")

01668fa2e5a39cd9d78b273dfd0ab340.png

在不同图形中叠加使用moon

这一点作为gplot对象来讲,已经不奇怪了,下面是例子

#---作为多对象同地图结合

dmeladh_adj <- dmeladh
dmeladh_adj$long <- dmeladh$Longitude + c(
  -2, 0, -2, 2, -3, 3, 3, 2, 3, 4, -2.5, -2.5, -1, -2, -2.5, -4, 2.5,
  5, 6, 7, 2, -7, -5.5, -3, 0, -7, -2, 3, 5.5, 0.5, -1, -1.5, -3, 2)
dmeladh_adj$lat <- dmeladh$Latitude + c(
  -2, 2, 0, 1, 0, 0, 0, 2, 0.5, -1, 1, -1.5, 2, 4, 1.5, 0, 2,
  1, -1, -3, -2, 1, -1, -2, -3, -2, -4, -3, -1, 1.5, 2, 2, -2, 0)

moonmap <- ggplot(dmeladh_adj, aes(long, lat)) +
  geom_polygon(
    data = map_data(
      "world", region = "(Australia)|(Indonesia)|(Papua New Guinea)"),
    aes(group = group),
    fill = "gray80"
  ) +
  geom_segment(aes(xend = Longitude, yend = Latitude), color = "gray20") +
  geom_point(aes(Longitude, Latitude), size = 0.75, color = "gray20") +
  scale_size(range = c(4, 10)) +
  coord_map(xlim = c(110, 160), ylim = c(-45, -5)) +
  theme_void() +
  theme(
    legend.position = c(0.05, 0.05),
    legend.direction = "horizontal",
    legend.justification = c(0, 0)
  )

moonmap +
  geom_moon(
    aes(ratio = AdhS / 100, size = N),
    right = FALSE, fill = "gold", color = "gold",
    key_glyph = draw_key_moon_left
  ) +
  geom_moon(
    aes(ratio = AdhF / 100, size = N),
    fill = "forestgreen", color = "forestgreen"
  )

f21dcddc20f67d9dcdafa32055e689ae.png

叠加,月牙点线图

moonphase <- subset(lunardist, !is.na(phase))
moonphase$percent <- ifelse(
  moonphase$phase == "new", 0, ifelse(moonphase$phase == "full", 1, 0.5))

ggplot(lunardist, aes(date, distance)) +
  geom_line() +
  # Plotting the lower layer as a full circle also works in most cases
  geom_moon(data = moonphase, ratio = 1, size = 5, fill = "black") +
  geom_moon(
    data = moonphase, aes(ratio = percent),
    size = 5, fill = "yellow", right = moonphase$phase == "first quarter"
  )

4fbd5d033876c73e62e3c7a435d45f79.png

添加主编微信 加入群聊

71ac79f5e0d195fae4fd81deee1ec32f.png
数据分析圈 - 知乎​www.zhihu.com
9f5425327d60a017dfaaaa3ad6273a00.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值