这是一个可重现的样本数据集,具有基于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
时(注意-
)也会混淆传说,这是不可取的。
使用以下命令返回一个我需要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
反映为红色(或任何匹配项)。
我无法找到适合网络的解决方案,所以我们非常感谢任何想法或建议!
答案 0 :(得分:2)
使用colour=flag
(colour
是geom_bar
中的大纲,而填充是填充)。然后添加+ scale_colour_manual(values=c(NA, 'red'))
,如果NA
为假(没有边框),则使用flag
,并且“红色”&#39;如果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
。