R热图新手:绘图对计数

R热图新手:绘图对计数,r,plot,heatmap,R,Plot,Heatmap,我需要创建一个热图,显示不同的charCount,wordCount对计数,其中charCount是字符数,wordCount是文本摘要中的单词数。 作为输入,我有一个csv文件,其中每行有两个数字:charCount和wordCount。我可以将其读入矩阵,然后绘制热图: data = read.csv("chars_words.txt", sep=",", header=FALSE) mtx <- as.matrix(data) heatmap(mtx) 我只有两种颜色的图。如何创建

我需要创建一个热图,显示不同的charCount,wordCount对计数,其中charCount是字符数,wordCount是文本摘要中的单词数。 作为输入,我有一个csv文件,其中每行有两个数字:charCount和wordCount。我可以将其读入矩阵,然后绘制热图:

data = read.csv("chars_words.txt", sep=",", header=FALSE)
mtx <- as.matrix(data)
heatmap(mtx)
我只有两种颜色的图。如何创建显示不同字符数、字数对计数和颜色的绘图

***更新2:

来自的代码解决了我的问题:

library(ggplot2)
library(dplyr)

# Convert your data into frequency matrix, then data.frame
df<-data.frame(table(data))

# Set columns ready for ggplot
df$Freq<-as.factor(df$Freq)
df$charCount<-as.character(df$charCount) %>% as.numeric()
df$wordCount<-as.character(df$wordCount) %>% as.numeric()

# plot using ggplot
ggplot(df,aes(x=charCount,y=wordCount)) +
geom_tile(aes(fill=Freq)) +
geom_text(aes(label=Freq))
这将生成一个很好的绘图示例数据,而不是来自原始任务:

更新

不确定如何使用热图功能,但此ggplot方法可能适合您:

library(ggplot2)
library(dplyr)

# Convert your data into frequency matrix, then data.frame
df<-data.frame(table(data))

# Set columns ready for ggplot
df$Freq<-as.factor(df$Freq)
df$charCount<-as.character(df$charCount) %>% as.numeric()
df$wordCount<-as.character(df$wordCount) %>% as.numeric()

# plot using ggplot
ggplot(df,aes(x=charCount,y=wordCount)) +
    geom_tile(aes(fill=Freq)) +
    geom_text(aes(label=Freq))
如果要自定义颜色等,可以向ggplot行添加其他参数。

更新

不确定如何使用热图功能,但此ggplot方法可能适合您:

library(ggplot2)
library(dplyr)

# Convert your data into frequency matrix, then data.frame
df<-data.frame(table(data))

# Set columns ready for ggplot
df$Freq<-as.factor(df$Freq)
df$charCount<-as.character(df$charCount) %>% as.numeric()
df$wordCount<-as.character(df$wordCount) %>% as.numeric()

# plot using ggplot
ggplot(df,aes(x=charCount,y=wordCount)) +
    geom_tile(aes(fill=Freq)) +
    geom_text(aes(label=Freq))

如果要自定义颜色等,可以向ggplot行添加其他参数。

请参阅更正的问题,谢谢请参阅更正的问题,谢谢谢谢!这将是伟大的,也显示对频率与颜色梯度。例如,如果对1000,99在一个数据集中出现了10次,那么对应的点是深红色,而对300,13出现了3次就变成了浅蓝色的点。有什么办法吗?请看我的问题更新。是否可以调整ggplot x和y最大值以适应较小的数字?我认为更新的答案是您可能需要的,请告诉我。谢谢!这将是伟大的,也显示对频率与颜色梯度。例如,如果对1000,99在一个数据集中出现了10次,那么对应的点是深红色,而对300,13出现了3次就变成了浅蓝色的点。有什么办法吗?请看我的问题更新。是否可以调整ggplot x和y最大值以适应较小的数字?我认为更新后的答案是您可能需要的,请告诉我。