2015-01-09 110 views
-5

下面的代碼是從Udacity的簡介統計Python的條形圖等效中的R

#Write a line of code to produce a barchart of Weight by groups of Height 
#using the barchart function 

from plotting import * 

Height=[65.78, 71.52, 69.4, 68.22, 67.79, 68.7, 69.8, 70.01, 67.9, 66.78, 
66.49, 67.62, 68.3, 67.12, 68.28, 71.09, 66.46, 68.65, 71.23, 67.13, 67.83, 
68.88, 63.48, 68.42, 67.63, 67.21, 70.84, 67.49, 66.53, 65.44, 69.52, 65.81, 
67.82, 70.6, 71.8, 69.21, 66.8, 67.66, 67.81, 64.05, 68.57, 65.18, 69.66, 67.97, 
65.98, 68.67, 66.88, 67.7, 69.82, 69.09] 

Weight=[112.99, 136.49, 153.03, 142.34, 144.3, 123.3, 141.49, 136.46, 
112.37, 120.67, 127.45, 114.14, 125.61, 122.46, 116.09, 140.0, 129.5, 142.97, 
137.9, 124.04, 141.28, 143.54, 97.9, 129.5, 141.85, 129.72, 142.42, 131.55, 
108.33, 113.89, 103.3, 120.75, 125.79, 136.22, 140.1, 128.75, 141.8, 121.23, 
131.35, 106.71, 124.36, 124.86, 139.67, 137.37, 106.45, 128.76, 145.68, 116.82, 
143.62, 134.93] 

barchart(Height, Weight) 

下面是輸出

enter image description here

執行此代碼返回一個條形圖展示高度的VS的線性關係重量。

R中有什麼等價的嗎?

謝謝。

+1

您是否期望我們運行Python來查看條形圖的外觀?順便說一句。一個barplot可能不是說明線性關係的最佳選擇。另外:http://www.statmethods.net/graphs/bar.html – Roland

回答

2

我寧願做一個簡單的散點圖:

Height=c(65.78, 71.52, 69.4, 68.22, 67.79, 68.7, 69.8, 70.01, 67.9, 66.78, 
     66.49, 67.62, 68.3, 67.12, 68.28, 71.09, 66.46, 68.65, 71.23, 67.13, 67.83, 
     68.88, 63.48, 68.42, 67.63, 67.21, 70.84, 67.49, 66.53, 65.44, 69.52, 65.81, 
     67.82, 70.6, 71.8, 69.21, 66.8, 67.66, 67.81, 64.05, 68.57, 65.18, 69.66, 67.97, 
     65.98, 68.67, 66.88, 67.7, 69.82, 69.09) 

Weight=c(112.99, 136.49, 153.03, 142.34, 144.3, 123.3, 141.49, 136.46, 
     112.37, 120.67, 127.45, 114.14, 125.61, 122.46, 116.09, 140.0, 129.5, 142.97, 
     137.9, 124.04, 141.28, 143.54, 97.9, 129.5, 141.85, 129.72, 142.42, 131.55, 
     108.33, 113.89, 103.3, 120.75, 125.79, 136.22, 140.1, 128.75, 141.8, 121.23, 
     131.35, 106.71, 124.36, 124.86, 139.67, 137.37, 106.45, 128.76, 145.68, 116.82, 
     143.62, 134.93) 

plot(Height, Weight) 

enter image description here

但是你可以玩的選項type有另一種類型的圖。例如:

plot(Height, Weight, type="h") 

編輯:用例圖,這個問題也許是更清晰。

評論:我認爲這是浪費了不少信息通過這樣做,它不會在兩個變量之間的所有線性鏈接說明以及...

這是一個命題:我假設有序因子是基於身高的,並且它被用來聚合權重(通過計算身高屬於某個箱子的個人體重的平均值)。

  1. 首先創建變量Height有序因素:

    f <- cut(Height, 5, ordered_result = TRUE) 
    
  2. 在此基礎上有序的因素,總的權重:

    y <- tapply(Weight, f, mean) 
    
  3. 和情節:

    barplot(y, col="steelblue", border=NA, xlab="Height", ylab="Weight") 
    

enter image description here

+0

我的思路是在高度上創建一個有序的因子變量,將其分組爲5箱,然後繪製該變量。 – chribonn

+0

@chribonn在這種情況下,你正在尋找一個直方圖。見'?hist'。 –