在R中创建具有对数刻度的条形图 [英] Create bar plot with logarithmic scale in R

查看:55
本文介绍了在R中创建具有对数刻度的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的数据从3.92到65700,我试图创建一个对数刻度的条形图.

这是我到目前为止使用的代码:

 海滩<-c(PlasticsBlue = 3.92,PlasticsGrey = 65700,FoamsOrange = 17.9,FoamsWhite = 51300,RopesGreen = 9.71,RopesGreen = 3140)海滩par(mar = c(10,5,10,5))barplot(beach,names.arg = c(塑料/蓝色",塑料/灰色",泡沫/橙色",泡沫/白色",绳索/绿色",绳索/绿色";),col = c("red2","slateblue4","red2","slateblue4","red2","slateblue4","red2"),图例.text = c(";最低",最高"),args.legend = list(cex = 0.75,x ="topright"),ylim = c(1,100000),log =("y"),las = 2,ylab = expression("mg g" ^-1)) 

哪个给了我这张图

I am trying to create a bar plot with a logarithmic scale as my data varies from 3.92 to 65700.

This is the code i have used so far:

beach <- c(PlasticsBlue=3.92, PlasticsGrey=65700, FoamsOrange=17.9, FoamsWhite=51300, RopesGreen=9.71, RopesGreen=3140)
beach
par(mar = c(10, 5, 10, 5))

barplot(beach, names.arg=c("Plastics/Blue", "Plastics/Grey", "Foams/Orange", "Foams/White", "Ropes/Green", "Ropes/Green"), col=c("red2", "slateblue4", "red2", "slateblue4", "red2", "slateblue4", "red2"), legend.text = c("Lowest", "Highest"), args.legend=list(cex=0.75,x="topright"), ylim=c(1,100000), log = ("y"), las=2, ylab = expression("mg g"^-1)) 

Which has given me this graph graph

This is exactly what I'm looking for apart from the log function used means that the next tick mark would be 1000000 which is far too large and therefore currently the y axis is only numbered up to 10000 which does not incorporate my largest values. Is there any way around this to have the y axis numbered up to 100000 whilst still using the log function as this seemed to work when I first made the graph in excel (see graph2 link) graph2

Thanks in advance, Alistair

解决方案

You can always get what if you are willing to fiddle with the details in R. In this case it is easier to bypass R's helpful log axis and construct your own:

options(scipen=8)
out <- barplot(log10(beach), names.arg=c("Plastics/Blue", "Plastics/Grey", "Foams/Orange",
      "Foams/White", "Ropes/Green", "Ropes/Green"), col=c("red2", "slateblue4", "red2",
      "slateblue4", "red2", "slateblue4", "red2"), legend.text = c("Lowest", "Highest"),
       args.legend=list(cex=0.75,x="topright"), ylim=c(0, 5), las=2, yaxt="n",
       ylab = expression("mg g"^-1))
yval <- c(1, 10, 100, 1000, 10000, 100000)
ypos <- log10(yval)
axis(2, ypos, yval, las=1)
text(out, log10(beach), beach, pos=3, xpd=NA)

The first line just keeps R from switching to scientific notation for the 100000 value. The barplot differs in that we convert the raw data with log10() set the ylim based on the log10 values, and suppress the y-axis. Then we create a vector of the positions on the y axis we want to label and get their log10 positions. Finally we print the axis. The last line uses the value out from barplot which returns the positions of the bars on the x axis so we can print the values on the tops of the bars.

这篇关于在R中创建具有对数刻度的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆