1

I want to make a scatterplot in ggplot2 where points with "Changed" equal to "yes" are red dots, the rest remaining grey, while points with "Significant" equal to "significant" have additional boarder in green, and the rest has a grey boarder. Here is my try:

  ggplot(df, aes(x = a, y = b)) +
  geom_point(aes(col = Changed)) +
  geom_point(aes(col = Significant)) +
  scale_color_manual(values = c("green", "red","grey", "grey")) +
  theme_bw(base_size = 12) + theme(legend.position = "bottom")

and input data:

structure(list(a = c(4.31316551, 5.7663368, 2.49063318, 5.83090286, 
3.11188057, 9.58084417, 4.08696886, 3.11188057, 6.43800344, 1.77771123, 
4.22594833), b = c(0.848363512, 0.045492721, 0.049883076, 0.136202457, 
0.572585532, 0.175069609, 0.000782666, 0.848363512, 0.254619199, 
0.378181529, 0.848363512), Significant = structure(c(1L, 2L, 
2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("no", "yes"), class = "factor"), 
    Change = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 
    1L, 1L), .Label = c("no", "yes"), class = "factor")), .Names = c("a", 
"b", "Significant", "Change"), class = "data.frame", row.names = c(NA, 
-11L))
2
  • 1
    Could you add a data sample please Commented Sep 5, 2017 at 8:40
  • please see the update version Commented Sep 5, 2017 at 8:56

1 Answer 1

1

Two different ways of doing this:

ggplot(data = df, mapping = aes(x=a,y=b)) + 
  geom_point(shape = 21, size=3, mapping = aes(fill=Change, color=Significant)) + 
  scale_fill_manual(values=c("grey", "red")) + 
  scale_color_manual(values=c("grey","green"))

ggplot(data = df, mapping = aes(x=a,y=b)) + 
  geom_point(data=df[df$Significant == "no",], color="grey", size=5) + 
  geom_point(data=df[df$Significant == "yes",], color="green", size=5) +
  geom_point(size=3, mapping = aes(color=Change)) + 
  scale_color_manual(values=c("grey","red"))
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I like the first one, just one more question, say there is a third specifier, e.g. a must be > 4, so the colors are even applied, otherwise they remain grey.
I'd recommend just adding the computed conditions as new columns to the data set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.