Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qplot factor by condition

Using this dataframe, df:

bat.condition bat.group      bat.money
1              safe          2825.882
2              safe          2931.875
1              glsa          6975.882
2              glsa          5407.500
1              studyabroad   3084.706
2              studyabroad   2253.750 
1              jcc           4134.706
2              jcc           5550.625
1              eagg          4578.824
2              eagg          5456.250

I would like to plot a bar chart with group on the x axis, money on the y axis. Further, I would like the bars to be separated/factored by the condition in which they occur. So far I have used the following code and it is nearly ideal. I have the bars factored by group, I would just like to further factor it by condition.

qplot(factor(bat.group), data = df, geom = "bar", fill = bat.group, 
      weight = bat.money, position = "dodge")

Which produces the image seen here.

enter image description here

like image 261
Daniel Avatar asked Apr 23 '26 06:04

Daniel


2 Answers

You can generally get groupings with the interaction function:

 qplot(interaction(bat.condition, bat.group), data = df, geom = "bar", fill = bat.group, 
      weight = bat.money, position = "dodge")
like image 98
IRTFM Avatar answered Apr 25 '26 19:04

IRTFM


You can add new variable to your dataframe and use facets like this:

df$factor = factor(df$bat.group)
qplot(as.character(bat.condition), data = df, geom = "bar", fill = bat.group, 
      weight = bat.money, position = "dodge", facets = . ~ factor)

Result

like image 35
redmode Avatar answered Apr 25 '26 19:04

redmode