سؤال

As a follow up to this extremely useful question and answer:

Color-coding 95% confidence ellipses for centroids

I have manage to successfully overlay confidence ellipses for my NMDS plots in ggplot. I want now to specify the colour and shape of the data points on the plot, and the colour and linetype of the ellipses, to distinguish between my four treatments.

NMDS is here:

http://pastebin.com/99WcC6wN

Dat2 is here:

http://pastebin.com/D93wrShT

The code I have tried is as follows:

ggplot(data = NMDS, aes(MDS1, MDS2)) + geom_point(aes(color = group)) +
  geom_path(data=Dat2, aes(x=NMDS1, y=NMDS2,colour=group), size=1,     linetype=2)+theme(axis.title.y=element_text(size=rel(1.1),vjust=0.2),axis.title.x=element_t    ext(size=rel(1.1),vjust=0.2),axis.text.x=element_text(size=rel(1)),axis.text.y=element_text(size=rel(1)),text = element_text(size=13)) +scale_shape_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"),
                  values = c("17", "19","17", "19")) +scale_colour_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"),
                  values = c("blue", "blue","red", "red")) +scale_linetype_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"),
                  values = c("3", "1","3", "1")) 

Unfortunately, only the colour of the points and ellipses seems to work - the linetype of the ellipses and the shape of the datapoints doesn't seem to change. Does anyone have a suggestion as to how to get around this?

Many thanks!

هل كانت مفيدة؟

المحلول

You need to map linetype and shape to group, just as you mapped color to group. You can actually do both of these mappings once in the first line, since the name of the variable is the same across your two datasets:

ggplot(data = NMDS, aes(MDS1, MDS2, color=group, linetype=group, shape=group))

Of course, then you need to delete the color and linetype mappings elsewhere. You also should specify the linetype values in scale_linetype_manual as numbers, not strings. In the end, you'll have something like this:

ggplot(data = NMDS, aes(MDS1, MDS2, color=group, linetype=group, shape=group)) + 
  geom_point() +
  geom_path(data=Dat2, aes(x=NMDS1, y=NMDS2), size=1) + 
  theme(axis.title.y=element_text(size=rel(1.1),vjust=0.2),axis.title.x=element_text(size=rel(1.1),vjust=0.2),axis.text.x=element_text(size=rel(1)),axis.text.y=element_text(size=rel(1)),text = element_text(size=13)) +
  scale_shape_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"), values = c(17, 19,17, 19)) +
  scale_colour_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"), values = c("blue", "blue","red", "red")) +
  scale_linetype_manual(name = "Treatment", labels = c("W+N-", "W+NC", "WCN-", "WCNC"), values = c(3, 1,3, 1))

the desired plot

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top