Created
March 26, 2012 09:09
-
-
Save andreuvall/2204071 to your computer and use it in GitHub Desktop.
Dubte de ggplot2: mapa de color + corbes de nivell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggplot2) | |
| # Per generar unes dades de prova... | |
| f <- function(x,y){ | |
| return(x^2 + y^2) | |
| } | |
| g <- expand.grid(x = seq(0,5), y = seq(0,5)) | |
| # Data frame en què totes les variables són numèriques | |
| A <- data.frame(x = g$x,y = g$y,z = f(g$x,g$y)) | |
| pA <- ggplot(A, aes(x,y, z = z)) | |
| # En aquest cas em funciona el geom_tile + stat_contour, | |
| # tot i que els tiles queden centrats a (x,y) i les corbes no | |
| pA + geom_tile(aes(fill = z)) # ok | |
| pA + stat_contour() # ok | |
| pA + geom_tile(aes(fill = z)) + stat_contour() # ok | |
| # Ara necessito ordenar les y decreixentment, | |
| # la manera que se m'acudeix és amb la funció ?ordered, | |
| # que s'ha de fer servir sobre factors | |
| B <- A | |
| B$y <- factor(B$y) | |
| B$y <- ordered(B$y, levels = rev(levels(B$y))) | |
| pB <- ggplot(B, aes(x,y, z = z)) | |
| # Ara funciona el geom_tile però no stat_contour | |
| pB + geom_tile(aes(fill = z)) # ok | |
| pB + stat_contour() # fail | |
| pB + geom_tile(aes(fill = z)) + stat_contour() # fail |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment