gpt4 book ai didi

r - 注释ggplot条形图错误: Unused arguments

转载 作者:行者123 更新时间:2023-12-04 00:17:34 27 4
gpt4 key购买 nike

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

    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 食谱中显示的示例),返回以下错误。
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)

session 信息:
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

最佳答案

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

library(ggplot2)    
library(NLP)

Attaching package: ‘NLP’

The following object is masked from ‘package:ggplot2’:

annotate


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

关于r - 注释ggplot条形图错误: Unused arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32056718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com