仅将网格线添加到次要中断(ggplot) [英] Add grid lines to minor breaks only (ggplot)

查看:336
本文介绍了仅将网格线添加到次要中断(ggplot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 ggplot 中制作一张图表。我希望y标签显示1到50的整组值,但我只想每10个数字就产生一个水平网格线。我认为加入 minor_breaks ,然后控制主题将起作用。但是,将主网格线设置为 element_blank 似乎也会覆盖次要网格线。我在这里发现了一些问题,人们询问如何添加比标签更多的网格线,但我想要相反。



如何设置网格线的数量小于休息次数?谢谢!





这是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(break = seq(1920,1980,by = 10))+
labs(
x =Year ,
y =Code
)+
主题(
panel.background = element_blank(),
panel.border = element_rect(fill = NA),
text = element_text(size = 10),
panel.grid.major.x = ele ment_line(color =grey80),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_line(color =grey80)#This not工作



解决方案

阅读这篇


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!

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

    )

解决方案

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

这篇关于仅将网格线添加到次要中断(ggplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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