简体   繁体   中英

How to make a color plot of a matrix in r

The plot between e_values and r_values, the values of probabilities of fp0 indicated by different colors of my color scale. I have a matrix which has 132 rows and 3 columns.The names of the are e_values, r_values and fp0.Now how do I plot a color plot of my matrix?The plot must have e_values on the y-axis and r_values on the x-axis and the various values of fp0 as the colors.The e_values vary from 0 to 0.5 in the intervals of 0.1 and the r_values vary from 0 to 6.5, in the intervals of 0.5 till 4 and then 0.2 from 4 to 6.5 and my fp0 values are basically my probabilities and so they vary from 0 to 1.I couldn't figure out the way to upload my csv file and so I gave a description of my matrix and am also printing my matrix.I am also quite new to R and quite unfamiliar with the way the packages like ggplot2 and similar others work, so it will be very helpful if I get a detailed explanation.

      e_values   r_values   fp0
    1   0          0         1.0000
    2   0.1        0         1.0000
    3   0.2        0         1.0000
    4   0.3        0         1.0000
    5   0.4        0         1.0000
    6   0.5        0         1.0000   
    7   0          0.5       1.0000  
    8   0.1        0.5       1.0000 
    9   0.2        0.5       1.0000
   10   0.3        0.5       1.0000
   11   0.4        0.5       1.0000
   12   0.5        0.5       1.0000
   13   0          1         1.0000

My matrix goes on in the above manner until r=4, after which the subsequent r values increase by 0.2 and the fp0 values also start to show variation. and this pattern is followed till the no. of rows become 132.

You just need to use fp0 as color in your ggplot.

The code below will create the plot you described. Here, darker colors indicate higher probabilities. If you have a discrete probability, you could also use distinct colors for each level (second plot).

require(ggplot2)

e_values = seq(0, 0.5, by = 0.1)
r_values = c(seq(0, 4, by = 0.5), seq(4.2, 6.5, by = 0.2))

df <- data.frame(e_values = sample(e_values, 132, replace = T),
                 r_values = sample(r_values, 132, replace = T),
                 fp0 = runif(132))

g <- ggplot(df, aes(x = r_values, y = e_values, color = fp0))
g <- g + geom_point()
g

df <- data.frame(e_values = sample(e_values, 132, replace = T),
                 r_values = sample(r_values, 132, replace = T),
                 fp0 = sample(seq(0, 1, by = 0.2), 132, replace = T))

g <- ggplot(df, aes(x = r_values, y = e_values, color = as.factor(fp0)))
g <- g + geom_point()
g

连续概率 离散概率

Edit: The picture you uploaded has r_values as y axis and e_values as x axis. Additionally, the shape of the points is different. Here is the code to produce the desired plot:

g <- ggplot(df, aes(x = e_values, y = r_values, color = fp0))
g <- g + geom_point(shape = 15, aes(fill = fp0))
g

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM