如何在ggplot2 :: geom_step()中将线居中,类似于highcharter [英] How to center line in ggplot2::geom_step() similar to highcharter

查看:65
本文介绍了如何在ggplot2 :: geom_step()中将线居中,类似于highcharter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的绘图,我希望 ggplot2 :: geom_step()线对齐以我的点为中心,而不是向左对齐

highcharter :: hc_add_series(type ="line")中,有一个名为 step ="center" 的选项.请参阅我的(v0.2.1)于2019-05-02创建sup>

解决方案

您可以使用 position = position_nudge(x = -0.5)

我调整了您的lcl和ucl值,以使更改更易于查看.

  my_data<-data.frame(x = as.Date(c("2015-06-01","2015-07-01","2015-08-01","2015-09-01","2015-10-01","2015-11-01","2015-12-01","2016-01-01"))),y = c(35,41,40,45,56,54,60,57),cl = c(37,37,37,37,59,59,59,59),ucl = c(48,47,42,47,70,69,68,68),lcl = c(26,27,30,27,48,49,50,49))ggplot(my_data,aes(x = x,y = y,组= 1))+geom_line()+geom_point()+geom_step(aes(y = cl),position = position_nudge(x = -15))+geom_step(aes(y = ucl),position = position_nudge(x = -15))+geom_step(aes(y = lcl),position = position_nudge(x = -15)) 

For my plot, I would like the ggplot2::geom_step() line alignment to be centered around my points, instead of aligned to the left

In highcharter::hc_add_series(type = "line") there is an option called step = "center". See my jsfiddle for the look I am going for in ggplot2.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.1

my_data <- 
  data.frame(
           x = c("2015-06", "2015-07", "2015-08", "2015-09",
                 "2015-10", "2015-11", "2015-12", "2016"),
           y = c(35, 41, 40, 45, 56, 54, 60, 57),
          cl = c(37, 37, 37, 37, 59, 59, 59, 59),
         ucl = c(48, 47, 47, 47, 69, 69, 68, 68),
         lcl = c(26, 27, 27, 27, 48, 49, 49, 49)
  )

# Minimal ggplot
ggplot(my_data, aes(x = x, y = y, group = 1)) +
  geom_line() + 
  geom_point() + 
  geom_step(aes(y = cl), linetype = "dashed") +
  geom_step(aes(y = ucl), linetype = "dotted") +
  geom_step(aes(y = lcl), linetype = "dotted")

Created on 2019-05-02 by the reprex package (v0.2.1)

解决方案

You can use position = position_nudge(x = -0.5)

I adjusted your lcl and ucl values to make the change easier to see.

my_data <- 
  data.frame(
    x = as.Date(c("2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01",
          "2015-10-01", "2015-11-01", "2015-12-01", "2016-01-01")),
    y = c(35, 41, 40, 45, 56, 54, 60, 57),
    cl = c(37, 37, 37, 37, 59, 59, 59, 59),
    ucl = c(48, 47, 42, 47, 70, 69, 68, 68),
    lcl = c(26, 27, 30, 27, 48, 49, 50, 49)
  ) 

ggplot(my_data, aes(x = x, y = y, group = 1)) +
  geom_line() + 
  geom_point() + 
  geom_step(aes(y = cl), position = position_nudge(x = -15)) +
  geom_step(aes(y = ucl), position = position_nudge(x = -15)) +
  geom_step(aes(y = lcl), position = position_nudge(x = -15))

这篇关于如何在ggplot2 :: geom_step()中将线居中,类似于highcharter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆