• [^] # Re: R

    Posté par . En réponse au journal Faire des graphiques à partir de données : comparaison. Évalué à 3.

    Bonjour Sarcastic ,

    Toutes mes plates pour la période d'hibernation.

    Sans transition, d'abord, la fonction image (bibliothèque base) peut aider à obtenir des résultats semblables à ceux de Gnuplot. Ensuite, pour des résultats améliorés, on peut voir du côté de la fonction image.plot (bibliothèque fields) ; dans les deux cas, la bibliothèque akima peut accomplir l'interpolation. Puis, si on veut ajouter de la perspective à la visualisation, la bibliothèque misc3d pourra aider. Enfin, à mon avis, ggplot2 est inadéquat pour ce genre de traitement. Foin de verbiage, place au code.

    • Pre-processing et quelques définitions
    invisible(sapply(c("akima", "fields", "ggplot2", "gridExtra", "misc3d"), function(xxx) {
     library(xxx, character.only = TRUE)
    }))
    interpoCoord <- function(component) {
     return(seq(
     from = min(component),
     to = max(component),
     length.out = 2 * length(component)
     ))
    }
    utilFields <- function(interpolated, xText = "", yText = "", legText = "") {
     image.plot(interpolated, horizontal = TRUE, legend.lab = legText)
     title(xlab = xText, ylab = yText)
    }
    rphys1 <- read.table("rphys_a1_K_1_3.dat", sep = "", quote = "", skipNul = TRUE)
    rphys2 <- read.table("rphys_a2_K_2_3.dat", sep = "", quote = "", skipNul = TRUE)
    names(rphys1) <- names(rphys2) <- c("x", "y", "z")
    rphys1Smooth <- with(rphys1, interp(x = x, y = y, z = z , xo = interpoCoord(x), yo = interpoCoord(y)))
    rphys2Smooth <- with(rphys2, interp(x = x, y = y, z = z, xo = interpoCoord(x), yo = interpoCoord(y)))
    imWidthBase <- 800
    imHeightBase <- 600
    imWidthFields <- imWidthBase - 350
    imHeightFields <- imHeightBase - 100
    shapeChar <- 19
    shapeSize <- 3
    lowCol <- "#720808"
    highCol <- "#ffff00"
    oddMargins <- unit(c(.01, .01, .01, .2), "cm")
    evenMargins <- unit(c(.01, .2, .01, .01), "cm")
    • Bibliothèque base
    png("rphys1_base.png", width = imWidthBase, height = imHeightBase)
    image(rphys1Smooth, xlab = absTag, ylab = ordTag)
    dev.off()
    png("rphys2_base.png", width = imWidthBase, height = imHeightBase)
    image(rphys2Smooth, xlab = absTag, ylab = "")
    dev.off()

    Avec-base

    • Bibliothèque fields
    png("rphys1_fields.png", width = imWidthFields, height = imHeightFields)
    utilFields(rphys1Smooth)
    dev.off()
    png("rphys2_fields.png", width = imWidthFields, height = imHeightFields)
    utilFields(rphys2Smooth)
    dev.off()

    Avec-fields

    • Bibliothèque misc3d

    Warning : La projection des perspectives est un traitement très lourd pour ces données !

    sceneObj1 <- with(rphys1, surfaceTriangles(x, y, z, material = "shiny"))
    sceneObj2 <- with(rphys2, surfaceTriangles(x, y, z, material = "shiny"))
    exportScene(sceneObj1, filename = "rphys1_3D", format = "OFF")
    exportScene(sceneObj2, filename = "rphys2_3D", format = "OFF")
    drawScene(sceneObj1, engine = "standard")
    drawScene(sceneObj2, engine = "standard")
    • Bibliothèque ggplot2
    fig1 <- ggplot(rphys1, aes(x, y, fill = z)) +
     geom_raster(interpolate = TRUE) +
     scale_fill_continuous(low = lowCol, high = highCol, name = "") +
     xlab("") + ylab("") +
     theme(legend.position = "top", plot.margin = oddMargins)
    fig2 <- ggplot(rphys2, aes(x, y, fill = z)) +
     geom_raster(interpolate = TRUE) +
     scale_fill_continuous(low = lowCol, high = highCol, name = "") +
     xlab("") +
     theme(legend.position = "top", axis.title.y = element_blank(), axis.text.y = element_blank(), plot.margin = evenMargins)
    fig3 <- ggplot(rphys1, aes(x, y, colour = z)) +
     geom_point(shape = shapeChar, size = shapeSize) +
     scale_colour_gradient(low = lowCol, high = highCol, name = "") +
     xlab("") + ylab("") +
     theme(legend.position = "top", plot.margin = oddMargins)
    fig4 <- ggplot(rphys2, aes(x, y, colour = z)) +
     geom_point(shape = shapeChar, size = shapeSize) +
     scale_colour_gradient(low = lowCol, high = highCol, name = "") +
     xlab("") +
     theme(legend.position = "top", axis.title.y = element_blank(), axis.text.y = element_blank(), plot.margin = evenMargins)
    grid.arrange(fig1, fig2, ncol = 2, respect = TRUE)
    grid.arrange(fig3, fig4, ncol = 2, respect = TRUE)

    Note. À part ceux de ggplot2, le reste des résultats s'interfacent très péniblement avec Tikz. Donc Gnuplot gagne haut la main sur la question de l'interopérabilité avec LaTeX et sur celle de la légèreté+rapidité, modulo mes connaissances très minimes sur Tikz : je suis preneur des propositions de comment éviter des erreurs du genre

    TeX capacity exceeded, sorry [main memory size=5000000].

    Oui, j'ai lu des propositions de solutions de ce type mais rien n'y a fait.