1

我有来自土地利用的 19 个子流域的十个水质参数的 data.frame。

我想以 19x10 图形矩阵的形式呈现这些数据。垂直的子流域和水平的质量参数。

我将无法在此处发送此 data.frame,但我举了一个类似的示例。

编辑:

df<-read.table(text="parameter  basin   land
ph  1   3.02
COD 1   1.37
OD  1   1.25
N   1   1.85
ph  2   1,71
COD 2   2.02
OD  2   2.96
N   2   1.59
ph  3   2.42
COD 3   2.81
OD  3   1.56
N   3   1.43
", sep = "", header = TRUE)

View(df)

我试过了as.matrixplot但没有用

df2<-df%>%
  as.matrix()
plot(df2)

我编辑了期望,因为我没有正确表达自己。我用transreader做一些事情。

在此处输入图像描述

4

2 回答 2

2

一种方式ggplot2

library(ggplot2)

df %>% 
  ggplot(aes(x = basin,y = parameter,fill = land))+
  geom_tile(col = "white")+
  scale_fill_viridis_b()+
  scale_x_continuous(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0))

在此处输入图像描述

于 2021-09-23T19:15:59.090 回答
1

(使用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')

在此处输入图像描述

于 2021-09-23T19:24:22.953 回答