0

I would like to create plots/graphs by Group for the dataset shown below.

    Type    Group   price
1   Fair    Cut 4405.202
2   Good    Cut 4053.888
3   Very Good   Cut 4175.925
4   Premium Cut 4779.045
5   Ideal   Cut 3654.54
6   D   Color   3485.46
7   E   Color   3291.415
8   F   Color   3760.281
9   G   Color   4091.388
10  H   Color   4610.674
11  I   Color   5083.602
12  J   Color   5173.224

In particular, I want to create a graph with "price" on the vertical axis and "Type" on the horizontal Axis for the Group = "Cut" and similar graph for the Group="Color". I have more groups in my dataset and I would like to automate this process for all the other groups. I have tried several options including using ggplot with facet wrap by Group but the graphs I get from ggplot are not unique for each Group. Any help will be greatly appreciated.

Thanks.

Regards, Philip

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 2
    Can you show some code that you have implemented ? – sid Apr 09 '14 at 06:56
  • In addition to @sid's comments, can you make your example reproducible? Here area few tips on how to do that http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Apr 09 '14 at 07:04
  • Welcome to Stack Overflow! If the answer worked for you, it is appreciated that you accept the answer (and/or give the answer an upvote). This will give future readers a clue about the value of the solution. See also this help page: [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) – Jaap Apr 13 '14 at 08:28

1 Answers1

1

Read the data:

df <- read.table(text="Type    Group   price
Fair    Cut 4405.202
Good    Cut 4053.888
Very_Good   Cut 4175.925
Premium Cut 4779.045
Ideal Cut 3654.54
D   Color   3485.46
E   Color   3291.415
F   Color   3760.281
G   Color   4091.388
H   Color   4610.674
I   Color   5083.602
J   Color   5173.224", header=TRUE)

Load the ggplot package:

require(ggplot2)

Creating a facetted plot:

ggplot(data=df, aes(x=Type,y=price)) +
  geom_bar(stat="identity") +
  facet_grid(Group~.) + 
  theme_bw() # making the plot a little more pretty

Subsetting the data inside ggplot for a plot of only one group:

ggplot(data=df[df$Group=="Cut",], aes(x=Type,y=price)) +
  geom_bar(stat="identity", fill="grey90", color="blue") + # making plot look better with 'fill' & 'color' parameters
  theme_bw()
Jaap
  • 81,064
  • 34
  • 182
  • 193