绘制情感对象:常规图-按y轴或ggplot排序

时间:2019-02-04 10:38:02

标签: r ggplot2 plot sentimentr

我想寻求一些帮助,以在R中绘制某些内容。使用plot或ggplot都可以,在两种情况下,我都能到达某处,但距离不远。 我有一个名为dat的数据框。这是它的结构:

str(dat)
'data.frame':   223 obs. of  3 variables:
 $ x: chr  "THE SOCIAL VOLCANO THESIS STATES THAT THE RISING INEQUALITY IN CHINA   THREATENS REGIME STABILITY. THIS IDEA, A"| __truncated__ "THE ASIAN INFRASTRUCTURE INVESTMENT BANK (AIIB) HAS BEEN WIDELY   CONCEIVED AS A CHINESE EFFORT TO PROMOTE REFO"| __truncated__ "USING A MIXED-METHODS APPROACH, THIS ARTICLE SURVEYS AND COMPARES HOW   CHINESE YOUTH VIEW CHINA'S RISE WITH HO"| __truncated__ "CHINA'S DISTORTED REGIONAL POLICY AND GEOGRAPHICAL FACTORS HAVE LED TO   UNBALANCED DEVELOPMENT IN VARIOUS REGI"| __truncated__ ...
 $ y: chr  "2018" "2018" "2018" "2018" ...
 $ z: int  1 2 3 4 5 6 7 8 9 10 ...

我使用了sentimentr软件包中的两个功能:

(out <- with(
  dat,
  sentiment_by(
    get_sentences(x),
    list(y)
  )))

> str(out)
Classes ‘sentiment_by’, ‘sentiment’, ‘data.table’ and 'data.frame': 26 obs. of  4 variables:
 $ y            : chr  "1993" "1994" "1995" "1996" ...
 $ word_count   : int  460 662 685 1434 554 266 184 987 388 1238 ...
 $ sd           : num  0.305 0.31 0.233 0.255 0.243 ...
 $ ave_sentiment: num  0.0582 0.0129 -0.0211 0.1034 0.0357 ...
 - attr(*, "sorted")= chr "y"
 - attr(*, ".internal.selfref")=<externalptr> 
 - attr(*, "sentiment")=<environment: 0x0000000035255940> 
 - attr(*, "groups")= chr "y"
 - attr(*, "uncombine")=<environment: 0x0000000033925198> 

> class(out)
[1] "sentiment_by" "sentiment"    "data.table"   "data.frame"  

> dput(out)
structure(list(y = c("1993", "1994", "1995", "1996", "1997", 
"1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", 
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", 
"2014", "2015", "2016", "2017", "2018"), word_count = c(460L, 
662L, 685L, 1434L, 554L, 266L, 184L, 987L, 388L, 1238L, 281L, 
801L, 781L, 1408L, 1379L, 1482L, 1277L, 1542L, 1202L, 1340L, 
4560L, 1774L, 2762L, 1329L, 2849L, 3282L), sd = c(0.304636023530558, 
0.309738374286375, 0.232934189622111, 0.255094571121293, 0.243144303515172, 
0.282911733030912, 0.215097562186623, 0.231165262827222, 0.203962930002132, 
0.291153465425392, 0.281978569365284, 0.386429241666039, 0.287562317308312, 
0.280925019904095, 0.21898183336857, 0.254307322561706, 0.282036183270972, 
0.268457123175779, 0.3937414839572, 0.33836367400151, 0.286163321374482, 
0.313361278896165, 0.306199068554844, 0.333909633230004, 0.276550678783484, 
0.292100796402794), ave_sentiment = c(0.0581999593535032, 0.0128947674652892, 
-0.0210839988614244, 0.103407736319711, 0.0357332646332619, -0.109632067857938, 
0.0227747336154236, 0.00442991862324721, 0.0440658684619395, 
0.02463434314142, -0.088116710594565, -0.0712639455279134, -0.00723038792159052, 
0.0554336776898467, 0.0510679816065003, 0.0658980030024626, 0.0636196211892638, 
-0.00144966905344101, 0.0790633232056969, 0.100195761227613, 
0.0500074909381625, 0.117002501770641, -0.0794863781585957, 0.020582509043321, 
0.0392390469577238, 0.0133066447300312)), row.names = c(NA, 26L
), class = c("sentiment_by", "sentiment", "data.table", "data.frame"
), .Names = c("y", "word_count", "sd", "ave_sentiment"), sorted = "y", .internal.selfref = <pointer: 0x0000000004750788>, sentiment = <environment>, groups = "y", uncombine = <environment>)
>  

在感悟者中,建议像这样绘制

plot(out)

a busy cat

,区别在于我有几年而不是罗姆尼或奥巴马。我的问题是,我想根据年份对y轴进行排序,从1993年至2018年或相反。现在,情节功能根据最积极的情绪进行组织。由于有序数据框(或几件事的组合?我不确定是什么动物),我不知道该如何排序,也没有在在线搜索时找到信息。

我通常更喜欢ggplot语言,或者确切地说,我更喜欢学习它。但是,我不确定如何将所有参数传递给ggplot,也许一周前就已经开始使用它了。我目前只知道该怎么做

ggplot(out, aes(x = ave_sentiment, y = y)) +
  geom_point(color = "darkorchid4")

在那种情况下,我显然看不到所有的小点。即使它们来自哪里,我也不确定。 我意识到这个问题可能非常简单,但是请您忍受,到目前为止,我只复制过R包中的手册。谢谢!

0 个答案:

没有答案