我有下面的数据框:
df <- data.frame(result=floor(rnorm(1000, 100, 20)))
Run Code Online (Sandbox Code Playgroud)
创建此直方图:
require(scales)
df <- data.frame(result=floor(rnorm(1000, 100, 20)))
ggplot(df)+
geom_histogram(aes(result, fill = (result<=80 | result > 95)),
binwidth = 5, center = 2.5, color = "black") +
scale_fill_manual(values=c("darkblue", "lightblue"), guide = F) +
labs(y = "Frequency", x = "Result") + scale_y_continuous(labels = comma)+
theme_classic(base_size = 16)+annotate("label",x=130,y=90, label = "text" )
Run Code Online (Sandbox Code Playgroud)
我添加了一个标签,但我想知道是否还有其他形状(可能是彩色的)可以放置文本。理想情况下是一个箭头,但也有带有文本和下方箭头的标签。

添加窄箭头可以这样完成:
... +
annotate("segment", x = 87.5, y = 110, xend = 87.5, yend = 98,
arrow = arrow(type = "closed", length = unit(0.02, "npc")))
Run Code Online (Sandbox Code Playgroud)
或者带有文本的更厚版本:
...+
annotate("segment", x = 87.5, y = 115, xend = 87.5, yend = 100,
size = 8, linejoin = "mitre",
arrow = arrow(type = "closed", length = unit(0.01, "npc"))) +
annotate("text", x=87.5,y=115, label = "text", color = "white",
angle = 90, hjust = 1.5, size = 5, fontface = "bold")
Run Code Online (Sandbox Code Playgroud)