2014-01-21 29 views
0

我策劃這樣的數據:令人失望的結果顯示ggplot箱線圖

Day,Property,Violent 
Mon,7.2,5.7 
Tue,5,4.5 
Wed,6.3,3.6 
Thu,5.4,4 
Fri,9.5,5.6 
Sat,16,10.9 
Sun,14.2,8.6 

用下面的代碼:

library(ggplot2) 
library(reshape) 
week <- read.csv("week.csv", header=TRUE) 
data.melt <- melt(week,id="Day") 

ggplot() + 
geom_boxplot(aes(x=Day, y= value, fill= variable), 
      data= data.melt, position = position_dodge(width = .9)) 
  1. 我的標誌爲什麼出現在傳說中,但不是在陰謀?
  2. 我怎樣才能從週一開始在邏輯上重新排序星期幾? 任何幫助將不勝感激

回答

1
DF <- read.table(text="Day,Property,Violent 
Mon,7.2,5.7 
Tue,5,4.5 
Wed,6.3,3.6 
Thu,5.4,4 
Fri,9.5,5.6 
Sat,16,10.9 
Sun,14.2,8.6", header=TRUE, sep=",") 

#I would consider the weekdays ordered, so let's turn them into an ordered factor. 
DF$Day <- ordered(as.character(DF$Day), as.character(DF$Day)) 

library(ggplot2) 
library(reshape2) 
data.melt <- melt(DF,id.vars="Day") 

ggplot() + 
    geom_boxplot(aes(x=Day, y= value, fill= variable), 
       data= data.melt, position = position_dodge(width = .9)) 

enter image description here

這一切正常。你看不到太多,因爲你每盒只有一個值。如果你想實際看到顏色,你需要每天更多的價值和變數。另外,您也可以使用geom_point

ggplot() + 
    geom_point(aes(x=Day, y= value, colour= variable), 
       data= data.melt, position = position_dodge(width = .9)) 

enter image description here