df.team_data <- expand.grid(teams = c("Team A", "Team B", "Team C", "Team D")
,metrics = c("Metric 1", "Metric 2", "Metric 3", "Metric 4", "Metric 5")
)
set.seed(41)
df.team_data$performance <- sample(c(0, 1), 20, replace = TRUE)
head(df.team_data)
ggplot(data = df.team_data, aes(x = metrics, y = teams)) +
geom_tile(aes(fill = performance))
我有一个非常简单的热图,只有两种颜色。如何指定哪些颜色与performance
的值相关联?为此,我希望1的性能为深蓝色,0的性能为浅蓝色。还有,有办法将图例更改为二进制文件吗?
答案 0 :(得分:2)
&#34;表现中的价值&#34;您的数据集的列位于&#34;数字&#34;格式。
使用factor()
将其转换为系数,您的图形将包含二进制图例:
df.team_data <- expand.grid(teams = c("Team A", "Team B", "Team C", "Team D")
,metrics = c("Metric 1", "Metric 2", "Metric 3", "Metric 4", "Metric 5")
)
set.seed(41)
df.team_data$performance <- sample(c(0, 1), 20, replace = TRUE)
df.team_data$performance<-factor(df.team_data$performance)
head(df.team_data)
library(ggplot2)
ggplot(data = df.team_data, aes(x = metrics, y = teams)) +
geom_tile(aes(fill = performance))
col.plot<-c('lightblue','darkblue')
ggplot(data = df.team_data, aes(x = metrics, y = teams)) +
geom_tile(aes(fill = performance))+scale_fill_manual(values=col.plot)
根据您的需要更改col.plot
中的值
答案 1 :(得分:2)
如果你使用scale_fill_gradient()
并设置了关于高值和低值颜色的几个选项,我想你可以得到你想要的东西。您还可以使用breaks ()
设置性能变量中的中断。
使用您的代码我获得以下内容:
df.team_data <- expand.grid(teams = c("Team A", "Team B", "Team C", "Team D")
,metrics = c("Metric 1", "Metric 2", "Metric 3", "Metric 4", "Metric 5")
)
df.team_data$performance <- sample(c(0, 1), 20, replace = TRUE)
head(df.team_data)
ggplot(data = df.team_data, aes(x = metrics, y = teams)) +
geom_tile(aes(fill = performance)) + scale_fill_gradient('performance', limits=c(0, 1), breaks = c(0, 0.25, 0.5, 0.75, 1), low = "lightblue", high = "darkblue")