gpt4 book ai didi

r - 控制主要网格线的长度 - ggplot2

转载 作者:行者123 更新时间:2023-12-04 12:31:50 24 4
gpt4 key购买 nike

在ggplot2 中是否有控制主要网格线长度的解决方案?我在这里有一个图,我希望主要网格线出现在零线的左侧(只是负值)。所以对于“a”栏,只有一小部分网格线是可见的。 enter image description here

# Some Data
df <- data.frame(trt = c("a", "b", "c"),
outcome = c(-222.3, 121.9, 103.2))

# A plot with major grid lines
ggplot(df, aes(trt, outcome)) +
geom_col() +
coord_flip() +
theme_classic()+
theme(panel.grid.major.y = element_line(color = "blue", size = 1)) +
geom_hline(yintercept = 0, size = 1)

最佳答案

您无法控制网格线的范围;根据定义,网格涉及整个绘图区域,因此在 ggplot 中没有选项。您可以做的是使用绘制线条的几何图形,例如 geom_linerange:

ggplot(df, aes(trt, outcome)) +
geom_linerange(ymin = -Inf, ymax = 0, color = 'blue') +
geom_col() +
coord_flip() +
geom_hline(yintercept = 0) +
theme_classic() +
theme(panel.grid = element_blank())

enter image description here

关于r - 控制主要网格线的长度 - ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51219357/

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