Verwandeln Sie nur eine Achse zu log10 Skala mit ggplot2
-
11-10-2019 - |
Frage
Ich habe folgendes Problem: Ich möchte eine diskrete und kontinuierliche Variable auf einem boxplot sichtbar zu machen, in dem dieser ein paar extrem hohe Werte aufweist. Dies macht die boxplot bedeutungslos (die Punkte und sogar den „Körper“ des Diagramms zu klein ist), deshalb habe ich diese Skala auf einem log10 zeigen möchte. Ich bin mir bewusst, dass ich die Extremwerte aus der Visualisierung verlassen konnte, aber ich bin nicht dazu gedacht.
siehe Nehmen wir ein einfaches Beispiel mit Diamanten Daten:
m <- ggplot(diamonds, aes(y = price, x = color))
Das Problem ist hier nicht ernst, aber ich hoffe, Sie könnten sich vorstellen, warum ich möchte die Werte bei einer log10 Skala sehen. Versuchen wir es:
m + geom_boxplot() + coord_trans(y = "log10")
Wenn Sie die y-Achse zu sehen ist log10 skaliert und sieht gut aus, aber es gibt ein Problem mit der x-Achse, die die Handlung sehr seltsam macht.
Das Problem tritt nicht mit scale_log
, aber das ist keine Option für mich , wie ich nicht einen benutzerdefinierten Formatierer diese Weise nutzen kann. Z.
m + geom_boxplot() + scale_y_log10()
Meine Frage: Hat jemand eine Lösung kennen die boxplot mit log10 Skala auf der y-Achse plotten, die Etiketten frei mit einer formatter
Funktion wie in diesem Thread ?
Bearbeiten der Frage zu helfen Beantworter basierend auf Antworten und Kommentare:
Was ich wirklich nach: ein log10 transformierte Achse (y) mit nicht wissenschaftlichen Etiketten. Ich mag es wie Dollar (formatter=dollar)
oder ein benutzerdefiniertes Format beschriften.
Wenn ich versuche @ Hadleys Vorschlag, den ich die folgenden Warnungen erhalten:
> m + geom_boxplot() + scale_y_log10(formatter=dollar)
Warning messages:
1: In max(x) : no non-missing arguments to max; returning -Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
Mit einer unveränderten y-Achse Etikett:
Lösung
Am einfachsten ist es nur geben dem ‚trans‘ (früher ‚Formatter‘ Argument den Namen der Log-Funktion:
m + geom_boxplot() + scale_y_continuous(trans='log10')
EDIT: Oder wenn Sie wie das nicht tun, dann eine diese erscheint zu geben unterschiedliche, aber brauchbare Ergebnisse:
m <- ggplot(diamonds, aes(y = price, x = color), log="y")
m + geom_boxplot()
m <- ggplot(diamonds, aes(y = price, x = color), log10="y")
m + geom_boxplot()
EDIT2 & 3: Weitere Experimente (nach dem man verwirft, die erfolgreich zu setzen „$“ Zeichen vor der protokollierten Werte versucht):
fmtExpLg10 <- function(x) paste(round_any(10^x/1000, 0.01) , "K $", sep="")
ggplot(diamonds, aes(color, log10(price))) +
geom_boxplot() +
scale_y_continuous("Price, log10-scaling", trans = fmtExpLg10)
Hinweis hinzugefügt Mitte 2017 Kommentare über Paket-Syntax ändern:
scale_y_continuous (Formatierer = 'log10') ist jetzt scale_y_continuous (trans = 'log10') (ggplot2 v2.2.1)
Andere Tipps
I had a similar problem and this scale worked for me like a charm:
breaks = 10**(1:10)
scale_y_log10(breaks = breaks, labels = comma(breaks))
as you want the intermediate levels, too (10^3.5), you need to tweak the formatting:
breaks = 10**(1:10 * 0.5)
m <- ggplot(diamonds, aes(y = price, x = color)) + geom_boxplot()
m + scale_y_log10(breaks = breaks, labels = comma(breaks, digits = 1))
After executing::
Another solution using scale_y_log10
with trans_breaks
, trans_format
and annotation_logticks()
library(ggplot2)
m <- ggplot(diamonds, aes(y = price, x = color))
m + geom_boxplot() +
scale_y_log10(
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x))
) +
theme_bw() +
annotation_logticks(sides = 'lr') +
theme(panel.grid.minor = element_blank())
I think I got it at last by doing some manual transformations with the data before visualization:
d <- diamonds
# computing logarithm of prices
d$price <- log10(d$price)
And work out a formatter to later compute 'back' the logarithmic data:
formatBack <- function(x) 10^x
# or with special formatter (here: "dollar")
formatBack <- function(x) paste(round(10^x, 2), "$", sep=' ')
And draw the plot with given formatter:
m <- ggplot(d, aes(y = price, x = color))
m + geom_boxplot() + scale_y_continuous(formatter='formatBack')
Sorry to the community to bother you with a question I could have solved before! The funny part is: I was working hard to make this plot work a month ago but did not succeed. After asking here, I got it.
Anyway, thanks to @DWin for motivation!