3

I'm trying to use ggplot to create a scatter plot of player data with a pop up that includes the players name. The data is from here. My code is below.

x <- Player_Stats %>%
  ggplot(aes(x=PTS, y=`ThreeP%`,color=Tm, text=paste("Player:", Player)))+
  geom_point()

ggplotly(x, tooltip = "text")

When I run the code I receive an error that says Error in gsub("\n", br(), a, fixed = TRUE) : input string 16 is invalid in this locale.

My current popup just includes PTS, 3p% and Team. How can I add the player name to the popup?

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Tamir
  • 43
  • 4

1 Answers1

1

I'm not sure what caused your error, but I went to your data source link and selected the "Get table as CSV (for Excel)" option from "Share & Export". I highlighted all the resulting table text, and copied to excel. I then did "Text to columns" and split the data by the comma delimiters. I then saved the file as a .csv extension. I then ran your code. It didn't work initially because the ThreeP% variable was named 3P% in the .csv file. I renamed it, and then your code worked fined thereafter, producing your desired output. Maybe your code is fine, but the input data, isn't working somehow? Hard to say without precisely reproducing your input.

library(tidyverse)
library(plotly)

data <- read_csv("player_stats.csv")

colnames(data)
colnames(data)[14] <- "ThreeP%"

x <- data %>%
  ggplot(aes(x=PTS, y=`ThreeP%`, color=Tm, text=paste("Player:", Player)))+
  geom_point()

ggplotly(x, tooltip = "text")

Output

Roasty247
  • 679
  • 5
  • 20
  • Thanks for taking the time to work through this. Glad to know I at least coded it correctly because I've been banging my head against the wall trying to figure out what is wrong with my code. It seems to be an issue with the text=paste("Player:",Player) because when I remove that argument it works for me. Any ideas as to why it might be rejecting that? Maybe I'm missing a package or something. – Tamir Jul 01 '22 at 22:42
  • Update I got it to work by update the system local to C using Sys.setlocale(locale="C"). Thanks again for your help. – Tamir Jul 01 '22 at 22:58