Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Formatting minor y breaks in ggplot

I have the below chart:

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")

enter image description here

How can I change the format of the minor y-axis break lines to be dotted for example, or of a lower width than the major breaks?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")

>Solution :

To control the appearance of the minor breaks use the theme element panel.grid.minor. Here I have also made the major gridlines thicker for demonstration purposes and emphasis using 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))

enter image description here

If you want only the gridlines in the y axis to be affected, you can use the theme elements panel.grid.minor.y and panel.grid.major.y

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading