0

I am very new to coding and know this probably a bad way of doing a lot of things here, especially the indexing, but specifically, I want to know how I can get the week 9 bar to be highlighted. Right now th current output is in the picture below.2

My data frame is also pictured below. 1

russ_graph<-ggplot(data=russ2020, aes(x=russ2020$week, y=russ2020$`round(mean(epa), 3)`)) + 
  geom_bar(stat="identity")+
  ggtitle("Let Russ Cook?")+
  annotate("text", x = 9, y = -0.25, label = "Russ Trademarks")+
  annotate("text", x = 9, y = -0.3, label = "\"Let Russ Cook\"")+
  theme(plot.title = element_text(hjust = 0.5))+
  xlab("Week") + ylab("EPA per Play")+
  scale_x_continuous(russ2020$week, labels = as.character(russ2020$week), breaks = russ2020$week)+
  geom_text(aes(label=russ2020$`round(mean(epa), 3)`), vjust=2, size=2.5)
stefan
  • 90,330
  • 6
  • 25
  • 51
  • 4
    Please provide parts of your data. `dput(data)` – TarJae Feb 20 '21 at 06:14
  • 1
    Images are not a good way for posting data (or code). See [this Meta](https://meta.stackoverflow.com/a/285557/8245406) and a [relevant xkcd](https://xkcd.com/2116/). Can you post sample data in `dput` format? Please edit **the question** with the code you've tried and with the output of `dput(russ2020)`. Also, by to highlight do you mean to have bar 9 displayed in a different color? If yes, see [this SO post](https://stackoverflow.com/questions/22894102/change-color-of-only-one-bar-in-ggplot). – Rui Barradas Feb 20 '21 at 07:28
  • You could add another `geom_bar()` layer, but with a different data frame (which contains only 1 line, the one with week 9), and give that layer a different color. `geom_bar(data = russ_w9, aes(round(mean(epa), 3)) ), color="red")`. By the way, you can omit the `russ2020` prefix , you can write just `week` instead of `russ2020$week`. – knb Feb 20 '21 at 09:28
  • the very first link when googling your verbatim question https://stackoverflow.com/q/45820250/7941188 – tjebo Feb 20 '21 at 10:59

1 Answers1

1

This could be achieved by conditionally mapping on the fill via e.g. ifelse and setting the desired fill colors via scale_fill_manual:

Note: I also renamed the clumsy column name for the mean to the more easily usable mean_epa.

library(ggplot2)
library(dplyr)

set.seed(42)
russ2020 <- tibble(
  week = 1:18,
  "round(mean(epa), 3)" = round(rnorm(18, mean = 0.5),3)
)
russ2020 <- rename(russ2020, mean_epa = `round(mean(epa), 3)`)

ggplot(data=russ2020, aes(x=week, y=mean_epa)) + 
  geom_bar(aes(fill = ifelse(week == 9, "high", "default")), stat="identity")+
  scale_fill_manual(values = c(high = "steelblue", default = "grey45")) +
  ggtitle("Let Russ Cook?")+
  annotate("text", x = 9, y = -0.25, label = "Russ Trademarks")+
  annotate("text", x = 9, y = -0.3, label = "\"Let Russ Cook\"")+
  theme(plot.title = element_text(hjust = 0.5))+
  xlab("Week") + ylab("EPA per Play")+
  scale_x_continuous(breaks = unique(russ2020$week))+
  geom_text(aes(label=mean_epa), vjust=2, size=2.5) +
  guides(fill = FALSE)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Wow, thank you. This was super helpful. My last concern is that the mean_epa data doesn't seem to reflect that of the initial data set. Is there a reason for this? – Mark Johnson Feb 21 '21 at 19:18