I have a csv file that I reduce to 3 columns and place in a data frame. The data is a date, a number, and a hex color code in that order. I would like to make a horizontal lollipop chart and have been following
*Edit to add sample data in the code section
https://www.r-graph-gallery.com/301-custom-lollipop-chart/
Everything works fine until I want set the segment colors by calling the color column. Then it loses the sorting for the color.
I don't quite grasp the mutate function so I'm guessing it's happening there. If someone could explain what's happening there, that would also be appreciated.
library(tidyverse)
dir <- 'C:[file directory]'
setwd(dir)
file <- "KK3s.csv"
mydata <- read.csv(file, header=T, as.is=T)
#sample data
Match UP ,W/L,MIN,PTS,FGM,FGA,FG%,3PM,3PA,3P%,FTM,FTA,FT%,OREB,DREB,REB,AST,STL,BLK,TOV,PF,+/-
28-Feb-19,W,22,22,8,15,53.3,6,10,60,0,0,0,0,3,3,2,1,0,2,2,9
27-Feb-19,W,12,0,0,2,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,-6
23-Feb-19,W,15,7,1,7,14.3,1,6,16.7,4,4,100,0,4,4,1,0,0,1,1,1
22-Feb-19,L,11,6,2,5,40,2,4,50,0,0,0,0,2,2,1,0,1,2,0,1
12-Feb-19,L,19,5,1,3,33.3,0,2,0,3,3,100,0,4,4,3,1,0,2,1,0
9-Feb-19,W,22,5,2,4,50,1,3,33.3,0,0,0,0,3,3,0,1,0,0,1,5
6-Feb-19,W,15,3,1,5,20,1,4,25,0,1,0,0,0,0,0,0,0,1,0,2
2-Feb-19,L,17,8,3,6,50,1,3,33.3,1,3,33.3,0,2,2,1,0,0,2,1,3
#add color column with a baseline color
mydata$color <- rgb(249, 161, 26, maxColorValue=255)
#change color based on W or L
mydata$color[mydata$W.L<"W"] = rgb(43, 81, 52, maxColorValue=255)
#Reduce df to only necessary columns
data <- data.frame(x=mydata$Match.Up, y=mydata$X3PM, WLcolor = mydata$color)
data %>%
arrange(y) %>%
mutate(x=factor(x,x)) %>%
ggplot( aes(x=x, y=y)) +
geom_segment( aes(x=x, xend=x, y=0, yend=y),
color = data$WLcolor,
size=1) +
geom_point( color="blue", size=4, alpha=0.6) +
theme_light() +
coord_flip() +
theme(
panel.grid.major.y = element_blank(),
panel.border = element_blank(),
axis.ticks.y = element_blank()
) +
xlab("Date") +
ylab("3PM")
Any data that has 3PM > 3 should have yellow lines