0

I am trying to plot a histogram showing the number of individuals as a function of Depth as the picture below: enter image description here

I am using this simple code:

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

However i cannot manipulate with the xaxis values to show more details between the depths 0 and 100. I need to decrease the scale to show more detail.

Any solution? Thank you

Myr
  • 67
  • 1
  • 1
  • 11
  • You might want to look here for some other insights: http://stackoverflow.com/questions/3785089/r-change-the-spacing-of-tick-marks-on-the-axis-of-a-plot – Nathaniel Payne Apr 08 '14 at 17:51
  • Have you read the help text? `?hist`. Please study the `breaks` argument, and run through the examples. – Henrik Apr 08 '14 at 17:53

2 Answers2

0

You can do this with the base plotting or with ggplot2.

Some sample data:

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

With the base plotting you could for example use:

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

However, this doesn't solve your problem. With ggplot2 you much more control about the appearance of your plot. Two examples:

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

which gives: 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()

which gives: enter image description here

With the binwidth parameter you can choose how much detail you want.

Jaap
  • 81,064
  • 34
  • 182
  • 193
0

You can define the number of breaks by setting the specific parameter e.g. :

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

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

enter image description here

To change the labels on the xaxis, hust xaxp graphical parameter, e.g. :

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

digEmAll
  • 56,430
  • 9
  • 115
  • 140