Hi all, I'm trying to use ggplot to make a boxplot of some data, but I can't seem to figure out how to make it use the data I'm giving it. The data is in a data.frame so that it has two columns:>meltlvalue L1 1234 1 1234 1 1235 1 ... 1255 1 2335 2 3444 2 ... 10001 50 12311 50 ... The first column is my x value, the second is my y. I'd like to produce one boxplot for each point on the graph, 50 in total (for this case). When I try something like: p <- ggplot(meltl, aes(L1, value)) p + geom_boxplot() I get one giant boxplot, which tells me nothing much. Can anyone give me a helping hand? -- Ian Bentley M.Sc. Candidate Queen's University Kingston, Ontario [[alternative HTML version deleted]]
What about this:> testdata <- cbind.data.frame(value = runif(100), L1 = rep(1:10, each=10)) > head(testdata)value L1 1 0.3370902 1 2 0.6766098 1 3 0.2433171 1 4 0.8848674 1 5 0.5253600 1 6 0.4067238 1> tail(testdata)value L1 95 0.8149121 10 96 0.5186669 10 97 0.1080695 10 98 0.6099110 10 99 0.6141373 10 100 0.3407369 10> boxplot(testdata$value ~ testdata$L1)Sarah On Wed, Jul 7, 2010 at 4:11 PM, Ian Bentley <ian.bentley at gmail.com> wrote:> Hi all, > > I'm trying to use ggplot to make a boxplot of some data, but I can't seem to > figure out how to make it use the data I'm giving it. > > The data is in a data.frame so that it has two columns: > >>meltl > value L1 > 1234 ?1 > 1234 ?1 > 1235 ?1 > ... > 1255 ?1 > 2335 ?2 > 3444 ?2 > ... > 10001 50 > 12311 50 > ... > > The first column is my x value, the second is my y. > > I'd like to produce one boxplot for each point on the graph, 50 in total > (for this case). ?When I try something like: > > p <- ggplot(meltl, aes(L1, value)) > p + geom_boxplot() > > I get one giant boxplot, which tells me nothing much. > > Can anyone give me a helping hand? > >-- Sarah Goslee http://www.functionaldiversity.org
On 7/7/2010 1:11 PM, Ian Bentley wrote:> Hi all, > > I'm trying to use ggplot to make a boxplot of some data, but I can't seem to > figure out how to make it use the data I'm giving it. > > The data is in a data.frame so that it has two columns: > >> meltl > value L1 > 1234 1 > 1234 1 > 1235 1 > ... > 1255 1 > 2335 2 > 3444 2 > ... > 10001 50 > 12311 50 > ... > > The first column is my x value, the second is my y. > > I'd like to produce one boxplot for each point on the graph, 50 in total > (for this case). When I try something like: > > p<- ggplot(meltl, aes(L1, value)) > p + geom_boxplot() > > I get one giant boxplot, which tells me nothing much. > > Can anyone give me a helping hand?You are getting one boxplot because ggplot sees L1 is a continuous variable, and so creates just one box, but covering the range of L1. You need to let ggplot know that you consider L1 to be an indicator of category membership, rather than a continuous value. Either of the following should work (untested since I don't have your meltl): ggplot(meltl, aes(factor(L1), value)) + geom_boxplot() This version turns L1 into a factor (categorical variable), so when ggplot creates boxplots, it does so for each level of the used factor(s). ggplot(meltl, aes(L1, value, group=L1)) + geom_boxplot() Here, L1 is still a continuous variable, but when ggplot goes to make boxplots, it is told to do so for each unique value of L1 separately. The two are not identical (first has a discrete x axis, the second a continuous one), but are very similar. -- Brian Diggs Senior Research Associate, Department of Surgery, Oregon Health & Science University