我想调整条形图上的文字。
我尝试调整hjust / vjust以显示我喜欢它但似乎它无法正常工作。
ggplot(data) +
geom_bar(aes(name, count,
fill = week), stat='identity', position = 'dodge') +
geom_text(aes(name,count,
label=count),hjust=0.5, vjust=3, size=2,
position = position_dodge(width = 1)) +
coord_flip()
所以我希望数字位于每个条形图上,中间位于右边缘,因此它的可读性不会像最后一部分那样重叠。
答案 0 :(得分:28)
让hjust
/ vjust
智能行为的更简单方法是将group
美学添加到geom_text
然后hjust
& position
自动调整group
。
ggplot(data) +
geom_bar(
aes(x = name, y = count, fill = week, group = week),
stat='identity', position = 'dodge'
) +
geom_text(
aes(x = name, y = count, label = count, group = week),
position = position_dodge(width = 1),
vjust = -0.5, size = 2
) +
theme_bw()
这给出了:
ggplot(data) +
geom_bar(
aes(x = name, y = count, fill = week, group = week),
stat='identity', position = 'dodge'
) +
geom_text(
aes(x = name, y = count, label = count, group = week),
hjust = -0.5, size = 2,
position = position_dodge(width = 1),
inherit.aes = TRUE
) +
coord_flip() +
theme_bw()
这给出了:
这不一定是执行此操作的最常用方法,但您可以拥有fill
依赖hjust
(或vjust
,具体取决于方向)变量。我不完全清楚如何选择调整参数的值,目前它基于看起来的权利。也许其他人可以建议一种更通用的方法来选择这个参数值。
library(dplyr)
library(ggplot2)
# generate some data
data = data_frame(
week = as.factor(rep(c(1, 2), times = 5)),
name = as.factor(rep(LETTERS[1:5], times = 2)),
count = rpois(n = 10, lambda = 20),
hjust = if_else(week == 1, 5, -5),
vjust = if_else(week == 1, 3.5, -3.5)
)
# Horizontal
ggplot(data) +
geom_bar(
aes(x = name, y = count, fill = week, group = week),
stat='identity', position = 'dodge'
) +
geom_text(
aes(x = name, y = count, label = count, vjust = vjust),
hjust = -0.5, size = 2,
inherit.aes = TRUE
) +
coord_flip() +
theme_bw()
以下是这样的:
ggplot(data) +
geom_bar(
aes(x = name, y = count, fill = week, group = week),
stat='identity', position = 'dodge'
) +
geom_text(
aes(x = name, y = count, label = count, vjust = vjust),
hjust = -0.5, size = 2,
inherit.aes = TRUE
) +
coord_flip() +
theme_bw()
以下是这样的:
答案 1 :(得分:6)
position_dodge()
语句采用宽度参数。要确保文本位于条形的末端(即条形的躲避宽度和文本相同),请为position_dodge()
中的geom_bar
语句指定相同的宽度参数并在geom_text
内。
还有geom_bar
的宽度参数,即条形的宽度。如果您希望条形在每个name
内相互对接,请使条形宽度与闪避宽度相同;如果你想在条之间留一个小间隙,那么条形宽度应比躲避宽度小一点。
如果你使用全球美学,你将不需要group
美学(但是,仅使用当地美学,你需要geom_text
的群体审美。
hjust = -0.5
会将文字标签定位在条形的末尾之外; hjust = 1.5
将它们放在栏杆的末端。
library(ggplot2)
# Generate some data - using @tchakravarty's data - Thanks.
df = data.frame(
week = as.factor(rep(c(1, 2), times = 5)),
name = as.factor(rep(LETTERS[1:5], times = 2)),
count = rpois(n = 10, lambda = 20))
position = position_dodge(width = .75)
width = .75
ggplot(df, aes(x = name, y = count, label = count, fill = week)) +
geom_bar(width = width, stat='identity', position = position) +
geom_text(hjust = -0.5, size = 2, position = position) +
coord_flip() +
theme_bw()
# To separate the bars slightly:
position = position_dodge(width = .75)
width = .65
ggplot(df, aes(x = name, y = count, label = count, fill = week)) +
geom_bar(width = width, stat='identity', position = position) +
geom_text(hjust = -0.5, size = 2, position = position) +
coord_flip() +
theme_bw()