gpt4 book ai didi

r - 仅将网格线添加到较小的中断处(ggplot)

转载 作者:行者123 更新时间:2023-12-02 11:22:13 24 4
gpt4 key购买 nike

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

如何将网格线数设置为小于分隔线数?谢谢!

enter image description here

这是绘图的代码:

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

enter image description here

关于r - 仅将网格线添加到较小的中断处(ggplot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43928779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com