R相关

ggplot2

最近想用ggplot2画一个点、线的图,发现有点超纲,搞了好久才搞明白。

要求如下

  1. 点和线的图例分开
  2. 点和线都有不同的颜色
  3. 点不能有边框
  4. 有副坐标轴

注意要点

  1. 数据集尽量写成大长表(术语是什么不太清楚),大概就是数据集的结构最好不要是这种:
          DATE  a  b  c
 1: 2021-01-01  1 11 21
 2: 2021-01-02  2 12 20
 3: 2021-01-03  3 13 21
 4: 2021-01-04  4 14 20
 5: 2021-01-05  5 15 21
 6: 2021-01-06  6 16 20
 7: 2021-01-07  7 17 21
 8: 2021-01-08  8 18 20
 9: 2021-01-09  9 19 21
10: 2021-01-10 10 20 20

而要写成这种:

          DATE variable value
 1: 2021-01-01        a     1
 2: 2021-01-02        a     2
 3: 2021-01-03        a     3
 4: 2021-01-04        a     4
 5: 2021-01-05        a     5
 6: 2021-01-06        a     6
 7: 2021-01-07        a     7
 8: 2021-01-08        a     8
 9: 2021-01-09        a     9
10: 2021-01-10        a    10
11: 2021-01-01        b    11
12: 2021-01-02        b    12
13: 2021-01-03        b    13
14: 2021-01-04        b    14
15: 2021-01-05        b    15
16: 2021-01-06        b    16
17: 2021-01-07        b    17
18: 2021-01-08        b    18
19: 2021-01-09        b    19
20: 2021-01-10        b    20
21: 2021-01-01        c    21
22: 2021-01-02        c    20
23: 2021-01-03        c    21
24: 2021-01-04        c    20
25: 2021-01-05        c    21
26: 2021-01-06        c    20
27: 2021-01-07        c    21
28: 2021-01-08        c    20
29: 2021-01-09        c    21
30: 2021-01-10        c    20
          DATE variable value
  1. 副坐标轴的加法有点反人类。要把副坐标轴的数据映射到主坐标轴的范围内,然后副坐标轴的设置同样根据主坐标轴映射关系进行调整。如:假设主坐标轴的数据范围在[10, 20]之间,而副坐标轴的范围在[-100, 200]之间,则,找到主副坐标轴的线性关系,即y = kx + b(其中,x代表副坐标轴的数据, y代表主坐标轴的数据,该例子中k = (20 - 10) / (200 - (-100)) = 0.03333b = 13.3333),则在画图过程中,用y = kx + b将副坐标轴的数据映射到主坐标轴的范围里,加负坐标轴的时候,用x = (y - b) / k公式构建出副坐标轴。
  2. geom_poinegeom_line在都有颜色colour属性时,若要将point和line的图例区分开,需要在geom_point中使用fill参数(而不要用colour,把这个属性留给line用)。这里需要注意的是,geom_point函数的fill参数,只在shape在21-25之间才有效(这点经常忘,导致每次想用fill参数的时候发现都无效)。然而,shape在21-25之间的时候默认是有边框的(即stroke参数控制的属性),如果想去掉边框,网上好多地方说设置stroke = 0,但是发现不好用,而应该是stroke = NA(猜测这个可能和电脑环境 / R的版本等有关系)。
  3. geom_pointstroke的属性体现不在图例中,即我在geom_point(aes(x = DATE, y = VALUE, fill = POINT, stroke = NA))这样写,发现图例里面point还是有黑边(!!!逼死强迫症!!!),这个时候,需要加上这样一条:p <- p +guides(fill = guide_legend(override.aes = list(stroke = NA))),强制把图例里面的边框去掉!

最后附上代码例子

library(data.table)
library(ggplot2)

sec_dt <- data.table::data.table(
  DATE = c("2021/10/11","2021/10/12","2021/10/13",
           "2021/10/14","2021/10/15","2021/10/18","2021/10/19",
           "2021/10/20","2021/10/21","2021/10/22","2021/10/25","2021/10/26",
           "2021/10/27","2021/10/28","2021/10/29","2021/10/31",
           "2021/11/1","2021/11/2","2021/11/3","2021/11/4","2021/11/5",
           "2021/11/8","2021/10/11","2021/10/12","2021/10/13","2021/10/14",
           "2021/10/15","2021/10/18","2021/10/19","2021/10/20",
           "2021/10/21","2021/10/22","2021/10/25","2021/10/26","2021/10/27",
           "2021/10/28","2021/10/29","2021/10/31","2021/11/1",
           "2021/11/2","2021/11/3","2021/11/4","2021/11/5","2021/11/8",
           "2021/10/11","2021/10/12","2021/10/13","2021/10/14","2021/10/15",
           "2021/10/18","2021/10/19","2021/10/20","2021/10/21",
           "2021/10/22","2021/10/25","2021/10/26","2021/10/27","2021/10/28",
           "2021/10/29","2021/10/31","2021/11/1","2021/11/2",
           "2021/11/3","2021/11/4","2021/11/5","2021/11/8","2021/10/11",
           "2021/10/12","2021/10/13","2021/10/14","2021/10/15","2021/10/18",
           "2021/10/19","2021/10/20","2021/10/21","2021/10/22",
           "2021/10/25","2021/10/26","2021/10/27","2021/10/28","2021/10/29",
           "2021/10/31","2021/11/1","2021/11/2","2021/11/3",
           "2021/11/4","2021/11/5","2021/11/8"),
  LINE = c("QUANTITY","QUANTITY","QUANTITY",
           "QUANTITY","QUANTITY","QUANTITY","QUANTITY","QUANTITY","QUANTITY",
           "QUANTITY","QUANTITY","QUANTITY","QUANTITY","QUANTITY",
           "QUANTITY","QUANTITY","QUANTITY","QUANTITY","QUANTITY",
           "QUANTITY","QUANTITY","QUANTITY","PRICE","PRICE","PRICE",
           "PRICE","PRICE","PRICE","PRICE","PRICE","PRICE","PRICE",
           "PRICE","PRICE","PRICE","PRICE","PRICE","PRICE","PRICE",
           "PRICE","PRICE","PRICE","PRICE","PRICE","BUY_POINT",
           "BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT",
           "BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT",
           "BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT",
           "BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT",
           "BUY_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
           "SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
           "SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
           "SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
           "SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
           "SELL_POINT"),
  POINT = c("QUANTITY","QUANTITY","QUANTITY",
            "QUANTITY","QUANTITY","QUANTITY","QUANTITY","QUANTITY","QUANTITY",
            "QUANTITY","QUANTITY","QUANTITY","QUANTITY","QUANTITY",
            "QUANTITY","QUANTITY","QUANTITY","QUANTITY","QUANTITY",
            "QUANTITY","QUANTITY","QUANTITY","PRICE","PRICE","PRICE",
            "PRICE","PRICE","PRICE","PRICE","PRICE","PRICE","PRICE",
            "PRICE","PRICE","PRICE","PRICE","PRICE","PRICE","PRICE",
            "PRICE","PRICE","PRICE","PRICE","PRICE","BUY_POINT",
            "BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT",
            "BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT",
            "BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT",
            "BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT","BUY_POINT",
            "BUY_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
            "SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
            "SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
            "SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
            "SELL_POINT","SELL_POINT","SELL_POINT","SELL_POINT",
            "SELL_POINT"),
  VALUE = c(12500,12500,12500,12500,12500,12500,
            12500,12500,12500,12500,10100,10100,10100,10100,10100,
            10100,10100,10100,10100,10100,10100,0,49.63,50.36,50.06,
            52.8,52.5,50.9,50.99,50.91,50.71,50.06,49.71,50.8,
            51.09,51.27,52.8,52.8,51.07,50.35,50.25,49.24,49.03,
            52.91,49.63,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
            NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
            NA,NA,49.71,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,52.91)
)


sec_dt <- sec_dt[, DATE := lubridate::ymd(DATE)]
x1 <- max(sec_dt[LINE == 'QUANTITY', VALUE]) * 2
y1 <- max(sec_dt[LINE == 'PRICE', VALUE])
x2 <- min(sec_dt[LINE == 'QUANTITY', VALUE])
y2 <- min(sec_dt[LINE == 'PRICE', VALUE])

k <- (y2 - y1) / (x2 - x1)
b <- y1 - k*x1
stopifnot(!is.na(k),  !is.na(b))

p <- ggplot(sec_dt, aes(x = DATE)) +
  geom_point(
    aes(y = VALUE, size = POINT, fill = POINT, shape = POINT, stroke = NA),
    data = sec_dt[POINT %in% c('BUY_POINT', 'SELL_POINT')], na.rm = TRUE
  ) +
  geom_line(aes(y = VALUE, linetype = LINE, colour = LINE), data = sec_dt[LINE %in% c('PRICE')]) +
  geom_line(aes(y = VALUE * k + b, linetype = LINE, colour = LINE), data = sec_dt[LINE %in% c('QUANTITY')]) +
  scale_linetype_manual('', values = c('PRICE' = 'solid', 'QUANTITY' = 'dashed')) +
  scale_size_manual('', values = c('BUY_POINT' = 3, 'SELL_POINT' = 3)) +
  scale_fill_manual('', values = c('BUY_POINT' = '#FF0000', 'SELL_POINT' = '#99FF66')) +
  scale_colour_manual('', values = c('PRICE' = '#000000', 'QUANTITY' = '#CC0000')) +
  scale_shape_manual('', values = c('BUY_POINT' = 21, 'SELL_POINT' = 21)) +
  scale_y_continuous(
    name = 'price', limits = c(min(sec_dt[LINE == 'PRICE', VALUE]), max(sec_dt[LINE == 'PRICE', VALUE])),
    sec.axis = sec_axis(~ (. - b) / k , name = "quantity")
  ) +
  guides(
    fill = guide_legend(override.aes = list(stroke = NA))
  ) +
  labs(x = NULL, y = NULL) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.title.x = element_text(size = 12),
    axis.title.y = element_text(size = 12),
    legend.text = element_text(size = 8),
    legend.position = "top"
  )
p

结果如下图

image.png

echarts4r

相关网址

echarts4r官网: https://echarts4r.john-coene.com/index.html
统计之都的中文讲解,比较全,可以满足大部分的画图需求:echarts4r: 从入门到应用
echart API的中文网站,英文API有些看不懂的可以参考这里的:https://echarts.apache.org/zh/

用法

在使用R的echarts4r包的过程中,发现里面有些函数挺反人类的,比如加双x轴,搞了好久都没搞出来,严重怀疑是这个包的bug。但是,全包最NB的函数就是e_list(),所有JSON option能实现的,用这个函数全可以实现!所以,如果在使用的过程中,对于某些特殊的画图需求,发现包里自带的函数难以实现,直接上e_list。比如在echarts4r的官网里的这个例子:

N <- 20 # data points

opts <- list(
  xAxis = list(
    type = "category",
    data = LETTERS[1:N]
  ),
  yAxis = list(
    type = "value"
  ),
  series = list(
    list(
      type = "line",
      data = round(runif(N, 5, 20))
    )
  )
)

p <- e_charts() |> 
  e_list(opts)

另外一个好用的函数为e_inspect,这个函数就是返回图的JSON options的源码,如果画的图有错误,可以通过源码来检查哪里写的有问题,接上面的例子:

library(magrittr)

json <- p %>%
  e_inspect(
    json = TRUE,
    pretty = TRUE
  )
json

综上所述,搭配使用e_inspecte_list函数,可以解决几乎所有的画图问题。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,905评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,140评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,791评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,483评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,476评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,516评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,905评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,560评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,778评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,557评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,635评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,338评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,925评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,898评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,142评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,818评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,347评论 2 342

推荐阅读更多精彩内容