2012-02-22 81 views
0

我想下面生成數據的直方圖躲着(數據來自SQLSERVER數據庫)在R語言

> 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 

我試圖qplot在ggplot與躲避。我希望我會得到多個直方圖,但我得到一個直方圖enter image description here

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

爲了驗證我有數據的兩個不同的溫度,我生成的臨時

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

enter image description here

的直方圖

我也生成了一個值的直方圖,它與上面的第一個圖相同。 爲了驗證我的命令,我產生了一些樣本數據和我使用的命令的圖形似乎是確定

> 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

請幫我看看這個問題

回答

2

變量用於定義兩組應該是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") 
+0

非常感謝,非常感謝。我將嘗試在文檔中找到更多關於這些因素的信息 – SAN 2012-02-22 06:17:26