geom_tile empty plot

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

Created on 2019-12-25 by the reprex package (v0.3.0)

Usually, a reproducible example, called a reprex is useful to answer questions like this. (This isn't because it doesn't have representative data.)

Good news! It's all in the error message

Error in eval(lhs, parent, parent): object 'PennState' 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.

will do! does this work?:

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/reprex1cf5b539768').
PennState %>%
  select(PlateLocHeight, PlateLocSide, ExitSpeed) %>%
data.frame() -> GameData
ggplot(GameData, aes(x=PlateLocSide, y=PlateLocHeight)) +
  geom_tile(color = "white", aes(fill = ExitSpeed)) +
  scale_fill_gradient(low = "blue", high = "red")

Created on 2019-12-25 by the reprex package (v0.3.0)

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.

library(ggplot2)
data.frame(PlateLocSide = c("IN", "IN", "OUT", "OUT"), 
           PlateLocHeight = c(1,2,1,2),
           ExitSpeed = c(90, 80, 100, 110)) -> GameData
ggplot(GameData, aes(x=PlateLocSide, y=PlateLocHeight)) +
  geom_tile(color = "white", aes(fill = ExitSpeed)) +
  scale_fill_gradient(low = "blue", high = "red")

Created on 2019-12-25 by the reprex package (v0.2.1)

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.

The

PennState <- read_csv("Baseball/PennState.csv")

should work.

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.

OK, the problem is in the data, since manually constructed data work correctly. Try running the code

PennState <- read_csv("Baseball/PennState.csv")
str(PennState)
summary(PennState)

That will reveal a lot of information about the content of the data frame and may explain why the plot does not behave as you expect.

1 Like

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!

Great! Please mark the solution for others to come!

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