尝试在 ggplot2 "Aesthetics must be either length 1 or the same as the data (12): x" 中创建 2 x 12 热图
Trying to create a 2 x 12 heatmap in ggplot2 "Aesthetics must be either length 1 or the same as the data (12): x"
我正在尝试使用 geom_tile 和 R 中的 ggplot2 创建一个 2 列 12 行的热图。
我尝试了多种在 aes() 中设置轴的方法
db <- read.csv("db.csv")
ggplot(db, aes(x=colnames(db), y=rownames(db))) +
geom_tile()
或
ggplot(db, aes(x=db[,2:3], y=rownames(db))) +
geom_tile()
我收到以下错误:
“美学必须是长度为1或与数据相同(12):x”
这就像它试图强迫我有一个方形热图,我不确定如何规避它。我应该重塑我的 .csv 文件吗?
这是我的 .csv:
enter image description here
是 - 您需要重塑数据,并将这些值用作 fill
参数。
library(tidyr)
library(ggplot2)
pivot_longer(df, 2:3) %>%
ggplot(aes(name, GN, fill=value)) +
geom_tile()
数据:(模拟)
df <- structure(list(GN = c("a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l"), plus = c(0.586, -0.988, -0.6246, 0.3637,
-0.2598, -0.2767, 0.7376, 0.8083, 0.2348, -0.7319, 0.5644, -0.1416
), minus = c(0.8545, 0.5465, -0.4806, -0.3576, -0.8796, -0.9131,
-0.8899, 0.2511, 0.9289, 0.6546, -0.3699, -0.5739)), class = "data.frame", row.names = c(NA,
-12L))
我正在尝试使用 geom_tile 和 R 中的 ggplot2 创建一个 2 列 12 行的热图。
我尝试了多种在 aes() 中设置轴的方法
db <- read.csv("db.csv")
ggplot(db, aes(x=colnames(db), y=rownames(db))) +
geom_tile()
或
ggplot(db, aes(x=db[,2:3], y=rownames(db))) +
geom_tile()
我收到以下错误: “美学必须是长度为1或与数据相同(12):x”
这就像它试图强迫我有一个方形热图,我不确定如何规避它。我应该重塑我的 .csv 文件吗?
这是我的 .csv:
enter image description here
是 - 您需要重塑数据,并将这些值用作 fill
参数。
library(tidyr)
library(ggplot2)
pivot_longer(df, 2:3) %>%
ggplot(aes(name, GN, fill=value)) +
geom_tile()
数据:(模拟)
df <- structure(list(GN = c("a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l"), plus = c(0.586, -0.988, -0.6246, 0.3637,
-0.2598, -0.2767, 0.7376, 0.8083, 0.2348, -0.7319, 0.5644, -0.1416
), minus = c(0.8545, 0.5465, -0.4806, -0.3576, -0.8796, -0.9131,
-0.8899, 0.2511, 0.9289, 0.6546, -0.3699, -0.5739)), class = "data.frame", row.names = c(NA,
-12L))