代码之家  ›  专栏  ›  技术社区  ›  Sandhya Ghildiyal

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

  •  0
  • Sandhya Ghildiyal  · 技术社区  · 5 年前

    我要在我的登记文件中的每个数字上加上标题。此标题应基于一个公式,该公式将显示源数据中的观测总数。我用nrow(数据)来计算观察总数。标题应为“观察总数=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 回复  |  直到 5 年前