SAN
SAN

Reputation: 2247

dodging in r language

I am trying to generate a histogram of below data (Data comes from a sqlserver database)

    > head(Data)
     value temp
1 47.34848   97
2 45.95588   97
3 47.34848   97
4 46.99248   97
5 46.64179   97
6 46.29630   97

I tried qplot in ggplot with dodging. I was hoping i will get multiple histogram, but i got a single histogramenter image description here

> qplot(value, data=Data, geom = "bar", fill = temp, position = "dodge")

To verify that i have two different temperatures in Data, i generated a histogram of temp

> qplot(temp,data=Data,geom="bar")

enter image description here

I also generated a histogram of the value and it is same as the first plot above. To verify my commands, i generated a graph with some sample data and the command i am using seems to be ok

    > head(SampleData)
  val cat
1   1   a
2   2   a
3   3   a
4   4   a
5   4   a
6   2   a

enter image description here

Please help me find the issue

Upvotes: 0

Views: 133

Answers (1)

Vincent Zoonekynd
Vincent Zoonekynd

Reputation: 32351

The variable used to define the two groups should be a factor.

# Sample data
n <- 100
d <- sample( c(TRUE,FALSE), n, replace=TRUE )
d <- data.frame(
  value = ifelse(d, 10, 30 ) + 10 * rnorm(n),
  temp = ifelse(d,0,97)
)

# Make sure temp is a factor
p <- ggplot(d, aes(x=value, fill=factor(temp))) 

p + geom_histogram(position="stack") 
p + geom_histogram(position="dodge") 

Upvotes: 2

Related Questions