R:如何生成x对多个离散y的xy图

时间:2016-06-08 20:02:27

标签: r plot boxplot

我现在有这样的数据框。

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

我想生成一个二维图,x轴显示不同的测量值,y轴显示实际测量值。

我怎样才能实现它? 我怎样才能生成箱线图?

1 个答案:

答案 0 :(得分: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

跟进

  

如何绘制点图而不是boxplot?

我们可以简单地使用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