有关R软件使用的英文书籍翻译
发布网友
发布时间:2022-05-15 18:54
我来回答
共1个回答
热心网友
时间:2023-12-24 13:00
Creatingheat maps of single Z variable with scale
单变量z的规模creatingheat地图
Inthis recipe we will learn how to make a heat map showing the variation invalues of one variable (z) along the X and Y axes as a grid of colors, anddisplay a scale alongside.
在这个食谱中我们将学习如何做一个热地图显示一个变量的变化上(Z)沿X轴和Y轴作为一个网格的颜色,显示规模的旁边。
Getting ready
准备好
Weare only using the base graphics functions for this recipe. So, just open upthe R prompt and type the following code. We will use the sales.csv example dataset for this recipe. So let'sfirst load it:
我们只使用基本图形函数这个食谱。所以,只要打开了R提示符下输入以下代码。我们将使用的示例数据集的sales.csv这个食谱。所以let'sfirst加载它:
sales<-read.csv("sales.csv")
销售<<读。CSV(“销售。csv”)
We will use the RColorBrewer package for some good color palettes. Solet's make sure it's installed and loaded:
我们将使用rcolorbrewer包装一些好的调色板。让我们确保它的安装和加载:
install.packages("RColorBrewer")
安装包(“rcolorbrewer”)。
library(RColorBrewer)
图书馆(rcolorbrewer)
How to do it...
如何做到这一…
The sales datasethas monthly sales data for four cities. Let's make a heat map with the monthsalong the X axis and the cities on the Y axis:
销售datasethas月度销售数据的四个城市。让我们与monthsalong X轴和Y轴的城市做一个热地图:
rownames(sales)<-sales[,1]
行名(销售)<销售[ 1 ],
sales<-sales[,-1]
销售< -销售[,- 1 ]
data_matrix<-data.matrix(sales)
data_matrix <数据矩阵(销售)。
pal=brewer.pal(7,"YlOrRd")
朋友=啤酒。PAL(7,“ylorrd”)
breaks<-seq(3000,12000,1500)
违反<<序列(3000120001500)
#Create layout with 1 row and 2 columns (for the heatmapand scale); the heatmap column is 8 times as wide as the scale column
#创建布局1行2列(为heatmapand规模);热图柱是8倍宽的表列
layout(matrix(data=c(1,2), nrow=1, ncol=2),widths=c(8,1),
布局(矩阵(数据= c(1,2)= 1,nrow,ncol = 2),宽度= C(8,1),
heights=c(1,1))
高度= c(1,1))
#Set margins for the heatmap
#设置页边距的热图
par(mar = c(5,10,4,2),oma=c(0.2,0.2,0.2,0.2),mex=0.5)
PAR(MAR = C(5,10,4,2),OMA = C(0.2,0.2,0.2,0.2),墨西哥= 0.5)
par(mar = c(5,10,4,2),oma=c(0.2,0.2,0.2,0.2),mex=0.5)
PAR(MAR = C(5,10,4,2),OMA = C(0.2,0.2,0.2,0.2),墨西哥= 0.5)
image(x=1:nrow(data_matrix),y=1:ncol(data_matrix),
图像(X = 1:nrow(data_matrix),Y = 1:ncol(data_matrix),
z=data_matrix,axes=FALSE,xlab="Month",
Z = data_matrix,轴= false,xlab =“月”,
ylab="",col=pal[1:(length(breaks)-1)],
ylab =”,col = PAL [ 1(长度(休息)- 1)],
breaks=breaks,main="Sales Heat Map")
休息=休息,主=“销售热图”)