1

I'm very new to the R language so please correct me if anything is wrong! I need to learn!

I've recently created a graph in RStudio using the ggplot2 package.

enter image description here

Now I have 2 questions.

Firstly, I am hugely interested in the data points behind the contour lines. Presumably in order to the programme to draw these lines there must be a series of underlying data points that allows the program to do so.

I am looking to explore whether it is possible to extract these data points?

Secondly if anyone could enlighten me on the underlying mathematical principals behind the +geom_density_2d() function in ggplot2 then that would be great!

The code I have used to create the graph is below...

ggplot(read.Alteryx("Office", mode="data.frame"),aes(x=Chances,y=Defensive,colour=Team)) + 
  facet_wrap( ~ Team, ncol=2) + 
  geom_density_2d() + 
  scale_colour_manual(values = c("Leicester City" = "#1b378b", "Tottenham Hotspur" = "#011657", 
                                 "Arsenal" = "#d02320", "Manchester City" = "#8ac7ff")) + 
  theme_bw()  + 
  theme(panel.background=element_rect(fill="White"))  + 
  theme(plot.background=element_rect(fill="White"))  + 
  theme(panel.border=element_rect(colour="White"))  + 
  theme(panel.grid.major=element_line(colour="gray48",size=0.2))  + 
  scale_x_continuous(minor_breaks=0, breaks=seq(14,26,12),limits=c(14,26))  + 
  scale_y_continuous(minor_breaks=0, breaks=seq(50,100,50),limits=c(50,100))  + 
  theme(axis.text.x=element_text(size=7,colour="#535353",face="bold"))  + 
  theme(axis.text.y=element_text(size=7,colour="#535353",face="bold"))  + 
  theme(axis.ticks=element_blank())  + 
  theme(legend.position="none")  + 
  ggtitle("Pass completion % (Y) v Pass length M (X) for each game – EPL Top 4")  + 
  theme(plot.title=element_text(face="bold",colour="#3C3C3C",size=10))  + 
  ylab("")  + 
  xlab("") + 
  theme(axis.title.x=element_text(size=11,colour="#535353",face="bold",vjust=-.5))  + 
  geom_hline(yintercept=50,size=0.2,colour="gray48") + 
  geom_vline(xintercept=14,size=0.2,colour="gray48")  + 
  theme(strip.text.x = element_text(size=0, face="bold"),
        strip.background = element_rect(colour="White", fill="White"))  + 
  geom_point(size = 3)  + 
  theme(panel.margin = unit(3, "lines"))  + 
  geom_rug(sides="trbl", size = 0.5)
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • 2
    Did you look at the help pages for `geom_density_2d`? It names the function it calls to produce the estimates (`MASS::kde2d`). You can look at the help page for that function for the references. – MrFlick Feb 24 '16 at 23:13
  • 3
    In addition to `MrFlick`'s method, which allows you to directly calculate the density values, you can also grab them from the plot object using the `ggplot_build` function. For example, if your plot object is `p`, do `pb = ggplot_build(p)`. Then the data for the contour lines will be in one of the data frames in the `pb$data` sub-list. – eipi10 Feb 24 '16 at 23:16
  • I will have a look at these answers! Thank you. eipi10 your response seems to be exactly what i'm after, I just need to figure it out! – Benjamin Moss Feb 24 '16 at 23:20
  • 2
    Glad to be of help. Please come back and edit your question if you have any follow-up questions. Also, if you do have any follow-up questions, it will be easier to help if you provide sample data to go with your code (and more generally, if you provide a [minimal reproducible example](http://stackoverflow.com/a/5965451/496488)). – eipi10 Feb 24 '16 at 23:33
  • I just tried the ggplot_build method and it works perfectly! Thanks for the response and also for the tips :) – Benjamin Moss Feb 28 '16 at 21:04

0 Answers0