How can I change the linetype for ellipses in ggplot2 using stat_ellipse?

So I'm trying to change the line type on ellipses generated from stat_ellipse in ggplot2 (see https://raw.github.com/low-decarie/FAAV/master/r/stat-ellipse.R here ). I can manually set the colors quite easily, but I would like to give it a linetype vector that will change the linetype of the ellipse. I tried to set linetypes in stat_ellipse () function and separately also with + scale_linetype_manual parameter, but only one value for line type works in stat_ellipse function and scale_linetype_manual does nothing. Any advice is appreciated!

Basic code and example image

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 is just a factor. PC1 and PC2 are just vectors with main components and the .df data is just a data frame with all this stuff.

enter image description here

+3


source to share


1 answer


The linetype can be changed by specifying the linetype as a factor in aes stat_ellipse and then scale_linetype_manual as mentioned in the comments by user aosmith.



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

      

+1


source







All Articles