Search code examples
rggplot2mediangeom-text

How to set the label for the point that bigger than their median in r?


I am new to R and I have problems with setting my labels that their coordinates are bigger than their median. Here is my dataframe:

dat <- data.frame(
  time = factor(c("Breakfast","Breakfast","Breakfast","Lunch","Lunch","Lunch","Dinner","Dinner","Dinner","Snack","Snack","Snack","Snack"), levels=c("Breakfast","Lunch","Dinner","Snack")),
  total_bill_x = c(12.75,14.89,20.5,17.23,30.3,27.8,20.7,32.3,25.4,14.5,13.7,14.2,15.7), total_bill_y= c(20.75,15.29,18.52,19.23,27.3,23.6,19.75,27.3,21.48,13.66,15.59,17.3,14.78)
)

Here is my code:

library (dplyr)
library(ggplot2)

c<-dat %>%
  group_by(time) %>%
  summarise(
    x = sum(total_bill_x),
    y = sum(total_bill_y) 
  )
#visualiser  
ggplot(c,aes(x,y))+
  geom_point()+
  geom_vline(linetype="dashed",color="red",xintercept = median(c$x))+
  geom_hline(linetype="dashed",color="red",yintercept = median(c$y))+
  geom_text(aes(label=time),hjust=1, vjust=1.2)

In this case, Label that I want to display are only Lunch and Dinner. Which condition should I add to achieve this?

Any help would be much appreciated.


Solution

  • Try this:

    
    library (dplyr)
    library(ggplot2)
    
    dat <- data.frame(
      time = factor(c("Breakfast","Breakfast","Breakfast","Lunch","Lunch","Lunch","Dinner","Dinner","Dinner","Snack","Snack","Snack","Snack"), levels=c("Breakfast","Lunch","Dinner","Snack")),
      total_bill_x = c(12.75,14.89,20.5,17.23,30.3,27.8,20.7,32.3,25.4,14.5,13.7,14.2,15.7), total_bill_y= c(20.75,15.29,18.52,19.23,27.3,23.6,19.75,27.3,21.48,13.66,15.59,17.3,14.78)
    )
    
    
    c<-dat %>%
      group_by(time) %>%
      summarise(x = sum(total_bill_x),
                y = sum(total_bill_y)) %>% 
      mutate(med_x = median(x),
             med_y = median(y),
             lab = case_when(x > med_x & y > med_y ~ as.character(time),
                             TRUE ~ NA_character_))
    
    #visualiser  
    ggplot(c,aes(x,y))+
      geom_point()+
      geom_vline(linetype="dashed",color="red",xintercept = median(c$x))+
      geom_hline(linetype="dashed",color="red",yintercept = median(c$y))+
      geom_text(aes(label=lab),hjust=1, vjust=1.2)
    
    
    
    

    Which gives you:

    enter image description here