减小R上的x轴刻度

时间:2014-04-08 17:37:21

标签: r scale histogram

我正在尝试绘制直方图,显示作为深度函数的个体数量,如下图所示: enter image description here

我正在使用这个简单的代码:

hist(dataset,xlab="Depth",ylab="Number of individuals")

但是我无法使用xaxis值来操作以显示深度0和100之间的更多细节。我需要缩小比例以显示更多细节。

任何解决方案? 谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用基本绘图或ggplot2

执行此操作

一些示例数据:

var <- sample(1:50,size=10000,replace=T)
dataset <- as.data.frame(var)

使用基础绘图,您可以使用:

hist(dataset, xlab="Depth", ylab="Number of individuals", breaks=10)

但是,这并不能解决您的问题。使用ggplot2,您可以更好地控制情节的外观。两个例子:

ggplot(data=dataset, aes(x=var)) + 
  geom_histogram(fill = "white", color = "black", binwidth = 10) +
  scale_x_continuous("Depth", limits=c(0,60), breaks=c(0,10,20,30,40,50,60)) +
  scale_y_continuous("Number of individuals", limits=c(0,2100), breaks=c(0,400,800,1200,1600,2000)) +
  theme_bw()

给出: enter image description here

ggplot(data=dataset, aes(x=var)) + 
  geom_histogram(fill = "white", color = "black", binwidth = 5) +
  scale_x_continuous("Depth", limits=c(0,60), breaks=c(0,10,20,30,40,50,60)) +
  scale_y_continuous("Number of individuals", limits=c(0,1100), breaks=c(0,250,500,750,1000)) +
  theme_bw()

给出: enter image description here

使用binwidth参数,您可以选择所需的详细信息。

答案 1 :(得分:0)

您可以通过设置特定参数来定义中断数量,例如:

dataset <- sample(1:50,size=10000,replace=T) # random data

hist(dataset,xlab="Depth",ylab="Number of individuals",breaks=100)

enter image description here

要更改x轴,hust xaxp图形参数上的标签,例如:

dataset <- sample(1:50,size=10000,replace=T) # random data

nSep <- 5

f <- hist(dataset,xlab="Depth",ylab="Number of individuals", 
          xaxp = c(min(dataset), max(dataset), nSep))

enter image description here

相关问题