1

I have a frequency distribution with a long tail and to make better use of the plot real state I draw a logarithmic plot. (The data below is for the sake of presentation)

freqs=c(0.7, 0.2, 0.05, 0.01, 0.001)
x=0:(length(freqs)-1)

df=data.frame(x=x, y=freqs)

library("ggplot2")

ggplot(df)  + 
geom_bar(mapping = aes(x = x, y = y),stat =   "identity")+ scale_y_log10() 

But then since the numbers are smaller than one, the logarithms will be negative and so the bars are upside down. Any way to go around this?

Reza
  • 388
  • 2
  • 14
  • just shift everything above 1? – Nate Jul 02 '17 at 20:37
  • 1
    Leave it as-is because it's accurate? Use `scales::log1p_trans`? Use a different transform (square root?)? What result do you want to see? How do you expect the plot to look? – Gregor Thomas Jul 02 '17 at 20:43
  • 3
    my suggestion would be *not* to use a bar plot here, just use `geom_point()`; arguably the reason you're having trouble is that you're violating one of the implicit premises of a barplot, which is that the baseline is at zero ... – Ben Bolker Jul 02 '17 at 21:00

1 Answers1

1

An interesting solution is based on the use of geom_segment as suggested here.

freqs <- c(0.7, 0.2, 0.05, 0.01, 0.001)
x <- 0:(length(freqs)-1)
df <- data.frame(x=x, y=freqs)

library(ggplot2)
ggplot(df) + 
geom_segment(aes(x=x, xend=x, y=1e-4, yend=y), size=35, col="gray30") + 
scale_y_log10(breaks=c(0.001,0.01,0.1,1))+ ylab("")+ xlab("") +
scale_x_discrete(limits=c(-.5,4.5)) 

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58