0

I am using RStudio to draw the histogram of the values in this data.

data = read.table("C:\\Test\\test.csv", header=TRUE, sep=",")
hist(data$a, breaks=100)
hist(data$b, breaks=100)

and got the following histograms:

enter image description here enter image description here But I want to:

1- have the y axis logged so that instead of values 0, 4000, 8000 and 12000 I'll have 0, 10, 100, 1000, 10000 and so on (log2 i.e., 0, 1, 2, 4, 8, 16, ... is also useful).

2- Have the two diagrams in a single diagram (preferably with bars of two different colors/patterns). In the resulting diagram, the two bars for each x-value would be beside each other like this: enter image description here

I tried this solution but got the following error:

NULL

Warning message: In (function () : Only one RStudio graphics

device is permitted

Community
  • 1
  • 1
Alisa
  • 2,892
  • 3
  • 31
  • 44
  • You've some histograms bars with height very close to zero in your graph. You might want to do something about that before applying log scale. – narendra-choudhary Dec 26 '15 at 03:21
  • And why this question is tagged rstudio? – narendra-choudhary Dec 26 '15 at 03:23
  • @Narendra, having the small values is the reason why I want log scaling. Instead of showing 0 as the small values, I want to show both small values and big values in the same diagram. – Alisa Dec 26 '15 at 04:44
  • @Narendra, I have seen in a comment ( http://stackoverflow.com/questions/11798967/image-plot-does-not-create-new-image ) that dev.new() which is part of the offered solution that I linked is not working in RStudio. So I added RStudio as a related tag. – Alisa Dec 26 '15 at 04:45

2 Answers2

1

Here's how I did it:

## Create fake data
x <- c(rep(1, 100), rep(2, 20000), rep(3, 800), rep(4, 10000))
y <- c(rep(1, 10), rep(2, 1000), rep(3, 10000), rep(4, 2000))

## Plot x
hist.x <- hist(x, plot = FALSE)
hist.x$counts <- log10(hist.x$counts + 1)
plot(hist.x, col = rgb(0, 0, 1, 0.25))

## Plot y
hist.y <- hist(y, plot = FALSE)
hist.y$counts <- log10(hist.y$counts + 1)
plot(hist.y, col = rgb(1, 0, 0, 0.25), add = TRUE)

which results in this:

enter image description here

If you would like them to be next to each other, just add

par(mfrow = c(1, 2)) 

at the top and change the plot command for y to `

plot(hist.y, col = rgb(1, 0, 0, 0.25))

The resulting plot looks like this: enter image description here

ytk
  • 2,787
  • 4
  • 27
  • 42
  • @alisa, I added an edit to show how you can do it in two diagrams beside each other! – ytk Dec 26 '15 at 14:47
  • everything is fine, but I want the bars in one single diagram, beside each other. I have edited the question and put a sample output example. – Alisa Dec 26 '15 at 17:05
0

Here's the solution I devised inspired from Teja_K 's answer:

data = read.table("C:\\test\\test.csv", header=TRUE, sep=",")
par( mar=c(3.1, 5.1, 0, 0)) 
hist.x <- hist(data$a, plot = FALSE, breaks=50)
hist.x$counts <- log10(hist.x$counts + 1)
plot(hist.x, col = rgb(0, 0, 1, 0.99), main="", xlab="", ylab="", yaxt="n")
yAxesTitles=c(1, 10, 100, 1000, 10000)
axis(2, at=c(0, 1, 2, 3, 4),labels=yAxesTitles, col.axis="black", las=2)
mtext(side = 1, text = "Number", line = 2)
mtext(side = 2, text = "Frequency", line = 4)
# Adding the second diagram to the first one:
relocatedData=data$b+0.2
hist.y <- hist(relocatedData, plot = FALSE, breaks=50)
hist.y$counts <- log10(hist.y$counts + 1)
plot(hist.y, col = rgb(1, 0, 0, 0.99), main="", xlab="", ylab="", yaxt="n", add=TRUE)
legend(7.5, 4, c("a", "b"), lwd=c(1, 1), col=c(rgb(0, 0, 1, 0.99), rgb(1, 0, 0, 0.99)), pch = c(15, 15), pt.cex=2)

And the result: enter image description here

Alisa
  • 2,892
  • 3
  • 31
  • 44