Reputation: 43
I have a dataset with the F1 teams and points they scored since the beginning with 203 rows.
I would like to make a barplot, but color-filling only the bars for constructors from 2020, and leave the rest in one colour. Is it even possible?
Find data below. The code I'm using to plot so far is:
ggplot(data=top_constructors[1:input$top1,], aes(x=reorder(name, total_points), y=total_points, fill=name)) +
geom_bar(stat = "identity") +
coord_flip() +
xlab("Constructor") +
ylab("Total points") +
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9", rep("black", 200)))
The code above is coloring 3 first values alphabetically, and I would like to set certain colors for certain teams, eg. red for Ferrari etc.
Thank you for your help.
dput(top_constructors)
gives such an output:
structure(list(name = c("Ferrari", "Mercedes", "McLaren", "Red Bull",
"Williams", "Renault", "Force India", "Team Lotus", "Benetton",
"Tyrrell", "Lotus F1", "Brabham", "Sauber", "BRM", "Toro Rosso",
"Ligier", "Cooper-Climax", "Maserati", "BMW Sauber", "Jordan",
"Racing Point", "Lotus-Climax", "Alfa Romeo", "Toyota", "BAR",
"Lotus-Ford", "Haas F1 Team", "Brabham-Repco", "Brawn", "Honda",
"March", "McLaren-Ford", "Arrows", "Kurtis Kraft", "Matra-Ford",
"Vanwall", "AlphaTauri", "Cooper-Maserati", "Wolf", "Brabham-Climax",
"Brabham-Ford", "Shadow", "Surtees", "Matra", "Cooper", "Porsche",
"Jaguar", "Hesketh", "Stewart", "Fittipaldi", "Epperly", "Minardi",
"March-Ford", "Watson", "Prost", "Lotus-BRM", "Lola", "Toleman",
"Footwork", "Gordini", "Talbot-Lago", "Penske", "Larrousse",
"Kuzma", "Cooper-BRM", "Ensign", "Brabham-Alfa Romeo", "Connaught",
"Dallara", "Brabham-BRM", "Eagle-Weslake", "BRP", "Lesovsky",
"Deidt", "Shadow-Ford", "Lancia", "Leyton House", "ATS", "Phillips",
"Onyx", "Rial", "Parnelli", "Iso Marlboro", "McLaren-BRM", "Osella",
"Simca", "Super Aguri", "Eagle-Climax", "Embassy Hill", "Frazer Nash",
"Sherman", "Cooper-Castellotti", "AGS", "Zakspeed", "Theodore",
"HWM", "Schroeder", "Marussia", "Spyker", "Tecno", "Trevis",
"McLaren-Serenissima", "Manor Marussia", "MF1", "Spyker MF1",
"Forti", "Pacific", "Simtek", "Fondmetal", "Andrea Moda", "Lambo",
"Coloni", "Euro Brun", "Life", "RAM", "Spirit", "Merzario", "Kauhsen",
"Rebaque", "Martini", "LEC", "McGuire", "Boro", "Apollon", "Kojima",
"Maki", "Lyncar", "Trojan", "Amon", "Token", "Politoys", "Connew",
"Bellasi", "De Tomaso", "LDS", "Protos", "Shannon", "Scirocco",
"RE", "Derrington", "Gilby", "Stebro", "Emeryson", "ENB", "JBW",
"Ferguson", "MBM", "Behra-Porsche", "Scarab", "Meskowski", "Christensen",
"Ewing", "Aston Martin", "Moore", "Dunn", "Elder", "Sutton",
"Fry", "Tec-Mec", "Alta", "OSCA", "Stevens", "Bugatti", "Pawl",
"Pankratz", "Arzani-Volpini", "Nichels", "Bromme", "Klenk", "Turner",
"Del Roy", "Veritas", "BMW", "EMW", "AFM", "ERA", "Aston Butterworth",
"Cisitalia", "Hall", "Marchese", "Langley", "Rae", "Olson", "Wetteroth",
"Adams", "Snowberger", "Milano", "HRT", "Virgin", "Cooper-OSCA",
"Cooper-Borgward", "Lotus-Maserati", "De Tomaso-Osca", "De Tomaso-Alfa Romeo",
"Lotus-Borgward", "Cooper-Alfa Romeo", "De Tomaso-Ferrari", "LDS-Climax",
"LDS-Alfa Romeo", "Cooper-Ford", "Cooper-Ferrari", "Cooper-ATS",
"BRM-Ford", "McLaren-Alfa Romeo", "March-Alfa Romeo", "Lotus-Pratt & Whitney",
"Shadow-Matra", "Lotus", "Caterham"), total_points = c(9292.77,
5824.14, 5723.5, 5043.5, 3567, 1777, 1098, 995, 861.5, 711, 706,
631, 557, 537.5, 500, 388, 336.5, 313.14, 308, 291, 283, 281,
279, 278.5, 227, 209, 200, 175, 172, 156, 148, 143, 142, 130,
130, 108, 107, 83, 79, 78, 68, 59, 54, 54, 52, 50, 49, 48, 47,
44, 44, 38, 37, 36, 35, 29, 27, 26, 25, 25, 25, 23, 22, 21, 20,
19, 18, 17, 15, 13, 13, 11, 10, 10, 9.5, 9, 8, 7, 7, 6, 6, 6,
6, 6, 5, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0)), row.names = c(NA, -209L), class = c("tbl_df", "tbl", "data.frame"
))
Upvotes: 1
Views: 1089
Reputation: 5204
The gghighlight
package is built specifically for this use and can be very helpful. Check out the vignette here for more info.
In your case I think are trying to specify the top number of teams to include in the plot but it wasn't included in your question so I've added that below:
library(gghighlight)
top1 <- 10 # looks like you're calling input$top1 for this value but didn't share it
top_constructors %>%
slice_max(order_by = total_points, n = top1) %>%
ggplot(aes(
x = reorder(name, total_points),
y = total_points,
fill = name
)) +
geom_col() +
gghighlight::gghighlight(
total_points,
max_highlight = 3,
unhighlighted_params = list(fill = alpha("black", 0.4))
) +
scale_fill_manual(values = c("#FF0000", "#E69F00", "#56B4E9")) +
coord_flip() +
xlab("Constructor") +
ylab("Total points")
This produces the following plot:
A few notes:
"black"
so I put in the unhighlighted_params
argument so you can see how to control that but the alpha was just for example."red"
, so I modified the manual colors you provided.gghighlight
, you only need to provide enough colors for the highlighted bars to scale_fill_manual
. However if you provide fewer than the number of highlighted bars, it will throw an error.Upvotes: 0
Reputation: 8506
You could define named colors (or just a subset with specified colors) and use scale_fill_identity
:
library(ggplot2)
library(dplyr)
topN <- 30 # limit graph to the 30 teams with most points
t2020 <- c("Mercedes", "Red Bull", "McLaren", "Racing Point", "Renault", "Ferrari",
"AlphaTauri", "Alfa Romeo", "Haas F1 Team", "Williams") # 2020 teams
top_constructors <- mutate(top_constructors, active_in_2020 = ifelse(name %in% t2020, T, F))
rest <- filter(top_constructors, !active_in_2020)$name # non-2020 teams
teamcolors <- c(setNames(scales::hue_pal()(length(t2020)), t2020),
setNames(rep("black", length(rest)), rest)) # only color 2020 teams
teamcolors[c("Ferrari", "Mercedes")] <- c("red", "grey70") # change colors for some teams
arrange(top_constructors, desc(total_points)) %>%
slice_head(n=topN) %>%
mutate(name=factor(name, name),
clr=teamcolors[as.character(name)]) %>%
ggplot(aes(x=name, y=total_points, fill=clr)) +
geom_col()+
scale_x_discrete(limits=rev) +
coord_flip() +
labs(x="Constructor", y="Total points") +
scale_fill_identity()
Created on 2020-12-22 by the reprex package (v0.3.0)
Upvotes: 1