我喜欢在我的图表中添加以下标题:
注:美国、英国和荷兰的市场集中度平均值分别为 1920、1388 和 1244
其中“注:”需要斜体,“荷兰分别为1920、1388和1244”应换行。
使用该paste功能,我可以将一部分斜体化。但是使用\ninside paste,可以将所有内容混合在一起,正如您在此处看到的那样(这是经过编辑的图像,使用以下 Paul 的建议制作):
我尝试了各种其他解决方案,但没有成功。这是我正在使用的代码:
library(ggplot2)
note = expression(paste(italic("Note: "), "Market concentration averages in the United States, United Kingdom, and the \nNetherlands are, respectively, 1920, 1388, and 1244"))
gg <- ggplot(mtcars, aes(wt, mpg)) + geom_point()+
# Title
labs(caption=note)
gg + theme(plot.caption=element_text(size=7.5, hjust=0, margin=margin(t=15)))
Run Code Online (Sandbox Code Playgroud) 我在我的计数器中添加了词形还原,正如Sklearn页面上所解释的那样.
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
class LemmaTokenizer(object):
def __init__(self):
self.wnl = WordNetLemmatizer()
def __call__(self, articles):
return [self.wnl.lemmatize(t) for t in word_tokenize(articles)]
tf_vectorizer = CountVectorizer(tokenizer=LemmaTokenizer,
strip_accents = 'unicode',
stop_words = 'english',
lowercase = True,
token_pattern = r'\b[a-zA-Z]{3,}\b', # keeps words of 3 or more characters
max_df = 0.5,
min_df = 10)
Run Code Online (Sandbox Code Playgroud)
但是,在创建时DTM使用fit_transform,我得到的错误如下(其中我也没有什么意义).在将词形还原添加到我的矢量化器之前,dtm代码始终有效.我深入研究了手册,并尝试了一些代码,但找不到任何解决方案.
dtm_tf = tf_vectorizer.fit_transform(articles)
Run Code Online (Sandbox Code Playgroud)
更新:
按照下面的@ MaxU的建议,代码运行没有错误,但数字和标点符号没有从我的输出中省略.我运行单独的测试,看看以后哪些功能有效,哪些LemmaTokenizer()无效.结果如下:
strip_accents = 'unicode', # works
stop_words = 'english', # works
lowercase …Run Code Online (Sandbox Code Playgroud)