Geom_Text 没有将 vjust 标签定位在 Geom_Bar 上
Geom_Text not positioning vjust label over Geom_Bar
我遵循的是 的代码,但它对我来说效果不一样。下面是我的代码和屏幕截图。你能帮我把标签居中吗?
p <- ggplot(data=mrc, aes(x = Year, y = Total, fill = Year)) +
geom_bar(stat="identity", position = "dodge") +
geom_text(
aes(x = Year, y = Total, label = Total),
position = position_dodge(width = 1),
vjust = -0.5, size = 3
) +
theme_bw() +
scale_fill_manual(values = c("#115740","#B9975B","#D0D3D4","#F0B323")) +
theme(axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position = "none",
plot.title = element_text(hjust = 0.5, face = "bold", colour = "#B9975B")) +
ggtitle("Petitions")
ggplotly(p)
The issue这里是ggplotly
。一种解决方案是使用 style(textposition = "top")
.
正在重新创建您的数据:
mrc <- data.frame(Year = c("2015-16", "2016-17", "2017-18", "2018-19"),
Total = c(225, 461, 471, 230),
stringsAsFactors = FALSE)
运行 你的第一段代码生成p
:
p
一切顺利。但是结果使用 ggplotly
:
ggplotly(p)
添加style()
:
ggplotly(p) %>% style(textposition = "top")
我遵循的是
p <- ggplot(data=mrc, aes(x = Year, y = Total, fill = Year)) +
geom_bar(stat="identity", position = "dodge") +
geom_text(
aes(x = Year, y = Total, label = Total),
position = position_dodge(width = 1),
vjust = -0.5, size = 3
) +
theme_bw() +
scale_fill_manual(values = c("#115740","#B9975B","#D0D3D4","#F0B323")) +
theme(axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position = "none",
plot.title = element_text(hjust = 0.5, face = "bold", colour = "#B9975B")) +
ggtitle("Petitions")
ggplotly(p)
The issue这里是ggplotly
。一种解决方案是使用 style(textposition = "top")
.
正在重新创建您的数据:
mrc <- data.frame(Year = c("2015-16", "2016-17", "2017-18", "2018-19"),
Total = c(225, 461, 471, 230),
stringsAsFactors = FALSE)
运行 你的第一段代码生成p
:
p
一切顺利。但是结果使用 ggplotly
:
ggplotly(p)
添加style()
:
ggplotly(p) %>% style(textposition = "top")