2016-10-05 9 views
1

2つの列にヘッダーを展開した表があります。このテーブルに3Dグラフを描く方法、または精巧なヘッダーを持つテーブルにグラフを描画する方法は何でしょうか。2つの列に精巧なヘッダーでRを描画する

year             
       2014    2015    2016    
       Reported Detected Reported Detected Reported Detected 
Murder   221  208  178  172  26  20  
Murder(Gain)  20  16  11  9  1  1  
Dacoity   51  45  44  36  5  1  
Robbery   538  316  351  201  23  10  
Chain Snatching 528  394  342  229  23  0 

コード:親切に私は(もしあれば)これを達成するための別の方法

犯罪表を示唆

library(tables) 
#CLASS 1 CRIMES 2014 
c14 <- structure(list(`Reported` = c(221, 20, 51, 
             538, 528), `Detected` = c(208, 16, 45, 316, 394)), .Names = c("Reported", 
                          "Detected"), row.names = c("Murder", "Murder(Gain)", "Dacoity", "Robbery", "Chain Snatching"), class = "data.frame") 

c14 

#CLASS 1 CRIMES 2015 

c15 <- structure(list(`Reported` = c(178, 11, 44, 
            351, 342), `Detected` = c(172, 9, 
                    36, 201, 229)), .Names = c("Reported", 
                             "Detected"), row.names = c("Murder", "Murder(Gain)", "Dacoity", 
                                   "Robbery", "Chain Snatching"), class = "data.frame") 
c15 

#CLASS 1 CRIMES 31-01-2016 
c16 <- structure(list(`Reported` = c(26, 1, 5, 
            23, 23), `Detected` = c(20, 1, 
                   1, 10, 0)), .Names = c("Reported", 
                          "Detected"), row.names = c("Murder", "Murder(Gain)", "Dacoity", 
                                "Robbery", "Chain Snatching"), class = "data.frame") 
c16 


# rbind with rownames as a column 
st <- rbind(
    data.frame(c14, year = '2014', what = factor(rownames(c14), levels = rownames(c14)), 
      row.names= NULL, check.names = FALSE), 
    data.frame(c15,year = '2015',what = factor(rownames(c15), levels = rownames(c15)), 
      row.names = NULL,check.names = FALSE), 
    data.frame(c16,year = '2016',what = factor(rownames(c16), levels = rownames(c16)), 
      row.names = NULL,check.names = FALSE) 
) 

crimetable <- tabular(Heading()*what ~ year*(`Reported` +`Detected`)*Heading()*(identity),data=st) 
crimetable 

答えて

0

私は3ウェイテーブルの3Dプロットを憎むとし、私はggplot2が好きです。私はこれを提案します:

"ロング"フォーマットにデータを集める:

library(tidyr) 
st_long = gather(st, type, count, -c(year, what)) 
head(st_long, 3) 
# year   what  type count 
# 1 2014  Murder Reported 221 
# 2 2014 Murder(Gain) Reported 20 
# 3 2014  Dacoity Reported 51 

あなたが見ることができるように、両方のDetectedReported列が今typeと呼ばれる同じ列に含まれています。これはファセットを簡単に作成できるので、ggplot2に便利です。ファセットは、同じ美的要素を共有するが、データの異なるグループにと協力プロット内の個別の要素です:

library(ggplot2) 
ggplot(st_long, aes(year, count, group = what, color = what)) + 
    geom_line() + 
    facet_wrap(~ type) 

(私はそのラインプロットを言っていないですが、ここだけ/最良のプロットであり、それは頻繁に使用されていますさまざまな時点での頻度を比較する場合)

+0

ありがとうございました –

関連する問題