R ggplot2: using stat_summary (average) and logarithmic scale

I have a bunch of measurements over time, and I want to build them in R. Here is an example of my data. I have 6 dimensions for each of 4 time points:

values <- c (1012.0, 1644.9, 837.0, 1200.9, 1652.0, 981.5, 2236.9, 1697.5, 2087.7, 1500.8, 2789.3, 1502.9, 2051.3, 3070.7, 3105.4, 2692.5, 1488.5, 1978.1, 1925.4, 1524.3, 2772.0, 1355.3, 2632.4, 2600.1) time <- factor (rep (c(0, 12, 24, 72), c(6, 6, 6, 6))) 

The scale of this data is arbitrary, and in fact I'm going to normalize it, so that the average value of t = 0 is 1.

 norm <- values / mean (values[time == 0]) 

So far so good. Using ggplot , I draw both individual points and a line passing through the average value at each moment in time:

 require (ggplot2) p <- ggplot(data = data.frame(time, norm), mapping = aes (x = time, y = norm)) + stat_summary (fun.y = mean, geom="line", mapping = aes (group = 1)) + geom_point() 

However, now I want to apply the logarithmic scale, and it is here that my problem begins. When I do this:

 q <- ggplot(data = data.frame(time, norm), mapping = aes (x = time, y = norm)) + stat_summary (fun.y = mean, geom="line", mapping = aes (group = 1)) + geom_point() + scale_y_log2() 

The line does NOT go through 0 at t = 0, as one would expect, because log (1) == 0. Instead, the line intersects the y axis slightly below 0. Apparently, ggplot applies the average value after the log transformation, which gives another result. I want it to take value before converting the log.

How can I tell ggplot apply the first value? Is there a better way to create this chart?

+4
source share
3 answers

scale_y_log2() will perform the conversion first, and then calculate the geometers.

coord_trans() will do the opposite: first calculate the geometry, and transform the axis.

So you need coord_trans(ytrans = "log2") instead of scale_y_log2()

+6
source

To solve this problem, if you do not want to use the coord_trans () function and still want to convert the data, you must create a function that will return its transformation:

 f1 <- function(x) { 10^(mean(x)) } stat_summary (fun.y = f1, geom="line", mapping = aes (group = 1)) 
0
source

The best solution I found for this problem was to use coord_trans() and scale_y_continuous(breaks = breaks) combos

As suggested earlier, using coord_trans will scale your axis without data transformation, however this will leave you with an ugly axis.

Setting limits in coord_trans works for some things, but if you want to correct your axis to have certain labels, then you turn on scale_y_continuous with the breaks you want to set.

 coord_trans(y = 'log10') + scale_y_continuous(breaks = breaks) 
0
source

All Articles