更改图中变量的颜色

Changing colors of variables on a plot

我正在尝试更改此图上单个 'Years' 变量的颜色。到目前为止我有这个代码:

x <- ggplot(elect, aes(x = `Congressional Spending`, y = `United States VEP Midterm Turnout Rate`, color = Year)) +
     geom_point() + geom_smooth(method = lm, col = 'purple', size = 0.5)

我也试过使用:

scale_color_manual(values = c("red", "yellow",
                               "blue", "orange",
                               "green", "black"))

Error: Continuous value supplied to discrete scale

看看数据会很有帮助。问题很可能是您的变量 yeardouble。所以你需要在代码中调用 as.factor()(见下文):

library(tidyverse)
ggplot(mtcars, aes(x=gear, y=carb, color=as.factor(cyl))) +
  geom_point() +
  scale_color_manual(values = c("black","green","blue"))

reprex package (v2.0.0)

于 2022-04-27 创建