Now I have a column of data, which is over 500 rows.
#example
df <- data.frame(
City = c("New York", "LA", "DC", "Boston", "Chicago"),
Data = c(780, 982, 111, 893, 989)
I want to build a heatmap but i only have one variable, which is City. Can I split this column into multiple columns and create the heatmap.
I am new to R and I can't find any answers online.
Thanks in advance!
The trick is to duplicate the single column with cbind and plot...
library(gplots)
df <- data.frame(
City = c("New York", "LA", "DC", "Boston", "Chicago"),
Data = c(780, 982, 111, 893, 989)
)
heatmap.2(cbind(df$Data, df$Data), trace="n", Colv = NA,
dendrogram = "row", labCol = "", labRow = df$City, cexRow = 0.75)
Note that I turned of the dendrograms completely as any ordering would be meaningless.
heatmap.2(matrix(df$Data, ncol = 2), trace="n", Colv = FALSE, Rowv=FALSE,
cellnote = matrix(df$City, ncol = 2), notecol = 1, scale = "none",
dendrogram = "none", labCol = "", labRow = "", cexRow = 0.75)
labRow
: sets row labels for (use ""
for no label)cellnote
: sets the cell label (delete this parameter if you do want cell labels)col
: sets the color palette. My suggestion is to use a pre-defined palette from colorspace
, RColorBrewer
or colorRamps
.
Some suggestions:
col=col=colorspace::sequential_hcl(15)
col=col=RColorBrewer::brewer.pal(9, "BuGn"
col=colorRamps::ygobb(25)
See this link for more on color palettes in R
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With