如何在R中绘制简单的名称与值?
我有下面的数据(在data.table中),我只想在x轴上绘制前20个如何在R中绘制简单的名称与值?,r,charts,R,Charts,我有下面的数据(在data.table中),我只想在x轴上绘制前20个rn,在y轴上绘制相应的p 它看起来像下图,只是我想以某种方式指出哪个数据点是来自rn的单词。也可以是另一种图表类型 它可能会变成这样: ggplot2只需几行即可绘制出这样的图形: ggplot(tdmunidt, aes(x = reorder(rn, -p), y = p)) + geom_bar(stat="identity") + xlab("") + theme(axis.text.x = e
rn
,在y轴上绘制相应的p
它看起来像下图,只是我想以某种方式指出哪个数据点是来自rn
的单词。也可以是另一种图表类型
它可能会变成这样:
ggplot2
只需几行即可绘制出这样的图形:
ggplot(tdmunidt, aes(x = reorder(rn, -p), y = p)) +
geom_bar(stat="identity") + xlab("") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
如果其他人想玩这些数据,我是这样复制的:
require(dplyr)
str <- "rn blogs news twit sm p logp
1: the 1042 1208 181 2431 0.06695679 -2.703708
2: said 170 1241 35 1446 0.03982703 -3.223209
3: will 604 522 196 1322 0.03641171 -3.312865
4: one 678 413 203 1294 0.03564051 -3.334272
5: just 575 283 321 1179 0.03247308 -3.427344
6: like 576 254 239 1069 0.02944336 -3.525287
7: can 555 279 186 1020 0.02809376 -3.572208
8: time 504 245 150 899 0.02476107 -3.698483
9: get 380 210 244 834 0.02297078 -3.773532
10: new 338 332 142 812 0.02236483 -3.800265
11: now 354 182 157 693 0.01908723 -3.958736
12: good 289 145 217 651 0.01793043 -4.021256
13: know 363 116 166 645 0.01776517 -4.030516
14: people 299 241 95 635 0.01748974 -4.046141
15: day 278 141 207 626 0.01724185 -4.060415
16: but 293 293 37 623 0.01715923 -4.065219
17: also 296 286 34 616 0.01696643 -4.076519
18: first 278 236 68 582 0.01602997 -4.133295
19: year 177 320 65 562 0.01547911 -4.168264
20: and 313 164 74 551 0.01517614 -4.188031"
str <- strsplit(str, "\n")[[1]] %>%
gsub("^ ?\\d+:\\s+", "", .) %>%
gsub("[ ]+", ",", .) %>%
paste(collapse="\n")
tdmunidt <- as.data.table(read.csv(textConnection(str)))
require(dplyr)
str%
gsub(“[]+”,“,”,)%>%
粘贴(折叠=“\n”)
tdmunidt对于词频分布的统计分析,该软件包可能很有用。特别是,它包含一个函数plot.tfl()
,在我看来,它对解决您的问题很有希望。但是,我从来没有使用过那个软件包,所以我不能肯定它是否提供了您想要的。谢谢,但我想这应该是可能的,不必求助于另一个软件包。有很多选择。您可以尝试使用(tdmunidt,{plot(p,xaxt=“n”);axis(1,at=seq(rn,labels=rn)}
或使用(tdmunidt,barplot(p,names.arg=rn,las=2))
或箱线图(p~rn,transform(tdmunidt,rn=reorder(rn,-,--p))
,如果rn是一个因素的话。为什么对我的问题投了反对票?这会是一样的吗。。。。aes(x=reorder(factor(rn),p),y=p…?换句话说,使用reorder()调用?是的,这是正确的,@lawyeR。直到我在上面的评论中看到它,我才意识到这个函数,但现在我的答案中包含了它。非常优雅!
require(dplyr)
str <- "rn blogs news twit sm p logp
1: the 1042 1208 181 2431 0.06695679 -2.703708
2: said 170 1241 35 1446 0.03982703 -3.223209
3: will 604 522 196 1322 0.03641171 -3.312865
4: one 678 413 203 1294 0.03564051 -3.334272
5: just 575 283 321 1179 0.03247308 -3.427344
6: like 576 254 239 1069 0.02944336 -3.525287
7: can 555 279 186 1020 0.02809376 -3.572208
8: time 504 245 150 899 0.02476107 -3.698483
9: get 380 210 244 834 0.02297078 -3.773532
10: new 338 332 142 812 0.02236483 -3.800265
11: now 354 182 157 693 0.01908723 -3.958736
12: good 289 145 217 651 0.01793043 -4.021256
13: know 363 116 166 645 0.01776517 -4.030516
14: people 299 241 95 635 0.01748974 -4.046141
15: day 278 141 207 626 0.01724185 -4.060415
16: but 293 293 37 623 0.01715923 -4.065219
17: also 296 286 34 616 0.01696643 -4.076519
18: first 278 236 68 582 0.01602997 -4.133295
19: year 177 320 65 562 0.01547911 -4.168264
20: and 313 164 74 551 0.01517614 -4.188031"
str <- strsplit(str, "\n")[[1]] %>%
gsub("^ ?\\d+:\\s+", "", .) %>%
gsub("[ ]+", ",", .) %>%
paste(collapse="\n")
tdmunidt <- as.data.table(read.csv(textConnection(str)))