user2904120
user2904120

Reputation: 416

multiple feature scatter plot in ggplot2

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))

Upvotes: 1

Views: 604

Answers (1)

Christoph Wolk
Christoph Wolk

Reputation: 1758

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"))

Upvotes: 1

Related Questions