【问题标题】:Cannot get geom_text to work with facets and the rest of my plot无法让 geom_text 与构面和我的其他情节一起使用
【发布时间】:2015-04-16 23:11:52
【问题描述】:

我已经使用 ggplot2 构建了一个分面图,但是当我尝试向每个分面添加文本时,我收到一条错误消息,我似乎可以绕过它('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

【问题讨论】:

  • 我删除了rm(list = ls())。没有理由让其他人清理他们的工作空间。
  • 在您的geom_text 调用中,您使用数据hldata 并说labels = qtd_ref,但qtd_ref 不是hldata 的列。
  • 谢谢,回想起来我可能不会太受欢迎。
  • 我最后对labels = lab 进行了更正,但仍然没有通过错误消息。
  • 你真正的问题是你有一个因子 targettg1 数据框映射到你的 x 轴。在您的 hldata 数据框中,您有一个 x 列,该列全是数字 0。它必须是与tg1$target 具有相同水平的因素,因为您正在为 geom_text 指定一个全新的 x 轴,而 ggplot 不知道如何处理它。

标签: r ggplot2 facets


【解决方案1】:

不知道为什么(可能是一个错误?)如果我将 hldata 的 x 列命名为 target,与 tg1 中的 x 列的名称匹配,我只能让它工作。但是,正如我在 cmets 中所说,如果您首先将 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 的调用替换它。并且没有必要重新分配每一行的情节。

【讨论】:

  • 感谢您为我的问题提供解决方案以及其他建议和清理。
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2018-07-08
相关资源
最近更新 更多