0

I need to highlight a single point of a geom_point graph from ggplot. Since it is an extensive dataset, I have sliced the rows that I needed to analyze with the following code, which gave me:

jpp1 <- my_data %>% slice(52:51)

The output:

Date   equity company  press    Categorization  Year Month Event
  <chr>   <dbl> <chr>    <chr>    <chr>          <dbl> <chr> <dbl>
1 05/18…   162. JPMorgan States … Negative        2021 May       1
2 05/19…   161. JPMorgan NA       NA                NA NA       NA

In order to analyze this information, I scattered a ggplot with the following code:

ggplot(data = jpp1) + geom_point(mapping = aes(equity, Date))

I need to highlight the row 52, which is date 05/18/21 in the y axis. I already tried with gghighlight but I could not manage to do it.

zork1
  • 69
  • 5
  • Would putting a flag on the row you want (i.e just add an extra column with a 1 in it), and then add that to a colour aesthetic do the trick for you? – Quixotic22 Aug 04 '21 at 18:19
  • `mapping = aes(equity, Date, color = replace(rep(0, NROW(jpg1)), 52, 1))` – dww Aug 04 '21 at 18:32

1 Answers1

0

With below example dataset df which is a part of your data and added fake rows we could:

  1. Data manipulation (Date should be Date I use lubridate package
  2. add a column highlight or normal depending which you want to highlight.
  3. define your colors my_colors
  4. geom_point with geom_point(size = 3, aes(colour = highlight))
  5. assign colors with scale_color_manual
library(tidyverse)
library(lubridate)

jpp1 <- df %>% 
    mutate(Date = mdy(Date)) %>% 
    mutate(highlight = ifelse(Date == "2021-05-18", "highlight", "normal"))
    
my_colors <- c("highlight" = "red", "normal" = "black")

ggplot(data = jpp1, aes(x = Date, y = equity)) +
    geom_point(size = 3, aes(colour = highlight)) +
    scale_color_manual("Status", values = my_colors) 

data:

df <- tribble(
~Date,   ~equity, ~company,  
"05/18/21",   162., "JPMorgan", 
"05/19/21",   161., "JPMorgan", 
"05/20/21",   160., "JPMorgan", 
"05/21/21",   159., "JPMorgan")

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66