这是一个可能满足您需求的快速技巧(尽管我敢打赌有更好的方法来做到这一点):
library(dplyr)
# Data frame with each word appearing a number of times equal to its frequency
df.freq = data.frame(words=rep(words, freq))
# Add a counter from 1 to freq for each word.
# This will become the `y` value in the graph.
df.freq = df.freq %>%
group_by(words) %>%
mutate(counter=1:n())
# Graph the words as if they were points in a scatterplot
p1 = ggplot(df.freq, aes(words, counter-0.5)) +
geom_text(aes(label=words), size=12) +
scale_y_continuous(limits=c(0,max(df.freq$counter))) +
labs(x="Words",y="Freq") +
theme_tufte(base_size=20) +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank())
# Save the plot, adjusting the aspect ratio so that the words stack nicely
# without large gaps between each copy of the word
pdf("word stack.pdf", 6,3.5)
p1
dev.off()
这是一个png
版本,因为 SO 不显示 PDF 文件。
如果您不打算使用一堆单词,另一种选择是坚持使用条形图并将单词添加到每个条的中间。例如:
# a toy data frame
words <- c("global", "local", "firm")
freq <- c(3, 5, 6)
df <-data.frame(words, freq)
ggplot(df, aes(words, freq)) +
geom_bar(stat="identity", fill=hcl(195,100,65)) +
geom_text(aes(label=words, y=freq*0.5), colour="white", size=10) +
theme_tufte(base_size=20) +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank())