注释ggplot条形图错误:未使用的参数

时间:2015-08-17 17:53:58

标签: r ggplot2 annotate

我试图在我通过函数创建的条形图上注释标准消息。以下是代码:

    hashbar <- function(x) {
    suppressWarnings(library(stringr))
    hash <- "#[A-Za-z0-9]{1,}"
    hashtg <- str_extract_all(x$text, hash)
    hashtg <- as.data.frame(unlist(hashtg))
    hashtg <- as.data.frame(sort(table(hashtg), decreasing = TRUE)[1:15])
    names(hashtg)[1] <- "Freq"  
    hashtg$hashtag <- rownames(hashtg)
    rownames(hashtg) <- NULL
    suppressWarnings(library(ggplot2))
    suppressWarnings(library(RColorBrewer))
    p <- ggplot(hashtg, aes(x=reorder(hashtag, Freq), y = Freq, fill = hashtag)) + geom_bar(stat="identity") +
            geom_bar(width = 0.4) + xlab("Hashtags Used") + ylab("Number of responses") + 
            geom_text(aes(label=Freq), hjust = 1, colour = "white" ) + 
            ggtitle("Analysis of Most Frequently Used Hashtags") + 
            theme(plot.title=element_text(size=rel(1.2), lineheight = 1, face = "bold")) +
            theme(axis.text = element_text(size = 12, color = "black")) +
            theme(axis.title = element_text(size=12, face = "bold")) + 
            theme(panel.background = element_rect(fill = "grey95")) +        
            theme(legend.position = "none") +
            coord_flip() 
    p + annotate("text", x = Inf, y = -Inf, label = "This is a test", hjust = 1.1, vjust = -0.5)
    p
}

数据:

        structure(list(text = c("_ShaneMacD: Great tour between the two sides. Shame the Kiwis have to go so the Aussies can come over. #ENGvNZ", 
   "- Kailua Kona, Hawaii : #travel #tour #trip #vacation #holiday #adventure #place #destinations #outd http://t.co/MocA1VMmup", 
   "- Kailua Kona, Hawaii : #travel #tour #trip #vacation #holiday #adventure #place #destinations #outd http://t.co/QHf2neoDHn", 
   "- Kailua Kona, Hawaii : #travel #tour #trip #vacation #holiday #adventure #place #destinations #outd http://t.co/vTbrdqw4tq", 
   "-----&gt; http://t.co/5cE07Ey3JM Some amazing places of India #IncredibleIndia #tourism #travel #traveltips #PlacesILove   http://t.co/p3fnVJhNGS", 
   "----&gt; http://t.co/2vWtTWdBD6 Before dying make sure to visit these places buddies #travel #tourism #Backpacking #trip #tourists #cool"
   ), inreply = c("", "", "", "", "", ""), source = c("IFTTT", "Pinterest", 
   "Pinterest", "Pinterest", "TwitterWebClient", "TwitterWebClient"
   ), tweet_time = c("24/06/15 2:41", "30/06/15 11:54", "30/06/15 11:54", 
   "30/06/15 11:54", "15/06/15 15:45", "25/06/15 12:08"), screen_name = c("izenkast", 
   "myvacationtour", "myvacationtour", "myvacationtour", "Alchetron", 
   "Alchetron"), followers = c(7223L, 489L, 489L, 489L, 60909L, 
   61862L), friends = c(759L, 15L, 15L, 15L, 42989L, 49767L), favorites = c(3L, 
   0L, 0L, 0L, 72L, 88L), statuses = c(16761L, 4069L, 4065L, 4066L, 
   301L, 343L), membersince = c("04/01/15 0:23", "26/04/12 10:11", 
   "26/04/12 10:11", "26/04/12 10:11", "08/11/13 21:09", "08/11/13 21:09"
   ), timezone = c("Chennai", "ahmedabad,Gujarat,India,380015", 
   "ahmedabad,Gujarat,India,380015", "ahmedabad,Gujarat,India,380015", 
   "Mumbai, Maharashtra", "Mumbai, Maharashtra"), tweet_date = c("24/06/15", 
   "30/06/15", "30/06/15", "30/06/15", "15/06/15", "25/06/15"), 
   twit_time = c("24/06/15 2:41", "30/06/15 11:54", "30/06/15 11:54", 
   "30/06/15 11:54", "15/06/15 15:45", "25/06/15 12:08"), rtt = c("T", 
   "T", "T", "T", "T", "T"), state = c("TN", "Guj", "Guj", "Guj", 
   "Mah", "Mah"), zone = c("South", "West", "West", "West", 
   "West", "West")), .Names = c("text", "inreply", "source", 
   "tweet_time", "screen_name", "followers", "friends", "favorites", 
   "statuses", "membersince", "timezone", "tweet_date", "twit_time", 
   "rtt", "state", "zone"), row.names = c(NA, 6L), class = "data.frame")

我希望标准文本消息注释到图表的右侧底部,而不管x轴范围如何。我使用Inf(ggplot cookbook中显示的示例),返回以下错误。

Error in annotate("text", x = Inf, y = -Inf, label = "This is a test",  : 
unused arguments (x = Inf, y = -Inf, label = "This is a test", hjust = 1.1, vjust = -0.5)

会话信息:

R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] chron_2.3-45       gridExtra_0.9.1    topicmodels_0.2-2  lda_1.3.2            sna_2.3-2         
[6] igraph_0.7.1       slam_0.1-32        wordcloud_2.5      tm_0.6-1           NLP_0.1-7         
[11] repmis_0.4.4       RColorBrewer_1.1-2 ggplot2_1.0.1      stringr_1.0.0      shinyapps_0.4.1.4 
[16] downloader_0.4     quantmod_0.4-5     TTR_0.23-0         xts_0.9-7          zoo_1.7-12        
[21] mapproj_1.2-2      maps_2.3-9         shiny_0.12.1      

loaded via a namespace (and not attached):
[1] modeltools_0.2-21 reshape2_1.4.1    lattice_0.20-31   colorspace_1.2-6     stats4_3.2.0     
[6] htmltools_0.2.6   SnowballC_0.5.1   R.oo_1.19.0       R.utils_2.1.0       R.cache_0.10.0   
[11] plyr_1.8.2        munsell_0.4.2     gtable_0.1.2      R.methodsS3_1.7.0 labeling_0.3     
[16] httpuv_1.3.2      parallel_3.2.0    proto_0.3-10      Rcpp_0.11.6       xtable_1.7-4     
[21] scales_0.2.4      jsonlite_0.9.16   mime_0.3          digest_0.6.8      stringi_0.4-1    
[26] RJSONIO_1.3-0     tools_3.2.0       bitops_1.0-6      magrittr_1.5      RCurl_1.95-4.7   
[31] MASS_7.3-40       data.table_1.9.4  httr_0.6.1        rstudioapi_0.3.1  R6_2.0.1         
[36] rstudio_0.98.1103

1 个答案:

答案 0 :(得分:2)

NLP 也有一个annotate函数,因为你在 ggplot2 之后加载了它,NLP::annotate最终掩盖了ggplot2::annotate 。在 ggplot2 之后加载 NLP 时,会收到有关屏蔽的警告消息:

library(ggplot2)    
library(NLP)
  

附加包裹:'NLP'

     

从'package:ggplot2'屏蔽以下对象:

annotate

您始终可以使用包名称和两个冒号显式使用特定包中的函数。在您的情况下,如果您当前会话中的工作涉及包 NLP 并且您不想返回并切换,请使用ggplot2::annotate而不是annotate来避免此问题包装加载的顺序。