Make scale_y_log10 to have the tickmarks at 0.01,0

2019-06-15 08:20发布

问题:

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.

回答1:

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:

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:

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))



回答2:

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")



标签: r ggplot2