新浪博客

ggplot2学习笔记:关于坐标轴的注释(axis, scales and transformations)

2016-04-06 16:56阅读:
参考资料: (1)好好看看这个帖子!很详细:http://www.sthda.com/english/wiki/ggplot2-axis-scales-and-transformations
(2)一般详细:http://docs.ggplot2.org/0.9.3/annotation_logticks.html
(3)官方解释:http://docs.ggplot2.org/current/scale_continuous.html
主要知识点:
scale_y_log10

Log and sqrt transformations

Built in functions for axis transformations are :
  • scale_x_log10(), scale_y_log10() : for log10 transformation
  • scale_x_sqrt(), scale_y_sqrt() : for sqrt transformation
  • scale_x_reverse(), scale_y_reverse() : to reverse coordinates
  • coord_trans(x =“log10”, y=“log10”) : possible values for x and y are “log2”, “log10”, “sqrt”, …
  • scale_x_continuous(trans=‘log2’),
scale_y_continuous(trans=‘log2’) : another allowed value for the argument trans is ‘log10’These functions can be used as follow :
# Default scatter plot sp <- ggplot(cars, aes(x = speed, y = dist)) + geom_point() sp # Log transformation using scale_xx() # possible values for trans : 'log2', 'log10','sqrt' sp + scale_x_continuous(trans='log2') + scale_y_continuous(trans='log2') # Sqrt transformation sp + scale_y_sqrt() # Reverse coordinates sp + scale_y_reverse()
应用实例:http://stackoverflow.com/questions/21533158/remove-outliers-fully-from-multiple-boxplots-made-with-ggplot2-in-r-and-display


plt_wool <- ggplot(subset(df_mlt, value > 0), aes(x=ID1,y=value)) + geom_boxplot(aes(color=factor(ID1)),outlier.colour = NA) + scale_y_log10(breaks = trans_breaks('log10', function(x) 10^x), labels = trans_format('log10', math_format(10^.x))) + theme_bw() + theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+ theme(axis.text=element_text(size=20)) + theme(axis.title=element_text(size=20,face='bold')) + labs(x = 'x', y = 'y',colour='legend' ) + annotation_logticks(sides = 'rl') + theme(panel.grid.minor = element_blank()) + guides(title.hjust=0.5) + theme(plot.margin=unit(c(0,1,0,0),'mm'))
所画的图如下所示:
ggplot2学习笔记:关于坐标轴的注释(axis, <wbr>scales <wbr>and <wbr>transformations)



我的更多文章

下载客户端阅读体验更佳

APP专享