R 将调色板与ggplot2主题关联

R 将调色板与ggplot2主题关联,r,ggplot2,R,Ggplot2,我希望我的ggplot2主题使用一组特定的颜色,但不知道如何避免主题之外的单独线条 我有以下数据: library(ggplot2) mycars <- mtcars mycars$cyl <- as.factor(mycars$cyl) 我试过: mytheme2 <- mytheme + scale_color_manual(values = mycolors) mytheme2您好,您可以将自定义元素放入列表中: # Data library("ggplot2")

我希望我的ggplot2主题使用一组特定的颜色,但不知道如何避免主题之外的单独线条

我有以下数据:

library(ggplot2)
mycars <- mtcars
mycars$cyl <- as.factor(mycars$cyl)

我试过:

mytheme2 <- mytheme + scale_color_manual(values = mycolors)

mytheme2您好,您可以将自定义元素放入
列表中

# Data
library("ggplot2")
mycars <- mtcars
mycars$cyl <- as.factor(mycars$cyl)

# Custom theme
mytheme <- theme(panel.grid.major = element_line(size = 2))
mycolors <- c("deeppink", "chartreuse", "midnightblue")
# put the elements in a list
mytheme2 <- list(mytheme, scale_color_manual(values = mycolors))

# plot 
ggplot(mycars, aes(x = wt, y = mpg)) +
  geom_point(aes(color = cyl)) +
  mytheme2
#数据
图书馆(“ggplot2”)

mycars我有时会用这个小把戏在情节之间改变调色板

library(ggplot2)
mycars <- mtcars
mycars$cyl <- as.factor(mycars$cyl)

scale_colour_discrete <- function(...) scale_colour_manual(values=palette())

(p <- ggplot(mycars, aes(x = wt, y = mpg)) +
  geom_point(aes(color = cyl)) )


当我在图表上使用此主题时,如果
scale\u color\u manual
不适用,这看起来不会导致问题。我正在为其他颜色场景添加它,它似乎工作得很好,例如,
mytheme2
mytheme2 <- mytheme + scale_color_manual(values = mycolors)
# Data
library("ggplot2")
mycars <- mtcars
mycars$cyl <- as.factor(mycars$cyl)

# Custom theme
mytheme <- theme(panel.grid.major = element_line(size = 2))
mycolors <- c("deeppink", "chartreuse", "midnightblue")
# put the elements in a list
mytheme2 <- list(mytheme, scale_color_manual(values = mycolors))

# plot 
ggplot(mycars, aes(x = wt, y = mpg)) +
  geom_point(aes(color = cyl)) +
  mytheme2
library(ggplot2)
mycars <- mtcars
mycars$cyl <- as.factor(mycars$cyl)

scale_colour_discrete <- function(...) scale_colour_manual(values=palette())

(p <- ggplot(mycars, aes(x = wt, y = mpg)) +
  geom_point(aes(color = cyl)) )
palette(c("deeppink", "chartreuse", "midnightblue"))
p
palette(RColorBrewer::brewer.pal(5, "Set1"))
p