Używam poniższego kodu, aby wygenerować serię działek, które obejmują zarówno geom_boxplot, jak i geom_point. Chcę, aby punkty mają bardziej charakterystyczne kolory bez mnie określające, co są za każdym razem:

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)
}

enter image description here

0
user14355414 5 październik 2020, 07:34

1 odpowiedź

Najlepsza odpowiedź

Użyj 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")

iris

Zobaczyć dostępne palety.

RColorBrewer::display.brewer.all()

palettes

0
Paul 5 październik 2020, 04:46