简体   繁体   中英

How to change range of heatmap using gheatmap in R

Im trying to add a heatmap to my phylogenetic tree. The range of the heatmap should be from 0 to 100 instead it only covers the the min and max of the values. Can I reset the range of the heatmap?

thanks.

library(ggtree)
library(ggplot2)
library(ggstance)

df1 <- structure(
  list(id = structure(
    c(5L, 15L, 29L, 18L, 24L, 21L, 
      13L, 11L, 8L, 25L, 23L, 9L, 16L, 3L, 6L, 2L, 20L, 27L, 30L, 17L, 
      14L, 4L, 1L, 7L, 22L, 28L, 10L, 12L, 26L, 19L), 
    .Label = c("t1", 
               "t10", "t11", "t12", "t13", "t14", "t15", "t16", "t17", "t18", 
               "t19", "t2", "t20", "t21", "t22", "t23", "t24", "t25", "t26", 
               "t27", "t28", "t29", "t3", "t30", "t4", "t5", "t6", "t7", "t8", 
               "t9"), class = "factor"), 
    location = structure(c(1L, 3L, 2L, 
                           1L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 2L, 1L, 1L, 3L, 2L, 1L, 
                           1L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L), 
                         .Label = c("CZ", "GZ", "HK"), class = "factor"), 
    Value = c(22L, 10L, 33L, 12L, NA, 
              NA, NA, NA, NA, NA, NA, NA, 45L, 89L, NA, NA, NA, NA, NA, NA, 
              NA, NA, NA, NA, NA, 80L, NA, NA, NA, NA)), 
  class = "data.frame", row.names = c(NA, 
                                      -30L))

tr <- rtree(30)
p <- ggtree(tr)
#df1<- your_example_data
p1 <- p %<+% df1 + geom_tippoint(aes(color=location))+  guides(color = "none")
d2 <- data.frame( val=rnorm(30, mean= 50, sd=20))
rownames(d2)<- tr$tip.label

library(ggnewscale)

p1 <- p1 + new_scale_fill() 

p2<- gheatmap(p1, d2 ,offset=0.015, width=0.05,
              colnames_angle=45, colnames_offset_y = 0.25,colnames_offset_x =0.001, colnames=TRUE,
              colnames_position='top',font.size = 3)+
  scale_fill_viridis_c(option="A", name="query\ncoverage\npercentage")


p2

在此处输入图像描述

Try using scale_fill_gradientn . I don't have ggtree in my library collection, but it should work with it too. data$Z are the values used in the legend.

min(data$Z)
[1] 10.43507

# using geom_tile instead
ggplot(data, aes(X, Y, fill= Z)) + 
  geom_tile() + 
  scale_fill_gradientn(limits = c(0,max(data$Z)), 
    colours=viridis(10,o="A"), 
    breaks=c(0,max(data$Z)), 
    labels=c(0,max(data$Z)))

ggplot2的热图

Data

data <- structure(list(X = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L), .Label = c("A", "B", "C", "D", "E", "F", "G", "H", 
"I", "J"), class = "factor"), Y = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L), .Label = c("var1", "var2", "var3", "var4", "var5", 
"var6", "var7", "var8", "var9", "var10"), class = "factor"), 
    Z = c(33.991562910378, 35.5263787321746, 24.5632742531598, 
    18.0254957079887, 45.778294513002, 38.0070783570409, 38.8778781332076, 
    13.9182714093477, 13.2864724285901, 12.3245238792151, 45.4634746629745, 
    43.5207717958838, 14.6174691990018, 14.6395265311003, 16.3748204801232, 
    37.5898649636656, 46.154183940962, 21.7670671269298, 45.9928634669632, 
    15.2300526481122, 42.3459290526807, 36.1509132292122, 13.004608694464, 
    17.2632187511772, 24.1008642502129, 21.0504860430956, 47.8412099648267, 
    22.8905160259455, 26.2689692527056, 42.2642367053777, 49.7228981740773, 
    18.5286565497518, 19.9640860501677, 19.8192273359746, 46.2587429210544, 
    45.3112288471311, 14.0251182205975, 46.5721819829196, 19.2603973485529, 
    11.8241156637669, 43.5814412590116, 12.3338401783258, 34.6708638872951, 
    16.535308547318, 12.5870429351926, 17.7716215513647, 38.3571200724691, 
    40.5572446156293, 38.3018106594682, 36.1261784471571, 23.6329158209264, 
    38.2715854980052, 31.8956978339702, 19.8036628682166, 41.236245688051, 
    42.5284101255238, 47.3572976142168, 10.9305525757372, 41.5727174282074, 
    39.237065333873, 41.6476187948138, 43.6902561411262, 39.2061061505228, 
    18.3187866955996, 42.8791201952845, 33.8544269837439, 17.3525733780116, 
    14.5423825085163, 46.209614733234, 24.5643785689026, 35.3784507885575, 
    44.3101883865893, 45.7905176281929, 36.0531417001039, 44.190902383998, 
    32.4274326208979, 33.8546730671078, 43.7150628026575, 44.4308217708021, 
    27.6862936094403, 39.8551124054939, 10.4350713547319, 35.6894047465175, 
    28.6168400477618, 18.5768875014037, 17.1367645263672, 30.369380293414, 
    17.7864238992333, 36.1986118741333, 43.2466325163841, 49.581032032147, 
    49.736803509295, 40.3205085452646, 27.0655540842563, 42.9749015253037, 
    30.9310132544488, 23.7332978192717, 35.1737863756716, 40.4224442131817, 
    15.6103290617466)), out.attrs = list(dim = c(X = 10L, Y = 10L
), dimnames = list(X = c("X=A", "X=B", "X=C", "X=D", "X=E", "X=F", 
"X=G", "X=H", "X=I", "X=J"), Y = c("Y=var1", "Y=var2", "Y=var3", 
"Y=var4", "Y=var5", "Y=var6", "Y=var7", "Y=var8", "Y=var9", "Y=var10"
))), row.names = c(NA, -100L), class = "data.frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM