这是一个可复制的样本数据集,具有基于的连续填充Amount
。正如你可以从下面的示例中看到的,我也有一列d
叫flag
。我想要的是能够flag
通过将条形图的边框颜色更改为红色来进行可视化。
# create reproducible data
library(ggplot2)
d <- read.csv(text='Day,Location,Length,Amount,flag
1,4,3,1.1,FALSE
1,3,1,2,FALSE
1,2,3,4,FALSE
1,1,3,5,FALSE
2,0,0,0,FALSE
3,3,3,1.8,TRUE
3,2,1,3.54,FALSE
3,1,3,1.1,FALSE',header=T)
ggplot(d, aes(x = Day, y = Length)) +
geom_bar(aes(fill = Amount, order = -Location), stat = "identity")
我已经尝试过了alpha
,但确实可以标记所需的条形,但是无法以我想要的方式可视化数据:
ggplot(d, aes(x = Day, y = Length)) +
geom_bar(aes(fill = Amount, order = -Location, alpha = -flag), stat = "identity")
alpha
使用-flags
(note -
)时也会使图例混乱,这是不可取的。
使用以下命令返回一个错误,我需要2个颜色变量,但我只需要1个,red
:
ggplot(d, aes(x = Day, y = Length)) +
geom_bar(aes(fill = Amount, order = -Location, alpha = -flag), stat = "identity") +
scale_colour_manual(values = alpha(c('red')))
回顾一下:
我想flag = TRUE
用红色轮廓(或同样明显的东西)显示该条,而无需更改已经确定的连续比例Amount
。
我希望图例以flag = TRUE
红色(或其他匹配颜色)反映出来。
我找不到在网上浏览的适用解决方案,因此非常感谢您的任何想法或建议!
使用colour=flag
(colour
是geom_bar
填充中的轮廓是填充)。然后添加if为假(无边框)时使用+ scale_colour_manual(values=c(NA, 'red'))
where NA
,如果flag
为true则使用'red' flag
。
ggplot(d, aes(x = Day, y = Length)) +
geom_bar(aes(fill = Amount, order = -Location, col=flag), stat = "identity") +
scale_colour_manual(values=c(NA, 'red'))
(注意:相反,您可以geom_bar(aes(...), col=ifelse(d$flag, 'red', NA))
跳过,scale_colour_manual
但是没有图例)。
如果你想增加边框宽度,加lwd=<new line width>
在你的geom_bar
(外aes
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句