简体   繁体   中英

R xy scatter plot marker color

Trying to do an xy scatter plot with the z value being denoted by the color of the xy point.

Data:

1.1, 32.27, 19.4  
1.2, 21.34, 18  
1.4, 47.45, 19.4

R code:

 inp <- scan("beps.txt",list(x=0,y=0,z=0))  
 plot(inp$x, inp$y,pch=".")

Creates a great scatter plot, but I would like the points to be colored by the Z value.

So set the color argument:

 plot(inp$x, inp$y, pch=".", col=inp$z)

Note though that colors are integer-valued.

Here is some reproducible example that uses ggplot2. If I understood you correctly I should do what you want.

library(ggplot2)

a = c(1.1, 32.27, 19.4)
b = c(1.2, 21.34, 18)
c = c(1.4, 47.45, 19.4)


df=as.data.frame(rbind(a,b,c))
names(df) = c("x","y","z")
df

p <- ggplot(df, aes(x,y,colour=z)) +geom_point()

In general I strongly recommend ggplot2 for stuff like that. It's really worth learning a little more about. I am still in the middle of the process and realize how much it pays to put some time into ggplot2. If you do not know the package and the documentation, make sure you check it. The documentation is easy to understand and powerful !

Similar to Dirk's answer, use:

plot(inp$x, inp$y, pch=".", col= heat.colors(30)[inp$z] )

You can, of course, use other color schemes. see ?heat.colors

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