How to change the colors on of my ggplot

I want to change the colors of my plot points to red and blue to represent the wins from the losses

>library(readxl)
Lakers_Overall_Stats <- read_excel("Desktop/Lakers Overall Stats.xlsx")
library(readxl)
Lakers_Record <- read_excel("Desktop/Lakers Record.xlsx")
require(dplyr)
require(ggplot2)

##WinPercentage of the Team after season
mydata <- Lakers_Record %>% select(Pts,Opp,W,L)%>%
  + mutate(wpct=Pts^13.91/(Pts^13.91+Opp^13.91),expwin=round(wpct*(W+L)),diff=W-expwin)
head(mydata)


##Plot of the teams wins
ggplot(mydata,aes(expwin,W)) + geom_point() + stat_smooth(method = "lm")
mod <- lm(W~expwin,data = mydata)
mod
summary(mod)

You just need to define your values with scale_colour_manual():

1 Like

Here is a reprex of when I tried to use the scale_colour_manual() function:


#Plot of the team wins
ggplot(mydata,aes(expwin,W)) + geom_point() + stat_smooth(method = "lm") +
  scale_colour_manual(values = colors)
#> Error in ggplot(mydata, aes(expwin, W)): could not find function "ggplot"
colors <- c("W" = "Green", "expwin" = "blue")
mod <- lm(W~expwin,data = mydata)
#> Error in is.data.frame(data): object 'mydata' not found
mod
#> Error in eval(expr, envir, enclos): object 'mod' not found
summary(mod)
#> Error in summary(mod): object 'mod' not found

Created on 2019-11-27 by the reprex package (v0.3.0)

I was unsuccessful. Am I missing something?

That is not a reprex, a reprex has to be reproducible and your code is not since it's missing library calls and most importantly sample data, please try to provide a proper reproducible example as explained in this guide

The error messages are telling you what's wrong.

You haven't loaded the required packages nor mydata.

library(readxl)
Lakers_Team_Stats <- read_excel("Desktop/Lakers Team Stats.xlsx")
#> Error: path does not exist: 'Desktop/Lakers Team Stats.xlsx'
library(readxl)
Lakers_Record <- read_excel("Desktop/Lakers Record.xlsx")
#> Error: path does not exist: 'Desktop/Lakers Record.xlsx'
library(reprex)
require(dplyr)
#> Loading required package: dplyr
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
require(ggplot2)
#> Loading required package: ggplot2
require(tidyverse)
#> Loading required package: tidyverse

#WinPercentage of the Team after season
mydata <- Lakers_Record %>% select(Pts,Opp,W,L)%>%

  • mutate(wpct=Pts^13.91/(Pts^13.91+Opp^13.91),expwin=round(wpct*(W+L)),diff=W-expwin)
    #> Error in eval(lhs, parent, parent): object 'Lakers_Record' not found
    head(mydata)
    #> Error in head(mydata): object 'mydata' not found

#Usage Percentage
Usgpct <- Lakers_Overall_Stats %>% select(FGA,FTA,TOV,MP,TmFga,TmFta,TmTov,TmMin)%>%
filter(rank(desc(Usgpct))==1)%>%
mutate(100*(Fga+0.44*Fta+Tov))TmMin/(TmFga+0.44TmFta+TmTov)*5(MP)
#> Error in eval(lhs, parent, parent): object 'Lakers_Overall_Stats' not found
head(Usgpct)
#> Error in head(Usgpct): object 'Usgpct' not found

#Specifiying
TmMin <- Lakers_Overall_Stats[23,6]
#> Error in eval(expr, envir, enclos): object 'Lakers_Overall_Stats' not found
TmFga <- Lakers_Overall_Stats[23,8]
#> Error in eval(expr, envir, enclos): object 'Lakers_Overall_Stats' not found
TmFta <- Lakers_Overall_Stats[23,18]
#> Error in eval(expr, envir, enclos): object 'Lakers_Overall_Stats' not found
TmTov <- Lakers_Overall_Stats[23,26]
#> Error in eval(expr, envir, enclos): object 'Lakers_Overall_Stats' not found

#Plot of the team wins
ggplot(mydata,aes(expwin,W)) + geom_point() + stat_smooth(method = "lm") +
scale_colour_manual(values = colors)
#> Error in ggplot(mydata, aes(expwin, W)): object 'mydata' not found
colors <- c("W" = "Green", "expwin" = "blue")
mod <- lm(W~expwin,data = mydata)
#> Error in is.data.frame(data): object 'mydata' not found
mod
#> Error in eval(expr, envir, enclos): object 'mod' not found
summary(mod)
#> Error in summary(mod): object 'mod' not found

Again, the errors are telling you what's wrong.

The two Excel files you are trying to read cannot be found. Check the correct path for the files.

I already have the code working for the project. The mydata portion isnt the issue. At this point I just need help getting the code to work for the scale_colour_manual.

Well it was the problem in the code you posted above.

If you want help on this please post a reproducible example (reprex) using the instructions in either of these links:

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.