I'm trying to generalize a set of plots I regularly need through a function - I have trouble in getting some aspects right in there.
myCustomScatter <- function(df, col_x, col_y, col_z){
p1 <- ggplot(df, aes(x=df[,col_x]))
p1 <- p1 + geom_point(aes(y=df[,col_y], color=df[,col_z]))
p1 <- p1 + scale_x_continuous(name=colnames(df)[col_x])
p1 <- p1 + scale_y_continuous(name=colnames(df)[col_y])
return(p1)
}
df1 <- data.frame(a=seq(1.1,9.9,1.1), b=seq(0.1,0.9,0.1), c=rev(seq(10.1, 99.9, 11.1)))
myCustomScatter(df1, 1, 2, 3)
This gives the following plot as expected.
I need the color ranges to be discrete based on df[,3]
values- I need blue
for value > 90, green
for 90 >= value > 70, yellow
for 70 >= value > 55, orange
for 55 >= value > 25 & red
for value <= 25 - how do I specify this ?
I need the title of the legend instead of df[,col_z]
to be c
which I can get through colnames(df1)[3]
- how do I specify this ?
You can try this:
myCustomScatter <- function(df, col_x, col_y, col_z){
p1 <- ggplot(df, aes_string(x=df[,col_x], color = cut(df[,col_z], c(-Inf, 25, 55, 70, 90, Inf))), size = 6)
p1 <- p1 + geom_point(aes_string(y=df[,col_y]))
p1 <- p1 + scale_x_continuous(name=colnames(df)[col_x])
p1 <- p1 + scale_y_continuous(name=colnames(df)[col_y]) # + guides(color=guide_legend('c'))
p1 <- p1 + scale_color_manual(name = names(df)[col_z],
values = c("red",
"orange",
"yellow",
"green",
"blue"))
return(p1)
}
df1 <- data.frame(a=seq(1.1,9.9,1.1), b=seq(0.1,0.9,0.1), c=rev(seq(10.1, 99.9, 11.1)))
myCustomScatter(df1, 1, 2, 3)
use aes_string
if you don't know the variable names in advance,
myCustomScatter <- function(df, col_x, col_y, col_z){
ggplot(df, aes_string(x=names(df)[col_x], y = names(df)[col_y])) +
geom_point(aes_string(colour=names(df)[col_z])) +
scale_x_continuous(names(df)[col_x]) +
scale_y_continuous(names(df)[col_y]) +
scale_color_gradientn(colours = terrain.colors(5))
}
df1 <- data.frame(a=seq(1.1,9.9,1.1), b=seq(0.1,0.9,0.1), c=rev(seq(10.1, 99.9, 11.1)))
myCustomScatter(df1, 1, 2, 3)
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.