One colour gradient according to value in column scatterplot in R

∥☆過路亽.° 提交于 2019-12-21 21:36:45

问题


I would like to make my scatterplot circles shaded according to the value in one column.

"value"; "avg.sal"; "avg.temp"
2,5698; 34,27254; 4,44
5,4361; 34,30686; 4,64
2,27; 34,3538; 8,05
5,6015; 34,50136; 5,01
2,27; 34,37596; 7,4

I have my plot ready with salinity on the y-axis and temperature on the x-axis.

plot(df$avg.sal, df$avg.temp)

How do I shade the circles (e.g. from light blue to dark blue) according to the column "value"? The values have a big range but smaller values (e.g. 2) should be light blue and larger values (e.g. 10) should be dark blue. I would prefer not to use GGplot.


回答1:


dat <- read.delim(text='"value"; "avg.sal"; "avg.temp"
 2,5698; 34,27254; 4,44
 5,4361; 34,30686; 4,64
 2,27; 34,3538; 8,05
 5,6015; 34,50136; 5,01
 2,27; 34,37596; 7,4"', sep=";", dec=",")

bluefunc <- colorRampPalette(c("lightblue", "darkblue"))
plot( dat$avg.sal, dat$avg.temp, 
              col=bluefunc(5)[findInterval(dat$value, seq(2:6))] )

To respond to the followup question. When cex is specified inside a vector it does control size of the "points":

dat$size <- 1:5
bluefunc <- colorRampPalette(c("lightblue", "darkblue"))
plot( dat$avg.sal,  dat$avg.temp,
            cex=dat$size,
            col=bluefunc(5)[findInterval(dat$value, seq(2:6))] )


来源:https://stackoverflow.com/questions/18827214/one-colour-gradient-according-to-value-in-column-scatterplot-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!