Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add grid lines to minor breaks only (ggplot)

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 horizontal gridlines every 10 numbers. I thought adding minor_breaks and then controlling the theme would work. However, setting the major grid lines to element_blankseems to be overriding the minor gridlines as well. I found some questions here where people have asked about adding more gridlines than labels, but I want the reverse.

How can I set the number of gridlines to be smaller than the number of breaks? Thanks!

enter image description here

Here is the code for the plot:

 library(nsRFA)
library(ggplot2)
library(dplyr)

data(hydroSIMN)
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

    )
like image 621
Sraffa Avatar asked May 12 '17 03:05

Sraffa


People also ask

How do you add primary minor vertical grid lines?

On the Layout tab, in the Axes group, click Gridlines. Do one or more of the following: Click Primary Horizontal Gridlines, Primary Vertical Gridlines, or Depth Gridlines (on a 3-D chart), and then click None.

How do you add gridlines to a graph in R?

In R you can add gridlines using the abline() command.

What is the difference between major and minor gridlines?

On value axes, major grid lines are drawn for every major axis division. Minor grid lines separate the units delineated by major grid lines. Minor grid lines, which can only appear on value axes, appear for every minor axis division. By default, major grid lines appear for value axes.

Which function can I use to modify the elements of a theme in Ggplot?

Modify a single plot's theme using theme() ; see theme_update() if you want modify the active theme, to affect all subsequent plots. Use the themes available in complete themes if you would like to use a complete theme such as theme_bw() , theme_minimal() , and more.


1 Answers

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

enter image description here

like image 174
J.Con Avatar answered Sep 26 '22 00:09

J.Con