我试图生成一个带有轴标签的水平图。
df = data.frame(quality = c("low", "medium", "high", "perfect"),
n = c(0.1, 11, 0.32, 87.45))
require(ggplot2)
require(dplyr)
size = 20
df %>%
ggplot() +
geom_bar(aes(x = quality, y = n),
stat = "identity", fill = "gray70",
position = "dodge") +
geom_text(aes(x = quality, y = n,
label = paste0(round(n, 2), "%")),
position = position_dodge(width = 0.9),
hjust = -0.2,
size = 10, color = "gray50") +
coord_flip() +
ggtitle("") +
xlab("gps_quality\n") +
#scale_x_continuous(limits = c(0, 101)) +
theme_classic() +
theme(axis.title = element_text(size = size, color = "gray70"),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_text(size = size,
color = ifelse(c(0,1,2,3) %in% c(2, 3), "tomato1", "gray40")))
不幸的是,有一根柱子比其他柱子要长得多,ggplot对其进行了部分裁剪。
有什么好的想法吗?
我已经尝试过scale_y_continuous(expand = c(0, 0)
,但这会在刻度文本和柱子之间留下很多空隙。
coord_cartesian
中添加clip="off"
。可以参考这个链接:https://twitter.com/ClausWilke/status/991542952802619392?s=19 - Sébastien Rochette