scale_size_continuous() 的自定义颜色
Custom color for scale_size_continuous()
在下图中,如何以绿色显示所有 cty >= 30
值,以红色显示 cty < 30
值?谢谢!
library(ggplot2)
data(mpg)
p <- ggplot() +
geom_point(data = mpg, aes(displ, manufacturer, size = cty), alpha = 0.2) +
scale_size_continuous(breaks = c(30, 20, 10)) +
scale_color_manual(values = c("green", "red", "red")) # does not work
p
我不认为数据集中有任何 cty
值大于 30,但由于这只是一个示例,让我们将截止值设置为 15:
ggplot() +
geom_point(data = mpg, aes(displ, manufacturer, size = cty,
color = cty <= 15), alpha = 0.2) +
scale_size_continuous(breaks = c(30, 20, 10)) +
scale_color_manual(values = c("green", "red"))
如果你想合并图例,你需要一个模拟离散色标的连续色标,比如:
ggplot() +
geom_point(data = mpg, aes(displ, manufacturer, size = cty,
color = cty), alpha = 0.2) +
scale_size_continuous(breaks = c(30, 20, 10)) +
scale_color_gradientn(colors = c("red", 'red', "green", 'green'),
values = c(0, 0.5, 0.50001, 1),
breaks = c(30, 20, 10),
guide = guide_legend())
在下图中,如何以绿色显示所有 cty >= 30
值,以红色显示 cty < 30
值?谢谢!
library(ggplot2)
data(mpg)
p <- ggplot() +
geom_point(data = mpg, aes(displ, manufacturer, size = cty), alpha = 0.2) +
scale_size_continuous(breaks = c(30, 20, 10)) +
scale_color_manual(values = c("green", "red", "red")) # does not work
p
我不认为数据集中有任何 cty
值大于 30,但由于这只是一个示例,让我们将截止值设置为 15:
ggplot() +
geom_point(data = mpg, aes(displ, manufacturer, size = cty,
color = cty <= 15), alpha = 0.2) +
scale_size_continuous(breaks = c(30, 20, 10)) +
scale_color_manual(values = c("green", "red"))
如果你想合并图例,你需要一个模拟离散色标的连续色标,比如:
ggplot() +
geom_point(data = mpg, aes(displ, manufacturer, size = cty,
color = cty), alpha = 0.2) +
scale_size_continuous(breaks = c(30, 20, 10)) +
scale_color_gradientn(colors = c("red", 'red', "green", 'green'),
values = c(0, 0.5, 0.50001, 1),
breaks = c(30, 20, 10),
guide = guide_legend())