Ggplot barplot: how to display small positive numbers with a y-axis logarithmic scale

The main problem: I want to display data from 0 to 1.0 as an ascending bar (starting from 0), but I do not want the intervals to be evenly distributed, but with the log interval.

I'm trying to display a column labeled "mean" in the dataset below as a bar graph in ggplot, but since the numbers are very small, I would like to show the y axis in a logarithmic scale, rather than log-transforming the data itself. In other words, I want to have vertical bars with Y axis labels like 0, 1e-8, 1e-6 1e-4 1e-2 and 1e-0 (i.e., from 0 to 1.0, but the intervals vary on a scale )

The solution below does not work because the columns are inverted.

> print(df)
        type         mean           sd           se snp
V7    outer 1.596946e-07 2.967432e-06 1.009740e-08   A
V8    outer 7.472417e-07 6.598652e-06 2.245349e-08   B
V9    outer 1.352327e-07 2.515771e-06 8.560512e-09   C
V10   outer 2.307726e-07 3.235821e-06 1.101065e-08   D
V11   outer 4.598375e-06 1.653457e-05 5.626284e-08   E
V12   outer 5.963164e-07 5.372226e-06 1.828028e-08   F
V71  middle 2.035414e-07 3.246161e-06 1.104584e-08   A
V81  middle 9.000131e-07 7.261463e-06 2.470886e-08   B
V91  middle 1.647716e-07 2.875840e-06 9.785733e-09   C
V101 middle 3.290817e-07 3.886779e-06 1.322569e-08   D
V111 middle 6.371170e-06 1.986268e-05 6.758752e-08   E
V121 middle 8.312429e-07 6.329386e-06 2.153725e-08   F

The code below correctly generates a grouped barcode with errors

ggplot(data=df, aes(x=snp,y=mean,fill=type))+
  geom_bar(stat="identity",position=position_dodge(),width=0.5) + 
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se),width=.3, position=position_dodge(.45)) 

, , y , scale_y_log10() :

 ggplot(data=df, aes(x=snp,y=mean,fill=type))+
  geom_bar(stat="identity",position=position_dodge(),width=0.5) + scale_y_log10() +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se),width=.3, position=position_dodge(.45)) 

, , ( ) , .

0
1

, , , , . geom_segment , "" ( , ), . , x .

y = 1e-20 y = 1. Y , , , , 1-20 1-19, , , 1-8 1-7, .

, , , - . , , 1e-20 1e-100 . , , .

-, , @hrbrmstr, , , , 10 . .

ggplot(data=df, aes(x=as.numeric(snp) + 0.3*(as.numeric(type) - 1.5), 
                    y=mean, colour=type)) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.3) +
  geom_segment(aes(xend=as.numeric(snp) + 0.3*(as.numeric(type) - 1.5),
                   y=1e-20, yend=mean), size=5) +
  scale_y_log10(limits=c(1e-20, 1), breaks=10^(-100:0), expand=c(0,0)) +
  scale_x_continuous(breaks=1:6, labels=LETTERS[1:6])

enter image description here

, , :

pd = position=position_dodge(.5)
ggplot(data=df, aes(x=snp,y=mean,fill=type))+
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se, colour=type), width=.3, position=pd) +
  geom_point(aes(colour=type), position=pd) +
  scale_y_log10(limits=c(1e-7, 1e-5), breaks=10^(-10:0)) +
  annotation_logticks(sides="l")

enter image description here

+4

Source: https://habr.com/ru/post/1691208/


All Articles