%useLatestDescriptors
%use lets-plot
fun genWord(length:Int):String {
val letters = ('a'..'z')
return List(length) { letters.random() }.joinToString("")
}
fun genWordSet(n:Int):Set<String> {
val words = HashSet<String>()
while(words.size < n) {
words.add(genWord(5))
}
return words
}
fun data(n:Int, words:Set<String>):Map<String, *> {
return mapOf(
"word" to List(n) { words.random() },
"g" to List(n) { listOf('a','b','c').random() }
)
}
// Number of unique words exceeds threshold (50) of default 'pick' sampling on bar chart.
val words = genWordSet(500)
val dat = data(1000, words)
val p = ggplot(dat) { x = "word" }
// Disable sampling to see the overplotting.
p + geomBar(sampling = samplingNone, size = 0)
// Draw plot with default sampling.
p + geomBar(size = 0)
// 'pick' sampling preserves groups on bar chart.
p + geomBar(size = 0) { fill = "g" }
// Orgering words by `count` might be a good idea.
p + geomBar(size = 0) {
fill = "g"
x = asDiscrete("word", orderBy = "..count..")
}