使用geom_bar()高亮显示每N个条
我试图突出显示每一个条,它是,比如说,5的倍数。但是,当有多个高亮显示的条时,我会得到不正确的条大小 下面是重现错误的示例代码:使用geom_bar()高亮显示每N个条,r,plot,ggplot2,geom-bar,R,Plot,Ggplot2,Geom Bar,我试图突出显示每一个条,它是,比如说,5的倍数。但是,当有多个高亮显示的条时,我会得到不正确的条大小 下面是重现错误的示例代码: library(ggplot2) smpl <- data.frame(sample(1:31, 1000, replace = T)) p <- ggplot(data = smpl, aes(x = smpl)) + geom_bar() p + geom_bar(aes(fill = (smpl %% 5 == 0))) 这两种
library(ggplot2)
smpl <- data.frame(sample(1:31, 1000, replace = T))
p <- ggplot(data = smpl, aes(x = smpl)) +
geom_bar()
p +
geom_bar(aes(fill = (smpl %% 5 == 0)))
这两种方法都会产生以下错误消息:
position_stack requires non-overlapping x intervals
通过谷歌搜索这个错误,我开始讨论POSIXct日期的格式,需要手工编写布尔掩码,这不适用于这个用例
但是,
有人对此有补救办法吗?我无法手动编辑图表,正如我在此处所需示例中所做的那样。(1)在aes
中使用变量名,而不是整个数据帧的名称(因此您没有发布警告)。(2) 使用显式组
。(3) 使用position=“identity”
(默认值为“stack”
)
#使用正确的变量名创建数据
smpl完美,这正是我想要的,非常感谢您的解释!
position_stack requires non-overlapping x intervals
# create the data with a proper variable name
smpl <- data.frame(x = sample(1:31, 1000, replace = T))
ggplot(data = smpl, aes(x = x)) +
geom_bar(aes(fill = (x %% 5 == 0), group = x), position = "identity")