Here's my initial dataframe.
data.df
x y z label
2 3 4 1
1 2 3 2
2 4 3 3
To make ggplot, this works when there is only 1 column (label) :
g <- ggplot(data.df) +
geom_point(data = data.df, aes(x= x, y= y,
color = ifelse(( label == 2), "a", "b")+
scale_colour_manual(values= c("a" = "blue", "b" = "green"))
return g
On clicking a button called "merge", new column gets added dynamically:
x y z label label2
2 3 4 1 1
1 2 3 2 2
2 4 3 3 2
Now in ggplot I need to access LAST column instead of label column (it could be label2, label3...) and update ggplot.
I tried two ways.
g <- ggplot(data.df) +
geom_point(data = data.df, aes(x= x, y= y,
color = ifelse(( data.df[, ncol(data.df)] == 2, "a", "b")+
scale_colour_manual(values= c("a" = "blue", "b" = "green"))
return g
As shown while using data.df[, ncol(data.df)] , I'm getting the error:
Error: Aesthetics must be either length 1 or the same as the data (40): x, y, colour
I have a feeling aes_string can be used instead of aes:
label <- paste("label", counter , sep="")
g <- ggplot(data.df) +
geom_point(data = data.df, aes_string(x= "x", y= "y",
color = ifelse((label == 2), a, b))) +
scale_colour_manual(values= c("a" = "blue", "b" = "green"))
I'm getting this error:
Error in ifelse((label == 2), a, b))), : object a not found
ggplot
. – Jake Kaupp