无法让 geom_text 处理构面和我的其余情节

Cannot get geom_text to work with facets and the rest of my plot

我已经使用 ggplot2 构建了一个分面图,但是当我尝试向每个分面添加文本时,我收到一条错误消息,我似乎可以绕过它('Error in eval(expr, envir, enclos) : object 'width' not found'。我希望能有另一双眼睛看到这一点。我要添加的行是向下的 3/4并注释掉 ( # p1 = p1 + geom_text(ae ..... ).

library(ggplot2)
library(dplyr)
library(tidyr)

rownum <- 1:6
orgs <- c('A','B','C','D','E','F')
level <- c(0,1,1,1,1,1)

qtd <- c(1216.146, 743.482, 276.105, 135.089, 52.703, 8.767)
qtd_ref <- c(0.53,0.529,0.56,0.556,0.499,0.421)
qtd_vs_qc <- c(0.574,0.646,0.656,0.508,0.215,0.249)
qtd_vs_qf <- c(0.566,0.627,0.656,0.507,0.217,0.249)
qtd_vs_qp <- c(0.536,0.622,0.52,0.458,0.25,0.233)
yl1_ref <- c(0.526,0.502,0.563,0.472,0.629,0.418)
yl2_ref <- c(0.534,0.544,0.62,0.422,0.344,0.478)
yl3_ref <- c(0.53,0.54,0.498,0.772,0.525,0.368)
ql1_ref <- c(0.548,0.557,0.56,0.595,0.319,0.594)
qc_vs_ref <- c(0.044,0.118,0.096,-0.048,-0.284,-0.172)

colors <- c("#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00")
class(colors)


## plot:  p1
## yAxis: Target QTD Attainment (%)
##
##

df <- data.frame( rownum, orgs, level, qtd, qtd_ref, qtd_vs_qc, qtd_vs_qf, qtd_vs_qp)
df1 <-  data.frame(df[,c('rownum','orgs','level')], width = 0.1 + 0.9 * ((df$qtd * 2) / sum(df$qtd)),df[,c('qtd_ref','qtd_vs_qc','qtd_vs_qf','qtd_vs_qp')])
n1 <- ncol(df1)
tg1 <- tbl_df(df1) %>% gather('target','percent',6:n1)
tg1$orgs <- factor(tg1$orgs, levels = orgs)
tg1

hldata<- data.frame(x = 0, y = qtd_ref * 100, lab = qtd_ref * 100, orgs = orgs)

p1 = ggplot(data = tg1, aes(x=target, y = percent * 100, width=width, fill=factor(target)))
p1 = p1 + geom_bar(stat='identity', position='identity')  
p1 = p1 + facet_wrap(~orgs)
p1 = p1 + geom_hline(aes(yintercept = qtd_ref * 100), hldata) 
# p1 = p1 + geom_text(aes(x=x,y=y,label=lab, vjust = -0.5, hjust = -0.5), data=hldata)
p1 = p1 + scale_fill_manual(values=c(colors[1], colors[3], colors[5]),  labels=c("Commit","Forecast","Plan"))
p1 = p1 + xlab("organization") 
p1 = p1 + ylab("Target QTD Attainment (%)")
p1 = p1 + labs(fill="Target")
p1 = p1 + theme(axis.text.x=element_text(angle=90,hjust=1,vjust=.5,colour='gray50'), 
                strip.text.y = element_text(size = 18) ,
                strip.text = element_text(size = 12)
)
p1 = p1 + ggtitle("Percent of Target Attained QTD")
p1

不知道为什么(也许是一个错误?)如果我将 hldata 的 x 列命名为 target,我只能让它工作,匹配 tg1。但是,正如我在评论中所说,如果您首先将 tg1$target 映射到 x 轴,那么您映射 x 轴 (hldata$x) 的任何其他数据都需要相同的数据类型:在这种情况下与 tg1$target.

水平相同的因子
# this will have the same levels as the tg1$target
# and there's no need for duplicate qtd_ref columns
hldata <- data.frame(target = tg1$target[1],
    y = qtd_ref, orgs = orgs) 

p1 = 
    ggplot(data = tg1, aes(x=target, y = percent, fill= target)) +
    geom_bar(aes(width = width), stat='identity', position='identity')  +
    facet_wrap(~ orgs) +
    geom_hline(aes(yintercept = qtd_ref), data = hldata) +
    # take vjust and hjust outside of aes()
    # map y and label to y since they were pointing to identical columns
    geom_text(aes(x = target, y = y,
                  # I'm guessing you want this as a percent too
                  label = scales::percent(y)), 
              vjust = -0.5, hjust = 1, data=hldata) +
    # put percent labels on the y axis
    scale_y_continuous(labels = scales::percent) +
    # pass vector to colors[]
    scale_fill_manual(values=c(colors[c(1, 3, 5)],
                      labels=c("Commit", "Forecast", "Plan")) +
    # a single labs call is easier than xlab(), ylab() ggtitle()...
    # and with the percent in the tick labels maybe don't need (%)
    labs(x = "organization",
         y = "Target QTD Attainment",
         fill = "Target",
         title =  "Percent of Target Attained QTD") +
    theme(axis.text.x = element_text(angle = 90, hjust = 1,
                                     vjust = .5, colour = 'gray50'), 
          strip.text.y = element_text(size = 18) ,
          strip.text = element_text(size = 12))

p1

我稍微清理了代码,删除了乘以 100 的代码,并用对 scales::percent 的调用替换了它。而且不需要每行重新分配情节。