R小技巧

词云图wordcloud2:做出好看的词云图

2021-05-25  本文已影响0人  被格格巫抓到的蓝精灵

展示字符串的利器,画一个漂亮的词云图

最简单的使用

library(wordcloud2)
#需要展示的词云
city<-c("中国","河南","河北","广东","安徽","黑龙江","牡丹江","广西","福建","甘肃","桂林","北京",
        "上海","河北","保定","哈尔滨","佛山","开封","宁夏","新疆","西藏","海南","台湾",'香港',
        '长沙','江西','赣州','四川','成都','重庆','绵阳','锦州','武汉','厦门','福州')
#设定一个颜色集,也可以选用默认的
cbPalette<-c("#a50026","#D35400","#006d2c","#006d2c","#a6cee3","#1f78b4","#b2df8a",
             "#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6")
#设定字符的相对大小
city<-data.frame(city = city,freq = sample(34:98,35))
#run
wordcloud2(city,size = 0.3,color = sample(cbPalette,35,replace = T))
随机形状的词云图展示

设定特定的形状

#为了使星星的形状更明显,我们增加了字符的数量
city<-data.frame(city = sample(city_name,140,T),freq = sample(34:98,140,T))
wordcloud2(city,size = 0.1,
                  color = sample(cbPalette,140,replace = T),
                  backgroundColor = "white",#设置背景颜色
                 shape = 'star'#设置为星型;
#能选择的形状还有:'circle','cardioid','diamond','triangle-forward','triangle','pentagon'
)
星星的词云图
上一篇下一篇

猜你喜欢

热点阅读