how to make colors change smoothly when plotting i

2019-08-04 08:08发布

ENV

R 3.3.1

mini data

rdn<-c(0.8,1.8,2.8)
tdn<-c(1,2,3,4,5,6,7,8,9)

idn<-matrix(c(0.3, 0.3, 0.3, 0.2, 0.2, 0.4, 0.1, 0.1, 0.5, 0, 0.2, 0.5, 0, 0.3, 0.6, 0, 0.4, 0.6, 0, 0.4, 0.6, 0, 0.5, 0.7, 0, 0.5, 0.7), nrow=9, ncol=3, byrow=T)

What I have now:

code

filled.contour(tdn, rdn, idn,
    color.palette=colorRampPalette(c("blue","yellow","red")), 
    plot.title=title(main="Detail", sub="detail", 
                    xlab="t", ylab="lambda"), 
    plot.axes = { axis(side = 1, at = tdn, labels = tdn)
                  axis(side = 2, at = rdn, labels = rdn) },
    key.title=title(main="ratio"),
    key.axes = axis(4, seq(0, 1, by = 0.1)))

Current Result

enter image description here

Problem

The color range is not what I want. There are borders among colors so colors do not change smoothly from deep blue to deep red.

Expected

enter image description here

Question

how to make colors change smoothly like my Expeced figure when plotting in R? How do I remove problems above? And rainbow not works for me either. Thanks.

EDIT

Follow Haboryme's solution

enter image description here

The foreground not disappear.

标签: r contour
2条回答
The star\"
2楼-- · 2019-08-04 08:55

Unfortunately, filled.contour ...

  • fixes the number of levels/colours between the plot and key, so experimenting with the nlevels argument (and col instead color.palette) results in a weird output (smoother image, but unreadable key).

  • In addition, the contour lines cannot be modified. Thus, an increase to 200 levels (from initially 14 in your example) is still 'readable', but higher values generate unwanted side-effects.

Example:

n <- 200
filled.contour(tdn, rdn, idn,
               col=colorRampPalette(c("blue","yellow","red"))(n),
               levels = seq(0,1,length.out = n),
               plot.title=title(main="Detail", sub="detail", 
                                xlab="t", ylab="lambda"), 
               plot.axes = { axis(side = 1, at = tdn, labels = tdn)
                   axis(side = 2, at = rdn, labels = rdn) },
               key.title=title(main="ratio"),
               key.axes = axis(4, seq(0, 1, by = 0.1)))

Output:

enter image description here

Maybe experimenting with lattice::levelplot solves the problem. At least for the legend color key, a higher number of levels does not disturb. But the image is not 'smoothed'.

Example:

library(lattice)
levelplot(idn, row.values = tdn, column.values = rdn, cuts = n, col.regions = colorRampPalette(c("blue","yellow","red"))(n))

Output:

enter image description here

查看更多
爷、活的狠高调
3楼-- · 2019-08-04 08:59

Close to @setempler answer but you might find it a bit closer to your expected output:

par(fg = NA,col="black")
filled.contour(x=tdn,
               y=rdn,
               z=idn,
               color.palette=colorRampPalette(c("blue","yellow","red")), 
               plot.title=title(main="Detail", sub="detail", 
                                xlab="t", ylab="lambda"), 
               nlevels=200,
               plot.axes = { axis(side = 1, at = tdn, labels = tdn)
                 axis(side = 2, at = rdn, labels = rdn) },
               key.title=title(main="ratio"),
               key.axes = axis(4, seq(0, 1, by = 0.1)))

enter image description here

查看更多
登录 后发表回答