0
votes

All I want is this R code to display the names of players inside the "topName" while hiding the names inside the "otherNames" by plotting both of them using two different geom_col().

epldata <- read.csv(file = 'epldata.csv') epldata$srno <- c(1:461) attach(epldata) points <- epldata[order(-fpl_points),] detach(epldata)

topNames[24:461]<-NA epldata$topNames <- topNames topPoints[24:461]<-NA epldata$topPoints <- topPoints

epldata$otherNames <- NA epldata$otherNames[24:461] <- as.character(points$name[c(24:461)]) epldata$otherPoints <- NA epldata$otherPoints[24:461] <- as.numeric(points$fpl_points[c(24:461)])

ggplot(data = epldata)+ geom_col(aes(x=epldata$topNames, y=epldata$topPoints), fill = "red", alpha = 1) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
annotate("text", x=epldata$topNames, y=-50, #epldata$topPoints, label = epldata$topNames, fontface = 1, size = 2, hjust = 0)+ geom_col(aes(x=epldata$otherNames, y=epldata$otherPoints), fill = "gray", alpha = 0.3)+ theme(legend.position = "none")+ #theme(axis.text.x = element_text(angle = 90, hjust = 1))+ xlab("Player Names")+ ylab("FPL Points")+ guides(fill=FALSE, color=FALSE, guide = FALSE) + coord_flip() + theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())

My output image here

This is the kind of output I am looking for but without using the Annotate Hack that I am currently using but directly plotting the names on the axis.

Update : have added the entire code and the link to the data set is below : https://drive.google.com/open?id=1KTitWDcLIBmeBsz8mLcHXDIyhQLZnlhS

1
Also the data with fill = "green" is actually showing as red on the plot and vice versaRushi Ladani
Please provide some or all of epldata. Also I suspect that you data are "wide" rather than "long". ggplot2 works better with one column for the labels (otherPoints, topPoints) and another for their values. And you should not be using $ inside aes(), just the column name.neilfws
use colour = I("green") to get greenRichard Telford

1 Answers

0
votes

Once you've created a list of topNames, you can use scale_x_continuous to display only these axis labels:

scale_x_discrete(breaks = topNames)

Also, rather than using two separate geom_col() geometries, you can create a new "highlight" column in the dataframe and use that with the fill and alpha aesthetics:

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())

Created on 2018-09-19 by the reprex package (v0.2.1)

including only some axis labels