开发者

Plotting mean and std. deriv. of logarithmic data in R

I'd like to plot some data stored in two vectors (x and y) i开发者_如何学运维n loglog scale. Furthermore, I want to add the mean and the standard derivation (latter using bars).

My problem is, that there are zeros in my y-data-vector and the "mean" function then gets log(0) (=-Inf) as an argument and also returns -Inf

qplot(x, y, log="xy") + stat_summary(fun.y=mean, geom="point")

How can I make the "mean" function work on the 'normal' data and not on the log'ed data?

Cheers,

Manuel


Calculate the stats before the transformation.

Ignoring the log scales for now, I think what you want to plot is something like this

p <- ggplot(dfr) + 
  geom_point(aes(x, y)) +       
  geom_point(
    aes(
      x = mean(x), 
      y = mean(y)
    ), 
    colour = "blue",
    size = 5
  ) +
  geom_rect(
    aes(
      xmin = mean(x) - sd(x),
      xmax = mean(x) + sd(x), 
      ymin = mean(y) - sd(y),
      ymax = mean(y) + sd(y)
    ),
    alpha = 0.2
  ) 
p

Now adding in the log scale is done as usual

p +
  scale_x_log10() + 
  scale_y_log10() 

Of course, you zeroes will not show on the graph, as they shouldn't. To deal with them, you have a choice between removing them from the dataset or substituting a small positive number.


EDIT: If you want stats for y values grouped by an x value, it sounds like your x-variable is a factor, in which case you probably want a barchart. Log y scales for barcharts are a bad idea, but you could possibly justify a square root transformation instead.


Read the help page for coord_trans. Using coord_trans(xtrans = 'log10', ytrans = 'log10') would help you create a log-log plot, since coordinate transformations occur after all statistics have been calculated.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜