如何使用ggplot2生成热图? [英] How to produce a heatmap with ggplot2?

查看:117
本文介绍了如何使用ggplot2生成热图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2生成热图。我找到了这个例子,我基本上是在尝试复制我的数据,但我有困难。我的数据是一个简单的.csv文件,如下所示:

 人,苹果,橙子,桃子
mike, 1,0,6
sue,0,0,1
帐单,3,3,1
特德,1,1,0

我想生成一个简单的热图,其中水果的名称在x轴上,人在y轴上。图表应描绘出每个方块的颜色代表所消耗水果数量的方块。对应于 mike:peach 的平方应该是最黑的。



以下是我用来生成热图的代码:

 <$ c (apple,orange,peach)$ c $ data<  -  read.csv(/ Users / bunsen / Desktop / fruit.txt,head = TRUE,sep =,) 
people < - data [,1]
(p <-ggplot(data,aes(fruit,people))+ geom_tile(aes(fill = rescale),color =white)+ scale_fill_gradient(low =white,high =steelblue))

获取x轴上的水果数量和y轴上的人数。我也没有得到代表水果数量的颜色渐变。我怎样才能得到X轴水果的名称与一个人所吃水果的数量显示为热图?
我在R中获取的当前输出如下所示:

解决方案

老实说@ dr.bunsen - 上面的例子很难再现而且您没有阅读教程的第一部分,您已经链接。这可能是你在找什么:

  library(reshape)
library(ggplot2)
library (比例)

data < - 结构(列表(人员=结构(c(2L,3L,1L,4L),
。标签= c(账单,麦克 (1L,0L,3L,1L),
orange = c(0L,0L,0L,3L,1L),
class = 3L,1L),
peach = c(6L,1L,1L,0L)),
.Names = c(people,apple,orange,peach),
class =data.frame,
row.names = c(NA,-4L))
data.m < - melt(data)
data.m< - ddply(data.m,。(variable),transform,rescale = rescale(value))
p < - ggplot(data.m,aes(variable,people))+
geom_tile(aes fill = rescale),color =white)
p + scale_fill_gradient(low =white,high =s teelblue)


I am trying to produce a heat map using ggplot2. I found this example, which I am essentially trying to replicate with my data, but I am having difficulty. My data is a simple .csv file that looks like this:

people,apple,orange,peach
mike,1,0,6
sue,0,0,1
bill,3,3,1
ted,1,1,0

I would like to produce a simple heat map where the name of the fruit is on the x-axis and the person is on the y-axis. The graph should depict squares where the color of each square is a representation of the number of fruit consumed. The square corresponding to mike:peach should be the darkest.

Here is the code I am using to try to produce the heatmap:

data <- read.csv("/Users/bunsen/Desktop/fruit.txt", head=TRUE, sep=",")
fruit <- c(apple,orange,peach)
people <- data[,1]
(p <- ggplot(data, aes(fruit, people)) + geom_tile(aes(fill = rescale), colour = "white") +    scale_fill_gradient(low = "white", high = "steelblue"))

When I plot this data I get the number of fruit on the x-axis and people on the y-axis. I also do not get color gradients representing number of fruit. How can I get the names of the fruits on the x-axis with the number of fruit eaten by a person displayed as a heat map? The current output I am getting in R looks like this:

解决方案

To be honest @dr.bunsen - your example above was poorly reproducable and you didn't read the first part of the tutorial that you linked. Here is probably what you are looking for:

 library(reshape)
 library(ggplot2)
 library(scales)

 data <- structure(list(people = structure(c(2L, 3L, 1L, 4L), 
                                           .Label = c("bill", "mike", "sue", "ted"), 
                                           class = "factor"), 
                        apple = c(1L, 0L, 3L, 1L), 
                        orange = c(0L, 0L, 3L, 1L), 
                        peach = c(6L, 1L, 1L, 0L)), 
                    .Names = c("people", "apple", "orange", "peach"),
                    class = "data.frame", 
                    row.names = c(NA, -4L))
 data.m <- melt(data)
 data.m <- ddply(data.m, .(variable), transform, rescale = rescale(value))
 p <- ggplot(data.m, aes(variable, people)) + 
         geom_tile(aes(fill = rescale), colour = "white") 
 p + scale_fill_gradient(low = "white", high = "steelblue")

这篇关于如何使用ggplot2生成热图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆