问题
I am trying to produce some high density scatter plots with R. What package should be installed for this? Or is there any other way to obtain the plots.
回答1:
If you really do want a log scaled scatterplot, then this is how to create them in each of the 3 plotting systems.
First, some data:
dfr <- data.frame(x = rlnorm(1e5), y = rlnorm(1e5))
In base graphics:
with(dfr, plot(x, y, log = "xy"))
In lattice graphics:
library(lattice)
p1 <- xyplot(y ~ x, dfr, scales = list(log = TRUE))
p1
In ggplot2 graphics (will need to install that package + dependencies):
library(ggplot2)
p2 <- ggplot(dfr, aes(x, y)) +
geom_point() +
scale_x_log10() +
scale_y_log10()
p2
回答2:
I've just been struggling with trying to plot these recently; and just ended up using the standard hist() function with a custom set of breaks:
x <- your data
nbreaks <- 50 # how many points do you want in your scatter plot
breaks <- exp(seq(log(min(x)), log(max(x)), len=nbreaks))
hh <- hist(x, breaks, plot=FALSE)
plot(hh$mids, hh$density, log="xy")
I.e. create an exponentially distributed set of breaks and generate the histogram, but manually plot the densities giving control over which axes are logged.
回答3:
ggplot2? - see examples for geom_point (using alpha) or geom_hex
来源:https://stackoverflow.com/questions/6598174/what-package-is-to-be-installed-in-r-for-scatter-plots-with-logarithmic-binning