ggplot2 geom_smooth

Hi,
This is the code I am using with ggplot2 and for some reason nothing is showing up with geom_smooth. Any ideas why this isn't working? There is no error either.

ggplot() +
geom_point(data=fourseamers1, aes(x=spin_rate, y=swstrike), color= "light green")+
ylim(0,.2) + xlab("Average Spin Rate") +ylab("Swinging Strike Percentage") + ggtitle("4-Seam Fastball")+
theme(plot.title=element_text(hjust =.5)) + geom_smooth(method = 'lm')

Rplot01

I think this is happening because you are just passing data to geom_point() and not globally although I can't be sure since you are not providing a reproducible example.

Try with this

ggplot(data=fourseamers1, aes(x=spin_rate, y=swstrike)) +
    geom_point(color= "light green") +
    ylim(0,.2) +
    xlab("Average Spin Rate") +
    ylab("Swinging Strike Percentage") +
    ggtitle("4-Seam Fastball")+
    theme(plot.title = element_text(hjust =.5)) +
    geom_smooth(method = 'lm')

If this doesn't work, please provide a minimal REPRoducible EXample (reprex). A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

That works, thank you!

I cannot see a strong relationship on my plot. I was wondering if there was a way I could split up the plot by velocity ranges. Then I could see spin rate compared to swinging strike percentage by the ranges. I am assuming I would use facet_wrap at the end of this, but I am not sure what to do up to that point. Do you have any ideas?
Thank you.

I'm pretty sure there is more than one, but we can't give you an example if you don't provide a reproducible example including some sample data on a copy/paste friendly format.

Here is my reproducible example. I appreciate it!

#library(ggplot2)

fourseamers1<- data.frame(
spin_rate = c(2580, 2210, 2275, 2333, 2494, 2274, 2187, 2351,
2268, 2345, 1980, 2266, 2192, 2398, 2267),
velocity = c(95.1, 94.3, 96.2, 97.6, 94.2, 95.4, 93.3, 89.3, 91.3, 92.4,
91.1, 93.2, 95.2, 95.1, 94.6),
swstrike = c(0.129782082324455, 0.0798982188295165, 0.121307353865493,
0.108133669609079, 0.138432364096081, 0.127034790935206,
0.103564223268325, 0.0868979454361738, 0.126290433585685,
0.122392211404729, 0.0737647877522617, 0.0782516743038421,
0.0872363246335359, 0.0817587209302326, 0.10285505124451),
player_name = as.factor(c("Justin Verlander", "Kevin Gausman",
"Gerrit Cole", "Luis Severino", "Max Scherzer",
"James Paxton", "Sean Newcomb", "Marco Estrada",
"Jake Odorizzi", "J.A. Happ", "Sean Manaea",
"Mike Clevinger", "German Marquez", "Blake Snell",
"Nick Pivetta"))
)

#my code now
ggplot(data=fourseamers1, aes(x=spin_rate, y=swstrike)) +
geom_point(color= "dodger blue") +
ylim(0,.2) +
xlab("Average Spin Rate") +
ylab("Swinging Strike Percentage") +
ggtitle("4-Seam Fastball")+
theme(plot.title = element_text(hjust =.5)) +
geom_smooth(method = 'lm', color= 'red')

Is this what you want to do? (I can´t tell if this is useful since there are too few points in the sample data and the plot looks odd)

library(ggplot2)
library(dplyr)

fourseamers1<- data.frame(
    spin_rate = c(2580, 2210, 2275, 2333, 2494, 2274, 2187, 2351,
                  2268, 2345, 1980, 2266, 2192, 2398, 2267),
    velocity = c(95.1, 94.3, 96.2, 97.6, 94.2, 95.4, 93.3, 89.3, 91.3, 92.4,
                 91.1, 93.2, 95.2, 95.1, 94.6),
    swstrike = c(0.129782082324455, 0.0798982188295165, 0.121307353865493,
                 0.108133669609079, 0.138432364096081, 0.127034790935206,
                 0.103564223268325, 0.0868979454361738, 0.126290433585685,
                 0.122392211404729, 0.0737647877522617, 0.0782516743038421,
                 0.0872363246335359, 0.0817587209302326, 0.10285505124451),
    player_name = as.factor(c("Justin Verlander", "Kevin Gausman",
                              "Gerrit Cole", "Luis Severino", "Max Scherzer",
                              "James Paxton", "Sean Newcomb", "Marco Estrada",
                              "Jake Odorizzi", "J.A. Happ", "Sean Manaea",
                              "Mike Clevinger", "German Marquez", "Blake Snell",
                              "Nick Pivetta")))
fourseamers1 %>%
    mutate(velocity_range = case_when(velocity <= 92 ~ "<80-92]",
                                velocity > 92 & velocity <= 94 ~ "<92-94]",
                                velocity > 94 ~ "<94-100]")) %>% 
    ggplot(aes(x = spin_rate, y = swstrike, colour = velocity_range)) +
    geom_point() +
    geom_smooth(method = 'lm') +
    scale_y_continuous(limits = c(0, 0.2)) +
    labs(title = "4-Seam Fastball",
         x = "Average Spin Rate",
         y = "Swinging Strike Percentage",
         colour = "Velocity Range") +
    theme(plot.title = element_text(hjust =.5)) +
    NULL

Created on 2019-06-09 by the reprex package (v0.3.0)

Yes. Thank you for your help.

Actually, is there a way you could put each range on its own graph?

Are you looking for facets?

library(ggplot2)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang

fourseamers1 <- data.frame(spin_rate = c(2580, 2210, 2275, 2333, 2494, 2274, 2187, 2351, 2268, 2345, 1980, 2266, 2192, 2398, 2267),
                           velocity = c(95.1, 94.3, 96.2, 97.6, 94.2, 95.4, 93.3, 89.3, 91.3, 92.4, 91.1, 93.2, 95.2, 95.1, 94.6),
                           swstrike = c(0.129782082324455, 0.0798982188295165, 0.121307353865493, 0.108133669609079, 0.138432364096081, 0.127034790935206, 0.103564223268325, 0.0868979454361738, 0.126290433585685, 0.122392211404729, 0.0737647877522617, 0.0782516743038421, 0.0872363246335359, 0.0817587209302326, 0.10285505124451),
                           player_name = c("Justin Verlander", "Kevin Gausman", "Gerrit Cole", "Luis Severino", "Max Scherzer", "James Paxton", "Sean Newcomb", "Marco Estrada", "Jake Odorizzi", "J.A. Happ", "Sean Manaea", "Mike Clevinger", "German Marquez", "Blake Snell", "Nick Pivetta"))

ggplot(data = within(data = fourseamers1,
                     expr = {velocity_range <- cut(x = velocity,
                                                   breaks = quantile(x = velocity),
                                                   include.lowest = TRUE)}),
       mapping = aes(x = spin_rate,
                     y = swstrike,
                     colour = velocity_range)) +
  geom_point() +
  geom_smooth(method = "lm") +
  scale_y_continuous(limits = c(0, 0.2)) +
  labs(title = "4-Seam Fastball",
       x = "Average Spin Rate",
       y = "Swinging Strike Percentage",
       colour = "Velocity Range") +
  theme(plot.title = element_text(hjust = 0.5),
        strip.text = element_blank()) +
  facet_wrap(facets = ~ velocity_range,
             nrow = 2)

That looks great, thank you.

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