(使用dec=","
后作为 Ben 的建议)首先我们可以将数据转换为宽格式,然后用于image
绘图。
# The continuous variable is on the left-hand side of the formula
# You can control which categorical variable is on the x-axis
# by changing the order of the variables in the right-hand side of
# the formula, or else transpose the matrix with `t`
trans_df <- xtabs(land ~ basin + parameter, df)
image(trans_df)
重新编辑:您可以使用this向图像图添加值。我使用该fields::imagePlot
功能,因为它是添加颜色条的简单方法。
trans_df <- xtabs(land ~ basin + parameter, df)
# parameter
# basin COD N OD ph
# 1 1.37 1.85 1.25 3.02
# 2 2.02 1.59 2.96 1.71
# 3 2.81 1.43 1.56 2.42
library(fields) # for the easy heatmap
# Transpose matrix so that it plots in the same order as
# it looks in the table above
imagePlot( t(trans_df[nrow(trans_df):1, ]), axes=FALSE)
# add values in cells
e <- expand.grid(seq(0,1, length=ncol(trans_df)), seq(1,0, length=nrow(trans_df)))
text(e, labels=t(trans_df), cex=2, col="black")
# axis labels
axis(3, at=seq(0,1, length=ncol(trans_df)), labels=colnames(trans_df), cex=2)
axis(2, at=seq(1,0, length=nrow(trans_df)), labels=rownames(trans_df), cex=2)

实际上,使用它可能更容易corrplot
:
library(corrplot)
corrplot(trans_df, is.corr = FALSE, method = "color", addCoef.col = 'black')
