tag:blogger.com,1999:blog-4946490806848569840.post6701952555145602546..comments2023-04-24T16:57:22.851-07:00Comments on R snippets: Visualizing tables in ggplot2Unknownnoreply@blogger.comBlogger9125tag:blogger.com,1999:blog-4946490806848569840.post-3845710558020354142012-12-13T15:16:45.659-08:002012-12-13T15:16:45.659-08:00Maybe your default graphic device is not wide enou...Maybe your default graphic device is not wide enough.<br />Try widening it using the mouse.Bogumił Kamińskihttps://www.blogger.com/profile/06250268799809238730noreply@blogger.comtag:blogger.com,1999:blog-4946490806848569840.post-18568954170034147202012-12-13T02:43:12.862-08:002012-12-13T02:43:12.862-08:00Hello, I tried your code but it can not display th...Hello, I tried your code but it can not display the graphical class you think you know the problem?<br />it displays black.<br />Thank you in advancebi2open.frhttps://www.blogger.com/profile/08716096253418139623noreply@blogger.comtag:blogger.com,1999:blog-4946490806848569840.post-26066469322706329482012-06-04T09:46:16.837-07:002012-06-04T09:46:16.837-07:00Another way to do this (maybe a bit cleaner) is th...Another way to do this (maybe a bit cleaner) is through use of plyr package:<br /><br />library(plyr)<br />ddply(melt(unclass(Titanic)),.(Class,Sex,Age), function(x) { sum(subset(x,Survived == "Yes", value)) / sum(x$value)})Bogumił Kamińskihttps://www.blogger.com/profile/06250268799809238730noreply@blogger.comtag:blogger.com,1999:blog-4946490806848569840.post-4787182559866100172012-06-04T06:14:39.858-07:002012-06-04T06:14:39.858-07:00acast looks at column with name "value" ...acast looks at column with name "value" by default.<br />The data frame passed to acast looks like this:<br /><br />> head(melt(unclass(Titanic)))<br />Class Sex Age Survived value<br />1 1st Male Child No 0<br />2 2nd Male Child No 0<br />3 3rd Male Child No 35<br />4 Crew Male Child No 0<br />5 1st Female Child No 0<br />6 2nd Female Child No 0<br /><br />so acast gets data from "value" column<br />ORDERED by values in Survived column because Survived was not used in the formula and in data frame melt(unclass(Titanic)) "No" values are before "Yes" values.<br /><br />If you used the following code:<br /><br />v <- melt(unclass(Titanic))<br />v <- v[order(v$Survived, decreasing=T),]<br />m <- acast(v, Class~Age+Sex, function(x) {x[2] / sum(x)})<br /><br />you would get the following table:<br />> m<br /> Adult_Female Adult_Male Child_Female Child_Male<br />1st 0.02777778 0.6742857 0.0000000 0.0000000<br />2nd 0.13978495 0.9166667 0.0000000 0.0000000<br />3rd 0.53939394 0.8376623 0.5483871 0.7291667<br />Crew 0.13043478 0.7772622 NaN NaN<br /><br />with probabilities of "No" because in data frame v now "Yes" values are before "No" values.Bogumił Kamińskihttps://www.blogger.com/profile/06250268799809238730noreply@blogger.comtag:blogger.com,1999:blog-4946490806848569840.post-62972566723796740212012-06-04T05:51:28.593-07:002012-06-04T05:51:28.593-07:00Thanks for the quick response!
I now understand w...Thanks for the quick response!<br /><br />I now understand why [2] would yield all the "Yes" values, but I'm still a bit confused as to why x corresponds to the survived column. Is this because acast looks for the first argument not specified in the formula?<br /><br />Thanks again.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4946490806848569840.post-65402772140052864882012-06-03T12:08:49.048-07:002012-06-03T12:08:49.048-07:00x[2] is number of survivors with Survided=="Y...x[2] is number of survivors with Survided=="Yes" in each intersection of Class/Age/Sex values.<br /><br />To see that first look at the code in my last explaining comment. Then notice that:<br />1) In formula:<br />m <- acast(melt(unclass(Titanic)), Class~Age+Sex,<br />function(x) {x[2] / sum(x)})<br />we do not supply value.var argument so "value" column is taken as values for calculations (this is a default column choice of acast).<br /><br />2) In formula Class~Age+Sex we did not use Survived variable. It is a factor with levels: No Yes (order is important) so our acast will pass to the function two values from column "value" in this order (corresponding to "No" and "Yes" counts). So x[2] is Yes count and sum(x) is total count.<br /><br />Notice that in acast documentation you have information that: "If the combination of variables you supply does not uniquely identify one row in the original data set, you will need to supply an aggregating function, fun.aggregate. This function should take a vector of numbers and return a single summary statistic.".<br />This is exactly what happens here.Bogumił Kamińskihttps://www.blogger.com/profile/06250268799809238730noreply@blogger.comtag:blogger.com,1999:blog-4946490806848569840.post-58218498778037411612012-06-03T09:40:01.868-07:002012-06-03T09:40:01.868-07:00Very cool.
I'm having trouble following some ...Very cool.<br /><br />I'm having trouble following some of the code, though. In the acast function, why does x[2] return the number of survivors for each group?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4946490806848569840.post-29328019135608657872012-05-06T12:39:42.133-07:002012-05-06T12:39:42.133-07:00"survived" scale represents probability ..."survived" scale represents probability that a member of given group (intersection of Class/Age/Sex) survived in Titanic accident.<br /><br />melt(unclass(Titanic)) results in a data frame with 5 columns:<br /><br />> head(melt(unclass(Titanic)))<br /> Class Sex Age Survived value<br />1 1st Male Child No 0<br />2 2nd Male Child No 0<br />3 3rd Male Child No 35<br />4 Crew Male Child No 0<br />5 1st Female Child No 0<br />6 2nd Female Child No 0<br /><br />Then if we use formula "Class~Age+Sex" a two element vector is passed to function containing counts for "survived" levels "No" and "Yes".<br /><br />You can find it out by inspecting the following code:<br /><br />> acast(melt(unclass(Titanic)), Class~Age+Sex,<br />+ function(x) { str(x); x[2] / sum(x)})<br /> num(0) <br /> num [1:2] 4 140<br /> num [1:2] 13 80<br /> num [1:2] 89 76<br /> num [1:2] 3 20<br /> num [1:2] 118 57<br /> num [1:2] 154 14<br /> num [1:2] 387 75<br /> num [1:2] 670 192<br /> num [1:2] 0 1<br /> num [1:2] 0 13<br /> num [1:2] 17 14<br /> num [1:2] 0 0<br /> num [1:2] 0 5<br /> num [1:2] 0 11<br /> num [1:2] 35 13<br /> num [1:2] 0 0<br /> Adult_Female Adult_Male Child_Female Child_Male<br />1st 0.9722222 0.32571429 1.0000000 1.0000000<br />2nd 0.8602151 0.08333333 1.0000000 1.0000000<br />3rd 0.4606061 0.16233766 0.4516129 0.2708333<br />Crew 0.8695652 0.22273782 NaN NaN<br /><br />Interestingly the first "num(0)" in the output is due to additional call of function in vaggregate because no "fill" argument is passed to acast.Bogumił Kamińskihttps://www.blogger.com/profile/06250268799809238730noreply@blogger.comtag:blogger.com,1999:blog-4946490806848569840.post-45017442414731569792012-05-06T10:56:59.714-07:002012-05-06T10:56:59.714-07:00Can you please explain what the 'survived'...Can you please explain what the 'survived' scale represents.<br /><br />Thanks, I really like this idea.Salilhttp://twitter.com/#!/weRuckusnoreply@blogger.com