如何在R中绘制不同颜色的直方图

问题描述:

我在一个csv文件中有一个大约500个整数值的数据集,每个值在50-89之间。我正在尝试在R中创建一个直方图,其中代表值50-65的条形为古铜色,66-74银和75-89金。到目前为止,我拥有的脚本如下:

I have a dataset of about 500 integer values in a csv file, with each value between 50-89. I am trying to create a histogram in R in which the bars that represent values 50-65 are bronze colored, 66-74 silver, and 75-89 gold. The script I have so far is the following:

dat1 <- read.csv("test2.csv", header=F)$V1
hist(dat1, main="Distribution of Player Ratings", xlim = c(0,99), breaks=c(seq(40,99,5)))

下面显示了一个test2.csv示例(极其简单)

A sample of test2.csv is shown below (extremely simple)

69,
68,
67,
65,
65,
62,
59,
59,
54,

现在我的图形为:

要实现前面解释的颜色准则,我该怎么办?

What would I have to do in order to fulfill the color guidelines explained earlier?

注意:我早些时候发布了这个问题,但是没有我的任何代码或对我的数据集的引用。

Note: I had posted this question earlier, but without any of my code or a reference to my dataset.

您需要在 hist 方法中添加 col 参数如下:

You need to add the col arguments in the hist method as follows:

t<- c(69,68,67,65,65,62,59,59,54)
hist(t, main="Distribution of Player Ratings",xlim = c(0,99), 
       breaks=c(seq(40,99,5)), col = c("blue", "red", "gray", "green"))

查看以上执行后得到的图像:

See the image I got after above execution:

现在,您可以根据自己的需要替换颜色值(名称或十六进制值,例如#FFFF00)要求。

Now you can replace the colour values(either name or hexadecimal values like "#FFFF00") as per your requirements.