I am learning R and I have problems with how to find the median and draw the coordinate depend on the median of each category of a variable. Suppose I have a dataframe as below:
dat <- data.frame(
time = factor(c("Breakfast","Breakfast","Breakfast","Lunch","Lunch","Lunch","Dinner","Dinner","Dinner"), levels=c("Breakfast","Lunch","Dinner")),
total_bill_x = c(12.75,14.89,20.5,17.23,30.3,27.8,20.7,32.3,25.4), total_bill_y= c(20.75,15.29,18.52,19.23,27.3,23.6,19.75,27.3,21.48)
)
I want to draw the points in coordinate (xy) correspond to Breakfast, Lunch, Dinner with x is the median of total_bill_x of each category and y is the median of total_bill_y of each category. For example for coordinate of breakfast I want x=median(12.75,14.89,20.5) and y= median(20.75,15.29,18.52). We do the same for Lunch and Dinner then draw them in the coordinate xy.
Any help for this would be much appreciated.
library (dplyr)
library(ggplot2)
dat %>%
group_by(time) %>% # group your data
summarise(
x = median(total_bill_x),
y = median(total_bill_y) # compute median
)%>%
ggplot(.,aes(x,y, col = time)) + #plot x,y
geom_point()