R ggplot2:如何使 x 轴线不与 y 轴重叠?
R ggplot2: how to make x-axis lines not overlapping y-axis?
我经常从 ggplot 制作图表,我喜欢有一个浅色设计(白色背景等)
这一切都很好,除了 x 轴线与 y 轴线重叠,请参见屏幕截图中的红色标记:
我想要的如下所示,目前,我必须在 Illustrator 中编辑每个图...我希望 x 轴不与 y 轴线重叠(不在右侧和不在左侧)。在我看来,这看起来干净多了。
有谁知道我怎样才能做到这一点?到目前为止我还没有找到任何东西...所以非常感谢任何帮助。
编辑(示例):
数据:
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")
我经常从 ggplot 制作图表,我喜欢有一个浅色设计(白色背景等) 这一切都很好,除了 x 轴线与 y 轴线重叠,请参见屏幕截图中的红色标记:
我想要的如下所示,目前,我必须在 Illustrator 中编辑每个图...我希望 x 轴不与 y 轴线重叠(不在右侧和不在左侧)。在我看来,这看起来干净多了。
有谁知道我怎样才能做到这一点?到目前为止我还没有找到任何东西...所以非常感谢任何帮助。
编辑(示例):
数据:
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")