TheFoxx
TheFoxx

Reputation: 1693

How to change the linetype for ellipses in ggplot2 with stat_ellipse?

So I'm trying to change the line type on ellipses generated from stat_ellipse in ggplot2 (see here https://raw.github.com/low-decarie/FAAV/master/r/stat-ellipse.R). I can manually set the colors easily enough, but I'd like to give it a vector of linetypes that would change the linetype of the ellipse. I've tried setting the linetypes in the stat_ellipse() function, and separately also with the +scale_linetype_manual but only a single value for line type seems to work in the stat_ellipse function, and the scale_linetype_manual doesn't do anything. Any advice is appreciated!

Basic code and example image is

ggplot(data.df,aes(x = PC1,y =PC2, color = mapping$Description))+
  geom_point(size=5,aes(shape=factor(mapping$Status)))+
  stat_ellipse(aes(x = PC1,y=PC2,fill=factor(mapping$Description)),
    geom="polygon",level=0.8,alpha=0.2)+
  scale_fill_manual(values=c("red","red","green","blue","blue"))

The mapping$... are just factors. PC1 and PC2 are just vectors with the principle components and data.df is just a data frame with all of those things in it.

enter image description here

Upvotes: 3

Views: 8413

Answers (1)

TheFoxx
TheFoxx

Reputation: 1693

The line type can be changed by first specifying the line type as a factor in the aes of the stat_ellipse, followed scale_linetype_manual as user aosmith pointed out in the comments.

ggplot(data.df,aes(x = PC1,y =PC2, color = mapping$Description))+
  geom_point(size=5,aes(shape=factor(mapping$Status)))+
  stat_ellipse(aes(x = PC1,y=PC2,lty=factor(mapping$Status),fill=factor(mapping$Description)),
    geom="polygon",level=0.8,alpha=0.2)+
  scale_fill_manual(values=c("red","red","green","blue","blue"))+
  scale_linetype_manual(values=c(1,2,1,2,1))

Upvotes: 1

Related Questions