将标签放置在 geom_col() 之外的条形图
Position labels in geom_col() outside of bars
我的条形图代码:
library(ggplot2)
data <- data.frame(vars = c("pos1", "neg1", "pos2", "neg2", "pos3", "pos4"),
values = c(164182, -72705, 1023777, -75002, 756206, 564523),
sign = c("p", "n", "p", "n", "p", "p"))
ggplot(data, aes(x = values, y = vars, fill = sign)) + geom_col() +
geom_text(aes(label = format(round(values), big.mark = ",")))
不错,但我希望标签刚好位于条形 外部 并且 完全可见 。在上面的示例中,我让它们“一半进一半”,pos2 的标签不完全可见。
所以我在最后一行添加了 hjust = "outward":
library(ggplot2)
data <- data.frame(vars = c("pos1", "neg1", "pos2", "neg2", "pos3", "pos4"),
values = c(164182, -72705, 1023777, -75002, 756206, 564523),
sign = c("p", "n", "p", "n", "p", "p"))
ggplot(data, aes(x = values, y = vars, fill = sign)) + geom_col() +
geom_text(aes(label = format(round(values), big.mark = ",")), hjust = "outward")
现在除了 pos1 之外的所有标签(为什么会这样?)都完全符合我的要求(在外面),但是其中三个标签超出了范围,这不好。
将“向外”更改为“向内”解决了“越界”问题,但标签现在位于条形内部(除了 pos1,它有什么问题?)
那么我如何结合第二和第三种解决方案,使所有标签都在条形之外并且可见?
条件 hjust
可能会有所帮助。请注意 hjust = "inward/outward"
表示“相对于图的中心” - see Hadley's comment in this discussion
规模扩张 = 这是体力劳动。对于编程方法,您需要访问 geom_text 维度,这似乎非常困难 - see this unanswered question
library(ggplot2)
data <- data.frame(vars = c("pos1", "neg1", "pos2", "neg2", "pos3", "pos4"),
values = c(164182, -72705, 1023777, -75002, 756206, 564523),
sign = c("p", "n", "p", "n", "p", "p"))
ggplot(data, aes(x = values, y = vars, fill = sign)) + geom_col() +
geom_text(aes(label = values),
hjust = ifelse(data$values>0,0,1))+
scale_x_continuous(expand = c(.3,0))
由 reprex package (v1.0.0)
于 2021 年 3 月 7 日创建
我的条形图代码:
library(ggplot2)
data <- data.frame(vars = c("pos1", "neg1", "pos2", "neg2", "pos3", "pos4"),
values = c(164182, -72705, 1023777, -75002, 756206, 564523),
sign = c("p", "n", "p", "n", "p", "p"))
ggplot(data, aes(x = values, y = vars, fill = sign)) + geom_col() +
geom_text(aes(label = format(round(values), big.mark = ",")))
不错,但我希望标签刚好位于条形 外部 并且 完全可见 。在上面的示例中,我让它们“一半进一半”,pos2 的标签不完全可见。
所以我在最后一行添加了 hjust = "outward":
library(ggplot2)
data <- data.frame(vars = c("pos1", "neg1", "pos2", "neg2", "pos3", "pos4"),
values = c(164182, -72705, 1023777, -75002, 756206, 564523),
sign = c("p", "n", "p", "n", "p", "p"))
ggplot(data, aes(x = values, y = vars, fill = sign)) + geom_col() +
geom_text(aes(label = format(round(values), big.mark = ",")), hjust = "outward")
现在除了 pos1 之外的所有标签(为什么会这样?)都完全符合我的要求(在外面),但是其中三个标签超出了范围,这不好。
将“向外”更改为“向内”解决了“越界”问题,但标签现在位于条形内部(除了 pos1,它有什么问题?)
那么我如何结合第二和第三种解决方案,使所有标签都在条形之外并且可见?
条件 hjust
可能会有所帮助。请注意 hjust = "inward/outward"
表示“相对于图的中心” - see Hadley's comment in this discussion
规模扩张 = 这是体力劳动。对于编程方法,您需要访问 geom_text 维度,这似乎非常困难 - see this unanswered question
library(ggplot2)
data <- data.frame(vars = c("pos1", "neg1", "pos2", "neg2", "pos3", "pos4"),
values = c(164182, -72705, 1023777, -75002, 756206, 564523),
sign = c("p", "n", "p", "n", "p", "p"))
ggplot(data, aes(x = values, y = vars, fill = sign)) + geom_col() +
geom_text(aes(label = values),
hjust = ifelse(data$values>0,0,1))+
scale_x_continuous(expand = c(.3,0))
由 reprex package (v1.0.0)
于 2021 年 3 月 7 日创建