我想要的是这个R代码,以显示“ topName”中玩家的姓名,同时通过使用两个不同的geom_col()绘制两个名字来隐藏“ otherNames”中的玩家的名字。
epldata <-read.csv(文件='epldata.csv') epldata $ srno <-c(1:461) 附加(epldata) 点<-epldata [order(-fpl_points),] detach(epldata)
topNames [24:461] <-NA epldata $ topNames <-topNames topPoints [24:461] <-NA epldata $ topPoints <-topPoints
epldata $ otherNames <-不适用epldata $ otherNames [24:461] <- as.character(points $ name [c(24:461)])epldata $ otherPoints <-不适用 epldata $ otherPoints [24:461] <- as.numeric(points $ fpl_points [c(24:461)])
ggplot(data = epldata)+ geom_col(aes(x = epldata $ topNames, y = epldata $ topPoints),填充=“ red”,alpha = 1)+
主题(axis.text.x = element_text(angle = 90,hjust = 1))+
注释(“文本”,x = epldata $ topNames,y = -50,#epldata $ topPoints, 标签= epldata $ topNames,字体= 1,大小= 2,高度= 0)+ geom_col(aes(x = epldata $ otherNames,y = epldata $ otherPoints),填充 =“灰色”,alpha = 0.3)+主题(legend.position =“无”)+ #theme(axis.text.x = element_text(角度= 90,正好= 1))+ xlab(“播放器名称”)+ ylab(“ FPL点”)+指南(填充= FALSE, color = FALSE,guide = FALSE)+ coord_flip()+ theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())
This is the kind of output I am looking for,但不使用我当前正在使用的Annotate Hack,而是直接在轴上绘制名称。
更新:已添加完整代码,并且指向数据集的链接如下: https://drive.google.com/open?id=1KTitWDcLIBmeBsz8mLcHXDIyhQLZnlhS
答案 0 :(得分:0)
创建了topNames列表后,可以使用scale_x_continuous
仅显示以下轴标签:
scale_x_discrete(breaks = topNames)
此外,您可以在数据框中创建一个新的“突出显示”列,而不是使用两个单独的geom_col()
几何形状,并将其与填充和alpha美学一起使用:
library(dplyr)
library(ggplot2)
# read data from google drive
id <- "1KTitWDcLIBmeBsz8mLcHXDIyhQLZnlhS" #google file ID
epldata <- read.csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id),
stringsAsFactors = FALSE)
N <- 24 #number of players to highlight
#get list of names of top N players
topNames <- epldata %>%
arrange(-fpl_points) %>%
head(N) %>%
pull(name)
#> Warning: package 'bindrcpp' was built under R version 3.5.1
# make variable for highlighting
epldata <- epldata %>%
mutate(highlight = ifelse(name %in% topNames, TRUE, FALSE))
ggplot(data = epldata,
aes(x = name, y = fpl_points, fill = highlight, alpha = highlight)) +
geom_col() +
scale_fill_manual(guide = FALSE,
values = c("gray", "red")) +
scale_alpha_manual(guide = FALSE,
values = c(0.4, 1)) +
scale_x_discrete(breaks = topNames) + #use breaks to determine axis labels
coord_flip() +
ylab("FPL Points") +
theme_classic() +
theme(axis.ticks.y = element_blank(),
axis.title.y = element_blank())
由reprex package(v0.2.1)于2018-09-19创建