Lennon Lee
Lennon Lee

Reputation: 244

preventing overwriting aes item in ggplot when applying stat_ellipse

I have my dataframe:

test <- read.table(text = "id   pc1 pc2 pc3 Agroup  Bgroup
A1  -0.129930482    -0.018973092    0.008513136 A   Big
A2  -0.102143393    -0.016435414    0.016676682 A   Big
A3  -0.109578301    -0.01841788 0.016013233 A   Big
A4  -0.193120038    0.005807723 0.134615624 A   Big
A5  -0.190417928    0.003890153 0.129485682 A   Big
A6  -0.197611556    0.015157595 0.128532864 A   Big
A7  -0.161689559    -0.015019394    -0.027334255    A   Big
A8  -0.167302706    -0.021986733    -0.036093066    A   Big
A9  -0.163510462    -0.028561404    -0.051607276    A   Big
B1  0.057531585 -0.169399591    0.119578024 B   Big
B2  0.080965446 -0.156989768    0.12677146  B   Big
B3  0.073448144 -0.160349211    0.123441267 B   Big
B4  0.076568502 -0.089241009    -0.031250932    B   Big
B5  0.088085588 -0.059495295    -0.009239871    B   Small
B6  0.087626231 -0.064351002    -0.006697004    B   Small
B7  0.035071855 0.107124946 0.003178192 B   Small
B8  0.036478974 0.101984278 -0.002018618    B   Small
B9  0.033504132 0.099925374 -0.004985971    B   Small
C1  0.066332615 -0.061698336    -0.026622501    C   Small
C2  0.069818463 -0.077728241    -0.03427275 C   Small
C3  0.076282473 -0.070022474    -0.025407112    C   Small
C4  0.044764509 -0.126369255    -0.104260416    C   Small
C5  0.035967207 -0.130660359    -0.109687301    C   Small
C6  0.048683016 -0.102567141    -0.093864923    C   Small
C7  -0.032066776    -0.04061788 -0.163315462    C   Small
C8  -0.042253269    -0.049473754    -0.169515012    C   Small
C9  -0.048820978    -0.051181292    -0.171750722    C   Small", stringsAsFactors = FALSE, header = TRUE)

I plotted a scatter plot with ellipse on it:

library(ggplot2)
ggplot(test,aes(x=pc1,y=pc2,fill=Agroup,shape=Agroup))+geom_point(size=8,alpha = 0.8)+scale_fill_manual(name="Groups",values = c("red","blue","yellow"))+scale_shape_manual(name="Groups",values=c(21,22,23))+stat_ellipse(aes(group=Bgroup,color=Bgroup),size=2,alpha=0.5)

and everything goes well: enter image description here

However, I want the ellipse to be filled, then I changed the code to:

ggplot(test,aes(x=pc1,y=pc2,fill=Agroup,shape=Agroup))+geom_point(size=8,alpha = 0.8)+scale_fill_manual(name="Groups",values = c("red","blue","yellow"))+scale_shape_manual(name="Groups",values=c(21,22,23))+stat_ellipse(aes(group=Bgroup,fill=Bgroup),size=2,alpha=0.5,geom = "polygon")

Then I got the error:

Error: Insufficient values in manual scale. 5 needed but only 3 provided.

I think it is because I have already had fill at the very beginning, how do I solve this problem and got what I want?

Upvotes: 1

Views: 496

Answers (1)

Jon Spring
Jon Spring

Reputation: 66445

Here are two possible approaches.

1) Define colors manually outside of aes for the ellipses:

ggplot(test,aes(x=pc1,y=pc2,fill=Agroup,shape=Agroup))+
    geom_point(size=8,alpha = 0.8)+
    scale_fill_manual(name="Groups",values = c("red","blue","yellow"))+
    scale_shape_manual(name="Groups",values=c(21,22,23))+
    stat_ellipse(data = filter(test, Bgroup == "Big"), aes(group=Bgroup), 
                 fill = "green", size=2, alpha=0.3,geom = "polygon") +
    stat_ellipse(data = filter(test, Bgroup == "Small"), aes(group=Bgroup), 
                 fill = "purple", size=2, alpha=0.3,geom = "polygon")

enter image description here

2) (This one's not fully baked in terms of the legend, but is only manual around the color choices.) You could reshape the data to put the categorization for fill in one column, and separately filter the geom_point term to be limited to the Agroup values, but the geom_ellipse term to be limited to Bgroup values.

test %>%
  gather(type, group, Agroup:Bgroup) %>%
  ggplot(aes(x=pc1,y=pc2,fill=group)) +
  geom_point(data = . %>% filter(type == "Agroup"), aes(shape=group), size=8,alpha = 0.8)+
  scale_fill_manual(name="Groups",values = c("red","blue","yellow", "green", "purple"))+
  scale_shape_manual(name="Groups",values=c(21,22,23)) +
  stat_ellipse(data = . %>% filter(type == "Bgroup"), 
               size=2, alpha=0.3,geom = "polygon")

enter image description here

Upvotes: 3

Related Questions