Автоматический выбор более отличительной цветовой палитры для ggplot2

#r #ggplot2

#r #ggplot2

Вопрос:

Я использую следующий код для создания серии графиков, которые включают оба geom_boxplot и geom_point . Я хочу, чтобы точки имели более отличительные цвета без того, чтобы я каждый раз определял, какие они:

 for (i in 1:length(Girder.Plot)) {
  Plot.Girder <- ggplot(data = subset(Girder.Plot[[i]], Names == "Sample"), 
         aes(x = Type, y = Moment, fill = factor(Spacing,levels = c("9","12","15"))))   
    geom_boxplot(outlier.shape = NA, position = position_dodge(width = 0.75))  
    stat_summary(fun = mean, geom="point", shape=23, size=2, 
             position = position_dodge(width = 0.75))   
    stat_boxplot(geom='errorbar', linetype=1, width=0.5,
             position = position_dodge(width = 0.75))   
    geom_point(data = subset(Girder.Plot[[i]], Names == "No Factor"), 
           aes(colour = Names), position = position_dodge(width = 0.75))  
    geom_point(data = subset(Girder.Plot[[i]], Names == "Factor 1"), 
           aes(colour = Names), position = position_dodge(width = 0.75))  
    geom_point(data = subset(Girder.Plot[[i]], Names == "Factor 2"), 
           aes(colour = Names), position = position_dodge(width = 0.75))  
    geom_point(data = subset(Girder.Plot[[i]], Names == "Factor 3"), 
           aes(colour = Names), position = position_dodge(width = 0.75))  
    labs(x = element_blank(), y = element_blank(), 
     title = paste0("Moment Live Load Distribution Factors n Along the Roadway Width for n Ultra-Girder Section: UG-"
                   ,str_extract(names(Girder.Plot)[i],"\d ")),
     fill = "Girder Spacing (ft):", colour = element_blank())  
    theme_classic()   ylim(0.4, 1.1)  
    theme(plot.title = element_text(hjust = 0.5, margin = margin(45,0,20,0), 
                                face = "bold", size = 18), 
      legend.title.align = 0.5, legend.position = "bottom", 
      legend.box.background = element_rect(colour = "black", size = 0.5),
      legend.box.margin = margin(0,0,0,0))
  print(Plot.Girder)
}
  

введите описание изображения здесь

Ответ №1:

Используйте scale_fill_brewer / scale_color_brewer .

 library(ggplot2)

ggplot(iris, aes(Species, Sepal.Length, fill = Species))  
  geom_boxplot()  
  theme_minimal()  
  scale_fill_brewer(palette = "Set1")
  

ирис

Чтобы просмотреть доступные палитры.

 RColorBrewer::display.brewer.all()
  

палитры