2016-06-08 63 views
0

我現在有一個這樣的數據框。R:如何生成一個具有多個離散y的xy圖x

df <-data.frame("networkNO"=c(1:length(dg)),"AverageDegree"=average_degree,"AverageBetweenness"=average_betweenness,"AverageCloseness"=average_closeness,"ClusterCoefficient"=cluster_coefficient) 

所以每一列都顯示了一系列網絡的度量。

 networkNO AverageDegree AverageBetweenness AverageCloseness ClusterCoefficient 
1   1  10.804124   300.453608  0.0012898154   0.4388075 
2   2  10.785714   31.660714  0.0085438562   0.4646219 
3   3  10.909091   52.688312  0.0055827873   0.4440915 
4   4  10.000000   19.435897  0.0131519596   0.5078864 
5   5  11.372014  1348.049488  0.0003100285   0.4193862 
6   6  8.736842   66.210526  0.0054046865   0.5077356 
7   7  1.000000   0.000000  1.0000000000    NaN 
8   8  7.755102   49.346939  0.0070593456   0.5193906 
9   9  9.000000   6.363636  0.0298526499   0.5279429 
10  10  7.538462   2.230769  0.0611896445   0.6666667 
11  11  7.297297   34.027027  0.0099660321   0.5391566 
12  12  1.000000   0.000000  1.0000000000    NaN 
13  13  6.666667   20.111111  0.0156903046   0.5445378 
14  14  3.000000   0.000000  0.3333333333   1.0000000 
15  15  9.658537   21.341463  0.0122712462   0.4870849 
16  16  7.100000   8.050000  0.0290803614   0.5692964 

我想生成一個2維繪圖,其中x軸顯示不同的測量值,y軸顯示實際測量值。

我該如何實現它? 我怎樣才能生成一個boxplot?

+0

我不知道我知道你想什麼陰謀。 「不同測量」和「實際測量」的含義 – ZachTurn

+0

測量意味着AverageDegree,AverageBetweenness等.Y軸顯示其離散值 –

+0

由於尺度不同(0-13000,0-1),很難做到這一點在一個情節。您能否按照該列中的最大值重新縮放每個測量值? – timcdlucas

回答

0

這應該是你想要什麼:

boxplot(df[-1]) ## exclude column `networkNO` 

enter image description here

但是,你必須重新調整數據。目前,這些列在規模上差別很大,將它們直接繪製在箱形圖上(如上圖)是一個不好的主意。

下面的代碼將重新調整你的列和更新df

df1 <- within(df, {AverageDegree = scale(AverageDegree); 
        AverageBetweenness = scale(AverageBetweenness); 
        AverageCloseness = scale(AverageBetweenness); 
        ClusterCoefficient = scale(ClusterCoefficient);}) 

boxplot(df1[-1]) ## exclude column `networkNO` 

enter image description here

scale的默認方法(見?scale)第一中心減去平均數據,然後通過標準差除以數據。您可能會認爲這是否是您想要的,因爲在重新縮放之後,y-axis上的值的含義稍有不同。

如果您不想要任何比例縮放,那麼您可能需要考慮爲每個列繪製單獨的盒形圖,並將它們排列在同一個面板中。下面做到這一點:

par(mfrow = c(2,2)) 
boxplot(df$AverageDegree, xlab = "AverageDegree") 
boxplot(df$AverageBetweenness, xlab = "AverageBetweenness") 
boxplot(df$AverageCloseness, xlab = "AverageCloseness") 
boxplot(df$ClusterCoefficient, xlab = "ClusterCoefficient") 

enter image description here

注意boxplot()有一個特殊的參數outline。通過設置outline = FALSE,異常值(非常大的數據)將被刪除。您可以比較:

par(mfrow = c(2,2)) 
boxplot(df$AverageDegree, xlab = "AverageDegree", outline = FALSE) 
boxplot(df$AverageBetweenness, xlab = "AverageBetweenness", outline = FALSE) 
boxplot(df$AverageCloseness, xlab = "AverageCloseness", outline = FALSE) 
boxplot(df$ClusterCoefficient, xlab = "ClusterCoefficient", outline = FALSE) 

enter image description here

跟進

我怎樣才能畫出散點圖,而不是箱線圖?

我們可以簡單地使用plot()

par(mfrow = c(2,2)) 
plot(rep(1, nrow(df)), df$AverageDegree, xlab = "AverageDegree", xaxt = "n") 
plot(rep(1, nrow(df)), df$AverageBetweenness, xlab = "AverageBetweenness", xaxt = "n") 
plot(rep(1, nrow(df)), df$AverageCloseness, xlab = "AverageCloseness", xaxt = "n") 
plot(rep(1, nrow(df)), df$ClusterCoefficient, xlab = "ClusterCoefficient", xaxt = "n") 

enter image description here

也許你也有興趣在做柱狀圖?

par(mfrow = c(2,2)) 
hist(df$AverageDegree, main = "AverageDegree", xlab = "") 
hist(df$AverageBetweenness, main = "AverageBetweenness", xlab = "") 
hist(df$AverageCloseness, main = "AverageCloseness", xlab = "") 
hist(df$ClusterCoefficient, main = "ClusterCoefficient", xlab = "") 

enter image description here

+0

非常感謝!我如何繪製點圖而不是boxplot?以平均程度爲x軸,值爲y軸? –