我有一个csv文件,我将其缩小为3列并放置在数据框中。数据是该日期的日期,数字和十六进制颜色代码。我想制作一个水平的棒棒糖图表,并一直关注
*编辑以在代码部分添加示例数据
https://www.r-graph-gallery.com/301-custom-lollipop-chart/
一切正常,直到我想通过调用颜色列设置分段颜色。然后,它失去了颜色的排序。
我不太了解mutate函数,因此我猜想它正在发生。如果有人可以解释那里发生的事情,那也将不胜感激。
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")
任何具有3PM> 3的数据都应带有黄线