Add grid lines to minor breaks only (ggplot)

前端 未结 1 1190
挽巷
挽巷 2021-01-18 18:22

I was trying to make a chart as the below in ggplot. I wanted the y labels to show the entire set of values from 1 to 50 but I only wanted to generate horizonta

相关标签:
1条回答
  • 2021-01-18 18:27

    From reading this https://github.com/tidyverse/ggplot2/issues/403, it would appear that there are some issues surround minor_breaks. However, using geom_hline() should get you what you want.

    library(nsRFA)
    library(ggplot2)
    library(dplyr)
    
    
    data(hydroSIMN)
    
    minors<-seq(10,50,by=10)
    
    annualflows %>% ggplot(aes(x = anno, y = cod)) + 
      geom_point(
        shape = 45,
        size = 5,
        col = "blue"
      ) + 
      scale_y_reverse(
        breaks = 1:50,
        labels = 1:50,
        minor_breaks = seq(10, 50, by = 10)
      ) +
      scale_x_continuous(breaks = seq(1920, 1980, by = 10)) +
      labs(
        x = "Year",
        y = "Code"
      ) + 
      theme(
        panel.background = element_blank(),
        panel.border = element_rect(fill = NA),
        text = element_text(size = 10),
        panel.grid.major.x = element_line(color = "grey80"),
        #panel.grid.major.y = element_blank(),
        #panel.grid.minor.y = element_line(color = "grey80") # This doesn't work
      )+
      geom_hline(mapping=NULL, yintercept=minors,colour='grey80')
    

    0 讨论(0)
提交回复
热议问题