ggplot geom_bar 基于条件的单独颜色的连续颜色填充
ggplot geom_bar continuous colour fill with a separate color based on a conditional
这是一个可重现的示例数据集,它具有基于 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
时(注意 -
)也会弄乱图例,这是不可取的。
使用下面的命令 returns 一个错误,我需要 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
中的轮廓,而fill是填充)。然后添加 + scale_colour_manual(values=c(NA, 'red'))
,如果 flag
为假(无边框),则使用 NA
,如果 flag
为真,则使用 'red'。
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
,但您不会得到图例)。
如果您想增加边框宽度,请在 geom_bar
中添加一个 lwd=<new line width>
(在 aes
之外)。
这是一个可重现的示例数据集,它具有基于 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
时(注意 -
)也会弄乱图例,这是不可取的。
使用下面的命令 returns 一个错误,我需要 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
中的轮廓,而fill是填充)。然后添加 + scale_colour_manual(values=c(NA, 'red'))
,如果 flag
为假(无边框),则使用 NA
,如果 flag
为真,则使用 'red'。
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
,但您不会得到图例)。
如果您想增加边框宽度,请在 geom_bar
中添加一个 lwd=<new line width>
(在 aes
之外)。