我想将ggplot折线图的颜色从深蓝色设置为浅紫色。
我认为我可能必须使用彩色啤酒机,但是我不确定这样做的正确方法
我可以将高颜色设置为某些颜色,将低颜色设置为其他颜色吗?
iris %>%
ggplot(aes(Sepal.Length, Sepal.Width, group = Species)) +
geom_line()
答案 0 :(得分:1)
scale_color_gradient
适用于连续数据,但不适用于因子。
您可以使用colorRampPalette
酿造自己的调色板,该调色板可以在两种所选颜色之间插入任意数量的所需阴影。
R的默认颜色中没有名为“浅紫色”的颜色,因此我使用了看起来很接近的颜色。尝试以下代码:
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.6.3
### create color making function
colfunct <- colorRampPalette(c("darkblue", "darkorchid1 "))
### brew desired number of colors, which is 3 for the built-in iris dataset
shades <- colfunct(3)
### plot the data and map color to the factor of interest
ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species))+
geom_point(shape=1)+
geom_smooth(method = lm, formula = y~x, se=FALSE)+
scale_color_manual(values = shades)
由reprex package(v0.3.0)于2020-10-10创建