Home > front end >  How to convert a dataframe into an plot visual array matrix in R?
How to convert a dataframe into an plot visual array matrix in R?

Time:09-24

I have the data.frame with ten water quality parameters from 19 sub-basins from land use.

I would like to present these data in the form of a 19x10 graphical matrix. Vertically the sub-basin and horizontally the quality parameters.

I won't be able to send this data.frame here, but I put a similar example.

EDIT:

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)

i tried as.matrix and plot but it didn't work

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

I edited the expectation, because I didn't express myself correctly. I use the transreader for a few things.

enter image description here

CodePudding user response:

A way with 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))

enter image description here

CodePudding user response:

(Afer using dec="," as Ben suggets) first we can transform the data to a wide format and then use image to plot.

# 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)

Re your edit: you can add values to an image plot using enter image description here


Actually it may be easier just to use corrplot:

library(corrplot)
corrplot(trans_df, is.corr = FALSE, method = "color",  addCoef.col = 'black')

enter image description here

  • Related