I'm currently trying to make a basic heatmap of exit velocities (ExitSpeed) based on pitch location from some baseball game data. The resulting plot has the fill legend, but the plot area is completely blank, with the correct x and y labels. I can use geom_point to plot each individual pitch, but cannot get geom_tile to show anything. The data is coming from a saved csv, so the error messages in the reprex aren't occurring locally. My code follows:
library(readr)
library(ggplot2)
library(tidyverse)
library(reprex)
PennState <- read_csv("Baseball/PennState.csv")
#> Error: 'Baseball/PennState.csv' does not exist in current working directory ('/private/var/folders/jb/1ybpmzm91l74pccjfmt27f1w0000gq/T/RtmpbhBJd4/reprex1cf30d1e1').
WithEV <- PennState %>% filter(ExitSpeed > 0)
#> Error in eval(lhs, parent, parent): object 'PennState' not found
ggplot(WithEV, aes(x=PlateLocSide, y=PlateLocHeight)) +
geom_tile(color = "white", aes(fill = ExitSpeed)) +
scale_fill_gradient(low = "blue", high = "red")
#> Error in ggplot(WithEV, aes(x = PlateLocSide, y = PlateLocHeight)): object 'WithEV' not found
I used reprex() to copy the code, is that not how I get the reprex? Also, the object is read off of a csv on my computer, which is why that error message is showing up. I'm not getting an error when I try and create the plot, it's just showing up empty
You could get the ReprEx to read the file by writing out the entire absolute path to the file but since we still would not have the file, that is not the best approach. Try making a small data frame with the data.frame() function with the three columns PlateLocSide, PlateLocHeight and ExitSpeed. Use that in the plot in your ReprEx so others can get exactly the result that you get.
You also need to put some data in the data frame. I did this in the example below, though I do not know what your data look like and I just invented something. As you can see, I got a plot. Try making data that are similar to your data and see what happens. You can post the result here if you get stuck.
Ok, so trying my own random data does fill the plot, but even creating the data frame with just those three variables from the spreadsheet returns an empty plot ('GameData' is a 3-column df in my environment, dk why it was empty for you). Any reason why pulling from the csv would be causing these problems?
#> Error: 'Baseball/PennState.csv' does not exist in current working directory ('/private/var/folders/jb/1ybpmzm91l74pccjfmt27f1w0000gq/T/RtmpbhBJd4/reprex1cf5b539768').
You should have your working directory under your home folder and Baseball should be a subdirectory in your working directory. Then PennState.csv should be in that subdirectory.
Even with the current working directory, I have no problem reading in the csv and running any other operations. The only thing that's giving me trouble is filling this heatmap.
I think the issue was N/A values in the 'ExitSpeed' variable. When using it to map size or color on a geom_point, for example, the N/A values were automatically excluded without interfering with the production of the plot. Not so for geom_tile, it appears. Thanks for the help!