仅将网格线添加到次要中断(ggplot)
Add grid lines to minor breaks only (ggplot)
我试图在 ggplot
中制作如下图表。我希望 y 标签显示从 1 到 50 的整组值,但我只想每 10 个数字生成水平网格线。我认为添加 minor_breaks
然后控制主题会起作用。但是,将主要网格线设置为 element_blank
似乎也会覆盖次要网格线。我在这里发现了一些问题,人们问的是添加更多的网格线而不是标签,但我想要相反的结果。
如何设置网格线的数量小于中断的数量?谢谢!
这是情节的代码:
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
)
阅读此 https://github.com/tidyverse/ggplot2/issues/403,minor_breaks
似乎存在一些问题。但是,使用 geom_hline()
应该可以满足您的需求。
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
中制作如下图表。我希望 y 标签显示从 1 到 50 的整组值,但我只想每 10 个数字生成水平网格线。我认为添加 minor_breaks
然后控制主题会起作用。但是,将主要网格线设置为 element_blank
似乎也会覆盖次要网格线。我在这里发现了一些问题,人们问的是添加更多的网格线而不是标签,但我想要相反的结果。
如何设置网格线的数量小于中断的数量?谢谢!
这是情节的代码:
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
)
阅读此 https://github.com/tidyverse/ggplot2/issues/403,minor_breaks
似乎存在一些问题。但是,使用 geom_hline()
应该可以满足您的需求。
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')