Ammar Sabir Cheema
Ammar Sabir Cheema

Reputation: 990

ifelse() condition not working correctly for ggplot

I have data as shown below that I want to plot using ggplot:

> C
State        C1           C2
1   var1 -5.3458708  2.4959909437
2   var2 -5.1344963  2.4385834964
3   var3 -5.0972730  2.8425796581
4   var4  0.3743154 -0.2244166193
5   var5  0.4102937 -0.6597997849
6   var6  0.2252697 -0.2681201215
7   var7 -2.1813244  0.8744499423
8   var8 -2.1879480  0.9329252014
9   var9 -2.4635253  1.0975828789
10 var10 -3.1579603  1.3480216825
11 var11 -3.3255364  1.4333042550
12 var12 -3.0378333  1.2389625856
13 var13  4.4218140 -0.0897113458
14 var14  5.1222106  0.8949353025
15 var15  5.6032688  0.4346656081
16 var16  3.5892269 -0.1845360437
17 var17  3.9025834  0.0008319954
18 var18  3.6995580  0.1258706884
19 var19  0.4185548  0.5899289156
20 var20  0.4571535  0.1595539675
21 var21  0.6146784 -0.0438042841
22 var22  8.5871265  2.6205985329
23 var23  9.3172001  3.0164859344
24 var24  9.1885141  2.9251166107

I want to plot this data in a way that if C1 has greater value than C2 then data point on the plot shows green color and if C2 has greater value than C1 then then data point on the plot shows red color. Code for achieving this is given below:

p <- ggplot(C, aes(C1, C2,label = State)) + 
  modelr::geom_ref_line(h = 0) +
  modelr::geom_ref_line(v = 0) +
  geom_point(aes(color= ifelse(c(abs(C$C1)) > c(abs(C$C2)), "green", "red"))) +
  guides(fill = guide_legend(reverse=TRUE)) +
  xlab("First Principal Component") +
  ylab("Second Principal Component") + 
  ggtitle("First Two Principal Components of my data")

p2 <- p + geom_text_repel(segment.color = NA)

But the ifelse() condition is working opposite i.e it is giving red color where C1 has greater value than C2 and vice versa as shown from the plot below, also this can be seen from the legend:plot

Also I want to change the legend title to "Suposed title" and labels as "Column1" for data points from C1 and "Column2" for data points from C2. How this is possible?

I have not tried ifelse() in aes() for first time it has been used in spatial ecology and R and also in this stackoverflow question.

Secondly this post is different from the one linked here for possible duplication because that question is applied to time series data, the plot is different and there numerous colors are given. I could also come up with a third column for colors like the one shown here but I wanted to have an efficient solution.

Upvotes: 1

Views: 3898

Answers (1)

cardinal40
cardinal40

Reputation: 1263

Perhaps you could try something like this:

library(tidyverse)
C %>% 
  mutate(color = if_else(abs(C1) > abs(C2), "Condition 1", "Condition 2")) %>% 
  ggplot(aes(x = C1, y = C2, label = State, color = color)) +
  geom_point() +
  scale_color_manual(
    values = c("Condition 1" = "green", "Condition 2" = "red"),
    name = "New title"
  ) +
  ggrepel::geom_text_repel(color = "black")

The if_else operation is a vectorized command that creates a new variable based on the conditions you outlined above. Running it before the plotting operation makes it a little easier to visualize what's going on.

Upvotes: 3

Related Questions