开发者

What package is to be installed in R for scatter plots with logarithmic binning?

开发者 https://www.devze.com 2023-03-17 22:52 出处:网络
I am trying to produce some high density scatter plots with R. What package should be installe开发者_StackOverflowd for this? Or is there any other way to obtain the plots.If you really do want a log

I am trying to produce some high density scatter plots with R. What package should be installe开发者_StackOverflowd for this? Or is there any other way to obtain the plots.


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


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.


ggplot2? - see examples for geom_point (using alpha) or geom_hex

0

精彩评论

暂无评论...
验证码 换一张
取 消