繁体   English   中英

在ggplot中格式化小y中断

[英]Formatting minor y breaks in ggplot

我有下面的图表:

library(ggplot2)
width = as.numeric(dff$time_terminal[1]-dff$time_terminal[2])*22.5
ggplot(data =dff, aes(x = time_terminal)) +
  geom_rect(aes(xmin = time_terminal - width, xmax = time_terminal + width, ymin = pmin(open.ha, close.ha), ymax = pmax(open.ha, close.ha), fill = dir)) +
  geom_linerange(aes(ymin=low, ymax=high)) +
  geom_point(aes(y = close), size = 1)+
  labs(title = paste(min(df$time_terminal), max(df$time_terminal)), y = "Close", x = "") + 
  theme_tq()+
  scale_y_continuous(breaks = seq(round(min(df$close),-1), max(df$close), by = 10))+
  theme(legend.position="none")

在此处输入图像描述

例如,如何将次要 y 轴断线的格式更改为虚线,或者宽度小于主要断线的格式?

dff <- structure(list(time_terminal = structure(c(1663973640, 1663973700, 
1663973760, 1663973820, 1663973880, 1663973940), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), open.ha = c(12310.0220066959, 12308.473503348, 
12306.386751674, 12303.655875837, 12306.1654379185, 12308.9202189592
), high = c(12311.3, 12310.3, 12303.8, 12316.8, 12321.3, 12312.8
), low = c(12301.3, 12300.3, 12294.8, 12300.3, 12302.8, 12300.8
), close.ha = c(12306.925, 12304.3, 12300.925, 12308.675, 12311.675, 
12305.425), dir = c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE), 
    close = c(12303.8, 12303.3, 12302.3, 12315.3, 12306.3, 12301.8
    )), row.names = 23033:23038, class = "data.frame")

要控制次要中断的外观,请使用主题元素panel.grid.minor 在这里,为了演示目的,我还使用panel.grid.major使主要网格线变粗了

ggplot(data = dff, aes(x = time_terminal)) +
  geom_rect(aes(xmin = time_terminal - width, xmax = time_terminal + width, 
                ymin = pmin(open.ha, close.ha), ymax = pmax(open.ha, close.ha), 
                fill = dir)) +
  geom_linerange(aes(ymin = low, ymax = high)) +
  geom_point(aes(y = close), size = 1) +
  labs(title = paste(min(dff$time_terminal), max(dff$time_terminal)), 
       y = "Close", x = "") + 
  tidyquant::theme_tq() +
  scale_y_continuous(breaks = seq(round(min(dff$close), -1), max(dff$close), 
                                  by = 10)) +
  theme(legend.position = "none",
        panel.grid.major = element_line(size = 1),
        panel.grid.minor = element_line(linetype = 'dashed', size = 0.5))

在此处输入图像描述

如果只想影响 y 轴上的网格线,可以使用主题元素panel.grid.minor.ypanel.grid.major.y

这些由panel.grid.minor主题元素控制。 因此,在 plot 中添加类似这样的内容:

theme(panel.grid.minor = element_line(color = "grey30", size = 0.1))

或者

theme(panel.grid.minor = element_line(linetype = "dashed", size = 0.1))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM