Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make scale_y_log10 to have the tickmarks at 0.01,0.1,1

Tags:

r

ggplot2

I wrote the following code to make the plot

pd<- position_dodge(.2)  # # move them .05 to the left and right
pm25 <- ggplot(data, aes(x=CombSEG, y=conc,shape=A,color=A, lty=A,     group=A)) + 
geom_point() +
geom_line() +
geom_errorbar(aes(ymin=conc-se, ymax=conc+se),
              width=.1, position=pd) +
theme_bw()+
limits(c(0
scale_y_log10(breaks=c(0.01,0.1,1),labels=c(0.01,0.1,1))

The automatic scale breaks are 10^-1.8, 10^-1.6, 10^-1.4 ... 10^-0.4. I would actually like the lowest tick to be 0.01 and the highest tickmark is 1.

Thank you for your help.

Edits: Here is what the plot look like after I tried your code. enter image description here

like image 401
Amateur Avatar asked Feb 10 '12 05:02

Amateur


2 Answers

Use the breaks and labels arguments of scale_y_log10 (read about them here).

# make up some sample data
df <- data.frame(x=1:100,y=10^-(2*runif(100)))
ggplot(df,aes(x=x,y=y)) + geom_point() + scale_y_log10()

Looks like: enter image description here

Then to modify the log10 scale to have custom breaks at .01, .1 and 1, use the breaks argument:

ggplot(df,aes(x=x,y=y)) + geom_point() + scale_y_log10(breaks=c(.01,.1,1))

Looks like: enter image description here

Finally, if you want the labels to also be 0.1, .1 and 1, use the labels argument:

ggplot(df,aes(x=x,y=y)) + geom_point() + 
          scale_y_log10(breaks=c(.01,.1,1),labels=c(.01,.1,1))

enter image description here

like image 58
mathematical.coffee Avatar answered Oct 05 '22 16:10

mathematical.coffee


Use coord_trans() instead of scale()

df <- data.frame(x=1:100,y=10^-(2*runif(100)))
ggplot(df,aes(x=x,y=y)) + geom_point() + coord_trans(y = "log10")

enter image description here

like image 25
Thierry Avatar answered Oct 05 '22 15:10

Thierry