打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ggplot2 进阶--多重color/fill scales
userphoto

2023.06.07 上海

关注

ggplot2 迄今是R里最受欢迎画图package,个人感觉最主要的一个因素就是layer叠加的语法结构(图层),类似于photoshop的图层概念。而且,ggplot2的变量映射aes也是很厉害。那么,就有个局限,每个变量只能映射一种scale,那怎么样在同一个图中出现两种或者多种color/fill scale的映射呢?

ggnewscale这个包你值得拥有!
github链接

使用方法】

其实很简单,见如下说明

# 直接在需要增加scale的geom之前加如下代码
?new_scale
#格式:new_scale(new_aes),需要跟geom(aes(shape,color,size...)) 名称对应进行映射
+ new_scale("shape")
+ new_scale("color")
+ new_scale("fill")
+ new_scale("size")
+ new_scale("linetype")
# 或者如下直接:
+ new_scale_fill()
+ new_scale_color()
+ new_scale_colour()

# 再接画图的 geom
+ geom_xxxx(data = xxx,aes(color = xx,shape=xx......))

使用example如下

install.packages(c("ggnewscale","tidyverse"))
library(tidyverse)
library(ggnewscale)

# 创建数据框-1
set.seed(42)
topography <- expand.grid(x = 1:nrow(volcano),
                          y = 1:ncol(volcano))
topography$z <- c(volcano)
topography
#  创建数据框-2
measurements <- data.frame(x = runif(30, 1, 80),
                           y = runif(30, 1, 60),
                           thing = rnorm(30))

ggplot(mapping = aes(x, y)) +
  geom_contour(data = topography, aes(z = z, color = stat(level))) +
  # 为topography设置一个color scale
  scale_color_viridis_c(option = "D") +
  # 接下来创建新的color 下面的 geom将会使用新的scale
  new_scale_color() +
  geom_point(data = measurements, size = 3, aes(color = thing)) +
  # Color scale applied to geoms added after new_scale_color()
  scale_color_viridis_c(option = "A")

pd = data.frame(
  letters = strsplit("AGTGACCGACTATCATAGTGACCCAGAATCATAGTGACCGAGTATGAT", "")[[1]],
  species = rep(c("Human", "Armadillo", "Porcupine"), each=16),
  x       = rep(1:16, 3),
  change  = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
              0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,
              0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0),
  score1  = c(0,0,0,0,0,0,1,1,2,2,2,3,3,3,4,3,
              0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,
              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  score2  = c(0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
              0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0,
              0,0,0,0,3,3,3,3,0,0,0,0,0,0,0,0)
)
ggplot(pd[pd$score1 != 0,], aes(x=x, y=species)) +
   geom_tile(aes(fill  =score1)) +
   scale_fill_gradient2("Score 1", limits = c(0, 4), 
                        low = "#762A83", mid = "white", high = "#1B7837") +
   
   new_scale("fill") +
   
   geom_tile(aes(fill = score2), data = subset(pd, score2 != 0)) +
   scale_fill_gradient2("Score 2", limits = c(0, 3), 
                        low = "#1B7837", mid = "white", high = "#762A83") +
   
   geom_text(data=pd, aes(label = letters, color = factor(change))) +
   scale_color_manual("Change", values = c("black", "#F2A11F"), 
                      labels = c("None", "Some")) +
   coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) 


看到上面的两个例子,是不是觉得这个包无敌,真的是无敌了啊

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ggplot2中的颜色设置,几行代码搞定!
ggplot2绘图学习 小提琴图
R语言可视化学习笔记之ggridges包
scale 修改默认设置,坐标轴的修改
R中scale_(x/y/size/color/fill)_continuous函数修改标题
ggplot2一页多图(组合图
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服