如何将 R Studio 3 变量数据框绘制成热图?

问题描述 投票:0回答:1

我目前正在 R 中做一个项目,并有这个数据框:

lrdf
    nseg  meanlen        loglr
1     27 16.64982  2.163818549
2     18 15.49226  0.524823313
3     22 23.85373  0.570587756

我想在 R Studio 中创建热图(或二维密度图)。我希望 nseg 在 x 轴上,meanlen 在 y 轴上,loglr 是填充热图的 z 值。

我读到首先必须将数据帧从宽格式转换为长格式。所以我这样做了:

#Load dplyr package for data manipulation
library(dplyr)

#Load tidyr package (pivot_longer is from tidyr)
library(tidyr)

#Convert lrdf to a longer format
lrdf_long <- lrdf %>%
  #Pivot the specified column (loglr) to a long format
  pivot_longer(
    cols = c(loglr),           # Column(s) to pivot (in this case, only loglr)
    names_to = "variable",      # Name of the new column for the original column names
    values_to = "loglr"         # Name of the new column for the values from loglr
  )

这给了我这个:

lrdf_long
# A tibble: 10,000 × 4
    nseg meanlen variable loglr
   <int>   <dbl> <chr>    <dbl>
 1    27    16.6 loglr     2.16
 2    18    15.5 loglr     0.52
 3    22    23.9 loglr     0.57

现在,使用 ggplot 创建热图,我这样做了:

# Load ggplot2 package for data visualization
library(ggplot2)

# Load viridis package for color scales
library(viridis)

# Create a heatmap using ggplot2
ggplot(lrdf_long, aes(x = nseg, y = meanlen, fill = loglr)) +
  # Add a tile layer to create a heatmap based on loglr values
  geom_tile() + 
  
  # Use color scale from the viridis package
  scale_fill_viridis_c() +
  
  # Add plot labels
  labs(
    title = "Heatmap of loglr", # Title of the plot
    x = "nseg",                 # Label for the x-axis
    y = "meanlen"               # Label for the y-axis
  ) +
 

不过,这段代码给了我一个空的情节。我是否错误地转换了数据框?

有谁可以帮忙解决这个问题吗?

r dataframe ggplot2 heatmap
1个回答
0
投票

这会起作用:

library(ggplot2)

lrdf <- tibble::tribble(
  ~nseg, ~meanlen, ~loglr,
  27, 16.64982,  2.163818549,
  18, 15.49226,  0.524823313,
  22, 23.85373,  0.570587756
)

ggplot(lrdf, aes(x = nseg, y = meanlen, fill = loglr)) +
  geom_tile()+
  scale_fill_viridis_c()
© www.soinside.com 2019 - 2024. All rights reserved.