2

How can I color A and D with a selected fill red/blue and the black outline. Check my <geom_point> to see what I have tried. Thanks!

obs <- c("Gene1", "Gene2", "Gene3", "Gene4","Gene5", "Gene6")
    func1 <- c("A", "B", "C", "D", "C", "A")
    func2 <- c("A1", "B1", "C1", "D1", "C2", "A2")
    Cond1 <- c(0.007623561, 0.004639893, 0.000994121, 0.017494429, 0.000366445, 0.006663334)
    Cond2 <- c(0.011299941, 0.009994388, 0.001012428, 0.013695669, 0.000299771, 0.010287904)
    Cond3 <- c(0.005055458, 0.016826251, 0.001311254, 0.016115009, 0.000242897, 0.004583889)
    df <- data.frame(obs, func1, func2, Cond1, Cond2, Cond3)

library(ggtern)

g = ggtern(data=df,aes(x=Cond1,y=Cond2,z=Cond3)) +
theme_bw() +
geom_point(aes(fill = c("A" = "red", "D" = "blue"), shape=21, colour="black" ) +
labs(x="Cond1",y="Cond2",z="Cond3") +
scale_T_continuous(breaks=unique(df$x))+ 
scale_L_continuous(breaks=unique(df$y))+ 
scale_R_continuous(breaks=unique(df$z))

enter image description here

Desired output:

enter image description here

Ecg
  • 908
  • 1
  • 10
  • 28

1 Answers1

2

You need to create a named list of colors and then use the scale_fill_manual function.

#A General way of creating a name list of colors
# col<-c("A" = "red", "B"="black", "C"="black", "D"="blue")
col<-rep("black", length(unique(df$func1)))
names(col) <- unique(df$func1)
col[which(names(col)=="A")] <- 'red'
col[which(names(col)=="D")] <- 'blue'

library(ggtern)

g <- ggtern(data=df, aes(x=Cond1,y=Cond2,z=Cond3)) +
   theme_bw() +
   geom_point(aes(fill=func1), shape=21, colour="black" ) +
   scale_fill_manual(values=col) +
   labs(x="Cond1",y="Cond2",z="Cond3") +
   scale_T_continuous(breaks=unique(df$x))+ 
   scale_L_continuous(breaks=unique(df$y))+ 
   scale_R_continuous(breaks=unique(df$z))

print(g)    

Update
ggplot plots the different layers in order that they are defined. In order to plot the A & D points on top of the other points, one can add another geom_point definition after the original one.

Here I created a subset of the A&D points and plotted them over the initial set.

df_ad <- df[(df$func1=="A" | df$func1=="D"),]

ggtern(data=df, aes(x=Cond1,y=Cond2,z=Cond3)) +
   theme_bw() +
   geom_point(aes(fill=func1), shape=21, colour="black" ) +
   geom_point(data=df_ad, aes(x=Cond1,y=Cond2,z=Cond3, fill=func1), shape=21) +
   scale_fill_manual(values=col) #+ ....
   
Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • Running this code on my data, do I need to manually say that other than A, D are black? because I get the folllowing error: < Error: Insufficient values in manual scale. 421 needed but only 18 provided. > – Ecg Nov 01 '20 at 19:20
  • Yes, you need to have the color defined for all of the unique values of the categories. Are there 421 values of func1 or func2? – Dave2e Nov 01 '20 at 20:07
  • Yes, there are 421 func1, 1418 func2, but I would like to only pick 3 or 4 of func 1 same as in the example here which I called "A" and "D". Is there a way of saying ? kind of? – Ecg Nov 01 '20 at 20:17
  • 1
    That is what I tried to do with `rep("black", length(unique(df$func1)))`, this should be a vector 421 elements long all set to 'black'. I named the elements and then change the values for the elements names A and D. Note the original posting I dropped the "df$", it is now correct. – Dave2e Nov 01 '20 at 20:59
  • Yes, that df$func1 solved the problem, thanks! But now I see some black points overlap on top of the coloured ones. Can you choose certain geom_points to overlap on top? You can see what I mean if you run this with bigger size: ggtern(data=df, aes(x=Cond1,y=Cond2,z=Cond3)) + theme_bw() + geom_point(aes(fill=func1), shape=21, size = 20, colour="black" ) + scale_fill_manual(values=col) + labs(x="Cond1",y="Cond2",z="Cond3") + scale_T_continuous(breaks=unique(df$x))+ scale_L_continuous(breaks=unique(df$y))+ scale_R_continuous(breaks=unique(df$z)) – Ecg Nov 01 '20 at 22:25
  • I have tried changing the col colour to 'white' but still white overlaps colours, and adding alpha=0.25 within geom_point but that just fades all colours including the red/blue assigned. What makes a dot plot on top of another? Maybe I should raise a new topic with this. – Ecg Nov 01 '20 at 22:27
  • I see it now, thanks for notifying me, interesting and very useful, thanks! – Ecg Nov 03 '20 at 12:13