在facetwrap ggplot2中标记hline

在facetwrap ggplot2中标记hline,r,ggplot2,label,geom-hline,R,Ggplot2,Label,Geom Hline,我正在尝试在我的ggplot中为hline添加标签,并在标签上标明每个方面的值。方面包装显示了几个评级,hline值来自基准测试。 我曾尝试在geom_hline中使用label=,但不起作用。如果您能为我提供帮助和/或建议,我将不胜感激 下面是一个示例代码来说明我的观点。先谢谢你 Rating = as.factor(c("A partner", "Approachable", "Arrogant", "Exciting")) A1 = as.integer(c(38,53,42,40)) A

我正在尝试在我的ggplot中为hline添加标签,并在标签上标明每个方面的值。方面包装显示了几个
评级,hline值来自
基准测试。
我曾尝试在geom_hline中使用
label=
,但不起作用。如果您能为我提供帮助和/或建议,我将不胜感激

下面是一个示例代码来说明我的观点。先谢谢你

Rating = as.factor(c("A partner", "Approachable", "Arrogant", "Exciting"))
A1 = as.integer(c(38,53,42,40))
A2 = as.integer(c(40,41,55,35))
A3 = as.integer(c(43,57,11,23))
A4 = as.integer(c(56,36,48,50))
Benchmark = as.integer(c(50,20,64,43))

df_full <- data.frame(Rating, A1, A2, A3, A4, Benchmark)
df <- df_full[,c(1:5)]

library(reshape2)
df_long <- melt(df, id = "Rating")

library(ggplot2)

p<-ggplot(data=df_long, aes(x=variable, y=value, fill = variable, label = value)) +
  #Order ratings
  facet_wrap(factor(Rating, levels = c(   "A partner"        
                                          ,"Approachable"     
                                          ,"Exciting"         
                                          ,"Arrogant"))~., scales = "free") +
  geom_bar(stat="identity") +  
  geom_text (aes()
             , hjust = -0.5
             , size = 5
             ,position = position_dodge(width = -1)) +

  geom_hline(data = df_full
             , aes(yintercept = as.integer(Benchmark))
             , color = "black"
             , linetype="dashed") +
  coord_flip() +
  ylim(0, 100)
p
Rating=as.factor(c(“合伙人”、“平易近人”、“傲慢”、“令人兴奋”))
A1=整数(c(38,53,42,40))
A2=整数(c(40,41,55,35))
A3=整数(c(43,57,11,23))
A4=整数(c(56,36,48,50))
基准=整数(c(50,20,64,43))

df_full可能带有第二个
geom_文本

# Using OPs provided data

library(ggplot2)
Rating <- factor(Rating, Rating)
ggplot(df_long, aes(variable, value, label = value)) +
    geom_bar(aes(fill = variable), stat = "identity") +  
    geom_text(hjust = -0.5, position = position_dodge(width = -1), size = 5) +
    geom_text(data = df_full, aes(1, Benchmark, label = Benchmark), 
              vjust = 2, hjust = -0.2) +
    geom_hline(data = df_full, aes(yintercept = Benchmark), 
               linetype = "dashed") +
    facet_wrap(~ Rating, scales = "free") +
    coord_flip(ylim = c(0, 100))
#使用OPs提供的数据
图书馆(GG2)

是的,这正是我想要的!非常感谢你。您是否知道如何将标签(特定值)添加到x轴,以使其不会笨拙地四处移动。(我确实调整了
vjust
,但它移出了容器。)@Christian代替'aes(1,Benchmark)`使用'aes(1,wantedXValue)
,例如:
aes(1,90)`。你可以接受我的答案,如果它有助于解决你的问题,我明白你的意思,但它固定在一个特定的值。我仍然希望它能够灵活地基于
基准测试
,但在某种程度上,它会将标签添加到x轴,而不一定是容器本身。这有意义吗?@Christian ohh,也许你脑子里有:
aes(-Inf,Benchmark,label=Benchmark),vjust=0,hjust=-0.2
-Inf
+
vjust=0
可以做到这一点