1

I have a question about plotting data in R, for example, if I have the dataset like:

    Group Species Value1 Value2
    group1 sp1      3.2    3.7
    group1 sp2      3.1    3.9
    group1 sp3      3.2    4.2
    group2 sp4      3.9    3.2
    group2 sp5      3.7    3.6
    group3 sp6      3.3    3.9
    group3 sp7      4.1    3.6

which means, different Groups have different species, and each species has two values.

I want to plot like this:

the chart I want to plot

I want to plot:

    aes(x=value1, y=value2),
  1. For the species belong to the same group, I want make a circle to make it clear;

  2. The lable in right side, I also want to group them.

what kind of package or code can I use?

ekad
  • 14,436
  • 26
  • 44
  • 46
X.Chi
  • 45
  • 8

1 Answers1

0

Drawing a circle around a group of points can be done using graphical primitives in package ggplot2. Specifically, geom_path() or geom_polygon(). Also, you can take a look at this question here.

Short of that, there's a lot you can do to make your groups distinct from each other on a plot. Here's an approach (note, this doesn't draw circles around them):

install.packages('ggplot2')
library(ggplot2)

ggplot(data = df, aes(x = value1, y = value2) +
    geom_point(aes(color = factor(Group), shape = factor(Group)))

This should give you a plot where each group has its own color and shape, making it easier to distinguish clusters from one another.

If you want the legend to display the different groups, you can convert your Group variable to a factor. Then ggplot will know what to do.

df$Group <- as.factor(df$Group)  
ggplot(data = df, aes(x = value1, y = value2, color = Group, shape = Group) + geom_point()

That should work.

Community
  • 1
  • 1
Raphael K
  • 2,265
  • 1
  • 16
  • 23
  • Thank you so much! Yes, I just want to distinct the same group from the other one. Your suggestion about using color and shape also very helpful, I will try this method. BTW, do you know how can I group the legend ? – X.Chi May 02 '16 at 17:01
  • Dear Raphael, @Raphael K, wonderful! It works, thank you so much. I will check how to use geom_path() and geom_polygon() – X.Chi May 02 '16 at 21:31