voi*_*nyx 3 r count legend ggplot2
我想使用来自ggplot2的geom_count图,但是值的范围太小,图例中断变成了发生次数的浮点,例如 1 1.5 2 2.5 3
这是一个测试案例:
test = mtcars[1:6,]
ggplot(test, aes(cyl, carb)) +
geom_count(aes(color = ..n.., size = ..n..)) +
guides(color = 'legend')
Run Code Online (Sandbox Code Playgroud)
如何使中断仅发生在整数上?
您可以breaks为连续color和size刻度设置。
您可以给出中断值的向量,但根据文档,breaks也可以给出自变量:
一个将限制作为输入并返回中断作为输出的函数
因此,对于像您的示例这样的简单案例,您可以使用as.integer或round作为函数。
ggplot(test, aes(cyl, carb)) +
geom_count(aes(color = ..n.., size = ..n..)) +
guides(color = 'legend') +
scale_color_continuous(breaks = round) +
scale_size_continuous(breaks = round)
Run Code Online (Sandbox Code Playgroud)
对于比您的示例更大的整数范围,您可以手动输入中断,例如breaks = 1:3,或编写一个接受小数位数限制并返回整数序列的函数。然后,您可以将此功能用于breaks。
可能看起来像:
set_breaks = function(limits) {
seq(limits[1], limits[2], by = 1)
}
ggplot(test, aes(cyl, carb)) +
geom_count(aes(color = ..n.., size = ..n..)) +
guides(color = 'legend') +
scale_color_continuous(breaks = set_breaks) +
scale_size_continuous(breaks = set_breaks)
Run Code Online (Sandbox Code Playgroud)