1
votes

I have a data frame with 5 column names and 10 row names. Values in this data frame consist of numbers between -1 to 1 and NAs.

                Comp.1 Comp.2 Comp.3 Comp.4    Comp.5
Param1              NA     NA     NA     NA        NA
Param2              NA     NA     NA     NA 0.9035769
Param3       0.9810636     NA     NA     NA        NA
Param4              NA     NA     NA     NA        NA
Param5              NA     NA     NA     NA        NA
...
Param10              NA     NA     NA     NA        NA

I want to plot a graph, where I have all rows with row names at Y-Axis and columns with column names at X-Axis. Also, values denote points of certain color and the shade of this color determines value being closer to 1 or -1. NAs can be a cross mark or can be ignored.

Thanks in advance!

2

2 Answers

2
votes

I guess I am not entirely sure what you're looking for but I suppose one way of doing this would be :

library(reshape2)
df$row.names<-rownames(df)
long.df<-melt(df,id=c("row.names"))
plotted<-ggplot(long.df,aes(x=row.names,y=variable,color=value))+geom_point()

Note:: If you have multiple combinations of row & column values then this will result in over-plotting. This can either be handled by geom_jitter() or via facet_wrap() / facet_grid()

Note 2 :: If you want more of a heatmap output per Yang Li's answer & don't have overlapping data you would use either geom_tiles() or geom_raster() in the place of geom_point()

1
votes

If I understand you correctly you basically want a heatmap. In that case, your question might be a duplicate, but I'll show you some ways to do heatmaps based on the specific data frame you have.

Suppose this is your data, I'll reduce the number of rows and columns for simplicity:

df <- data.frame(Comp.1 = c(0.3, -0.2, -1, NA, 1),
             Comp.2 = c(-0.4, -0.1, NA, 0, 0.6),
             Comp.3 = c(0.2, NA, -0.4, 0.3, NA))
row.names(df) <- c("Param1", "Param2", "Param3", "Param4", "Param5")

Base R

First, convert to a matrix:

df_matrix <- data.matrix(df)

Next, you can use the heatmap() function like so:

heatmap(df_matrix, Rowv=NA, Colv=NA, col = cm.colors(256), scale="column", margins=c(5,10))

                                                   heatmap1

You can change the colors you use with the col parameter. There are other built in palettes like rainbow(), heat.colors(), topo.colors(), but it still won't look that great, so you should try other packages, for example...

Plotly package

Install the plotly package, then use this code:

library(plotly)

plot_ly(
    x = names(df), y = row.names(df),
    z = df_matrix, type = "heatmap"
)

                                              heatmap2

And, if you want to customise your colors, just add a colors = <Insert your color range here> as an extra parameter. This heatmap is also interactive.