Search code examples
rplotlevelplot

How to keep plot continous after setting xlim and ylim using levelplot in R?


I plot a graph which I want to specify xlim and ylim values for! However it does not work. Here is the function I am using:

DensityPlotF <- function (xx, plotname)
{
        library(RColorBrewer)
        library(latticeExtra)
        library(lattice)
        lattice.options(default.theme = standard.theme(color = FALSE))
        cols <- colorRampPalette(brewer.pal(11, "RdBu"))
        ppi <- 300
        png(filename = plotname, width=6*ppi, height=6*ppi, res=ppi)
        print(levelplot(xx$rho ~ xx$r + xx$z, panel = panel.levelplot.points, col.regions = cols, xlab="R (Å)", ylab="z (Å)", xlim=c(0,10.0), ylim=c(-30,30)))
        dev.off()
}

cl2d <- read.table("cl_2d.dat", header=TRUE)
colnames(cl2d) <- c("r", "z", "rho")
DensityPlotF(cl2d, "cl2d.png")

The .dat file looks like as follows:

#r z rho
6.500000e+00 -4.250000e+01 3.397767e-01
6.500000e+00 -4.200000e+01 3.246126e-01
6.500000e+00 -4.150000e+01 3.301116e-01
6.500000e+00 -4.100000e+01 3.289452e-01
6.500000e+00 -4.050000e+01 3.264456e-01

Finally, graph before xlim and xlim were set: enter image description here

After I set xlim and ylim, plot looks weird. Is there an easy way to show columns in the plot all together (ie continuous)?

enter image description here


Solution

  • You want to move "xlim=c(0,10.0), ylim=c(-30,30)" inside levelplot, i.e.,
    print(levelplot(.... ylab="z (Å)", xlim=c(0,10.0), ylim=c(-30,30)))
    Remove the braces right after ylab

    PS: not enough points to put it under comment