使用公式在Rmarkdown中的图形上添加标题

时间:2019-06-08 16:04:52

标签: graph r-markdown caption

我想为我的Rmarkdown文档中的每个图形加上标题。该标题应基于一个公式,该公式将显示源数据中的观测值总数。我正在使用nrow(data)计算观察总数。标题应为“观察总数= n” 此“ n”应基于公式(nrow(data)),然后将针对每个块自动更改。

theme_set(theme_classic())
library(stringr)
library(scales)


g9_x79 <- ggplot(Q9_x79, aes(x=reorder(Q,Freq), Freq))

g9_x79+geom_bar(stat = "identity", width = 0.9,fill="#00A8C8")+ ggtitle("PARTICIPANTS PROFILE - INDUSTRY")+
      labs( x= NULL, y= NULL,
           subtitle=NULL) +
  ylab(NULL)+
  geom_text(aes(label = paste0(roundUp(Freq*100), "%")),face= "bold",color="#FDFEFE",size=3,position = position_stack(vjust = 0.5))+coord_flip()+
    theme(axis.text.x = element_blank())+ 
  theme(axis.text.y = element_text(face="bold",color = "#626366",size = 10, vjust=0.6), plot.title = element_text(family = "Arial", color="#00A8C8", face="bold", size=15, hjust=0),    
  element_line(colour="#939597") ) + scale_y_continuous(labels = percent)

dput(Q9_x79)
structure(list(Q = c("Banking/Financial Services", "Chemicals", 
"Consumer Goods", "Energy", "High Tech", "Insurance/Reinsurance", 
"Life Sciences", "Logistics", "Mining & Metals", "Other Manufacturing", 
"Other Non-Manufacturing", "Retail & Wholesale", "Services (Non-Financial)", 
"Transportation Equipment"), Freq = c(0.08, 0.08, 0.28, 0.02, 
0.12, 0.02, 0.05, 0.02, 0, 0.02, 0.08, 0.1, 0.12, 0)), row.names = c(NA, 
-14L), class = "data.frame")

我尝试了fig.caption,但是无法在其中添加公式。

0 个答案:

没有答案