[英]How to make an R barplot with a log y-axis scale?
This should be a simple question... I'm just trying to make a barplot from a vector in R, but want the values to be shown on a log scale, with y-axis tick marks and labelling. 这应该是一个简单的问题......我只是想从R中的矢量绘制一个条形图,但希望这些值以对数刻度显示,带有y轴刻度标记和标记。 I can make the normal barplot just fine, but when I try to use log or labelling, things go south.
我可以使普通的条形图很好,但是当我尝试使用日志或标签时,事情就会向南移动。
Here is my current code: 这是我目前的代码:
samples <- c(10,2,5,1,2,2,10,20,150,23,250,2,1,500)
barplot(samples)
Ok, this works. 好的,这很有效。 Then I try to use the
log=""
function defined in the barplot manual, and it never works. 然后我尝试使用barplot手册中定义的
log=""
函数,它永远不会起作用。 Here are some stupid attempts I have tried: 以下是我尝试过的一些愚蠢的尝试:
barplot(samples, log="yes")
barplot(samples, log="TRUE")
barplot(log=samples)
Can someone please help me out here? 有人可以帮帮我吗? Also, the labelling would be great too.
此外,标签也很棒。 Thanks!
谢谢!
The log
argument wants a one- or two-character string specifying which axes should be logarithmic. log
参数需要一个或两个字符的字符串,指定哪些轴应该是对数的。 No, it doesn't make any sense for the x-axis of a barplot to be logarithmic, but this is a generic mechanism used by all of "base" graphics - see ?plot.default
for details. 不,barplot的x轴是对数的没有任何意义,但这是所有“基础”图形使用的通用机制 - 有关详细信息,请参阅
?plot.default
。
So what you want is 所以你想要的是
barplot(samples, log="y")
I can't help you with tick marks and labeling, I'm afraid, I threw over base graphics for ggplot
years ago and never looked back. 我无法帮助你使用刻度线和标签,我担心,我在几年前为
ggplot
扔了基础图形并且从未回头。
This should get your started fiddling around with ggplot2
. 这应该让你开始摆弄
ggplot2
。
d<-data.frame(samples)
ggplot(data=d, aes(x=factor(1:length(samples)),y=samples)) +
geom_bar(stat="identity") +
scale_y_log10()
Within the scale_y_log10()
function you can define breaks, labels, and more. 在
scale_y_log10()
函数中,您可以定义scale_y_log10()
,标签等。 Similarly, you can label the x-axis. 同样,您可以标记x轴。 For example
例如
ggplot(data=d, aes(x=factor(1:length(samples)),y=samples)) +
geom_bar(stat="identity") +
scale_y_log10(breaks=c(1,5,10,50,100,500,1000),
labels=c(rep("label",7))) +
scale_x_discrete(labels=samples)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.