gpt4 book ai didi

R ggplot2 : how to make x-axis lines not overlapping y-axis?

转载 作者:行者123 更新时间:2023-12-01 16:39:03 24 4
gpt4 key购买 nike

我经常用 ggplot 制作图表,而且我喜欢浅色设计(白色背景等)除了 x 轴线与 y 轴线重叠之外,一切正常,请参见屏幕截图中的红色标记:

overlapping axis, marked red

我想要的如下所示,目前,我必须在 Illustrator 中编辑每个绘图...我希望 x Axis 不与 y 轴线重叠(不在右侧,也不在右侧)左侧)。在我看来,这看起来更干净。

what i want

有谁知道我怎样才能实现这个目标?到目前为止我还没有找到任何东西......所以非常感谢您的帮助。

编辑(示例):

数据:

                                    label_de proz
1: Dialekt / Sprache 37.6
2: Landschaft 52.1
3: Traditionen und Bräuche (Fasnacht, etc.) 20.4
4: Siedlungsraum (Gebäude usw.) 21.6
5: Sportclubs (Fussball, Eishockey, etc.) 13.4
6: Freunde und Bekannte 61.7
7: Familie 57.0
8: Bewohnerinnen / Bewohner 14.9
9: Kulinarisches Angebot (Essen, Trinken) 12.2
10: Freizeitangebot 18.6
11: Politisches Profil 5.8
12: Anderes 13.1

和 ggplot 代码(无排序):

p <- ggplot(data=ggdata, aes(x=label_de, y=proz)  ) +
geom_bar(stat="identity", position="dodge") +
ylim(0,100) +
coord_flip() +
theme_bw() + theme( strip.background = element_blank(),
panel.grid.major = element_line(colour = "grey80"),
panel.border = element_blank(),
axis.ticks = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(colour = "grey80"),
axis.text.y = element_text(hjust = 1),
legend.position="bottom")

最佳答案

您可以将 expand= c(0,0) 添加到刻度,然后移动标签,这样它们就不会被切断。随机数据示例

set.seed(123)
ggplot(data=data.frame(label_de=letters[1:10], proz = runif(10,0, 85)),
aes(x=label_de, y=proz)) +
geom_bar(stat="identity", position="dodge") +
coord_flip() +

# notice this part
scale_y_continuous(limits = c(0,100), expand = c(0,0)) +
theme_bw() +
theme( strip.background = element_blank(),
panel.grid.major = element_line(colour = "grey80"),
panel.border = element_blank(),
axis.ticks = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(colour = "grey80"),
axis.text.y = element_text(hjust = 1),

# notice this part
axis.text.x = element_text(hjust = 1),
legend.position="bottom")

enter image description here

关于R ggplot2 : how to make x-axis lines not overlapping y-axis?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36552448/

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