r - 具有条件段着色的 R 棒棒糖
问题描述
我有一个 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 的数据都应该有黄线
解决方案
推荐阅读
- jquery - 带有有效数据的数据表警告
- sql - 如何根据列的值获取sql server中的行
- python - 如何从大文本文件中的随机位置进行 python 正则表达式搜索
- android - 一起使用 firestore 和 firebase auth/core 的问题
- c# - 如何在没有 Excel 的情况下使用 ExcelDNA 从 C# 开发的 XLL
- bigrquery - R 无法通过 bigrquery 包导入数据 - !self$finished 不是 TRUE
- angular - ngFor 不显示来自可观察事件源的数据
- excel-formula - 特定日历月的所有对应值的总和以及截至上个月的所有对应值的总和?
- r - 在 R 中总结组均值时如何创建有条件的新组
- wordpress - 如何知道wordpress中安装了哪些插件