hermesh2
11/22/2015 - 1:13 AM

03 Solution2.R

# Solution 2
# S plot density with Palette ---------------------------------------------
colfunc <- colorRampPalette(c("darkblue", "lightblue", "green", "yellow", "red")) # To create scale for representing the heatmap
p <- 
  ggplot(d, aes(x = CURRENT_FIX_X, y =CURRENT_FIX_Y)) +
  annotation_custom(grob = image_1, # Image
                    xmin= 0, # Coordinates to represent the image
                    xmax= 1000,
                    ymin= 0,
                    ymax= 800 
                    # The image has 700 pixel. But the matrix has the median in 400 and if a put the image with 700px the eyes are in 350 (coord y).
                    # The I changed the size to 800 px in order to adjust the eyes position to the data position
  ) +
  coord_fixed(xlim = c(0, 1000), ylim = c(0, 700))  + # fix the image in the coordinates that we deffined before
  geom_point(data= d, aes(x = CURRENT_FIX_X, y =CURRENT_FIX_Y),  # coordinates       
             color="black", # Color point
             # position=position_jitter(w=0.01,h=0.01), # Point plot desviation
             alpha=0.5) + # Point transaparecen
  
  stat_density2d(data= d, aes(x = CURRENT_FIX_X, y =CURRENT_FIX_Y , fill = ..level.., alpha = ..level..),
                 size= 100, bins= 50, geom='polygon') +
  theme_bw() + # Kind of theme. I strongly recomend theme_bw
  scale_fill_gradientn(colours=colfunc(100)) +  # Create a color for filling the heatmap
  guides(fill = guide_colorbar(barwidth = 0.5, barheight = 10)) +
  theme(legend.title=element_blank()) +
  scale_alpha_continuous(range=c(0.0, 1) , guide = FALSE) + # You can play with the range to show a better image. Range belongs to [0, 1] interval
  xlim(0, 1000) + # Control lim for x-axe
  ylim(0, 700) # Control lim for y-axe
print(p)
jpeg(filename = "Results/Solution2.jpeg", width = 1000, height = 700, units = "px")
print(p)
dev.off()
# E plot density with Palette ---------------------------------------------