G.Maubach at weinwolf.de
2016-Oct-05 13:55 UTC
[R] Antwort: RE: How to plot a bunch of dichotomous code variables in one plot using ggplot2
Hi Bob, Hi John, Hi readers, many thanks for your reply. I did barplot(colSums(dataset %>% select(FirstVar:LastVar))) and it worked fine. How would I do it with ggplot2? Kind regards Georg Von: "Fox, John" <jfox at mcmaster.ca> An: "G.Maubach at weinwolf.de" <G.Maubach at weinwolf.de>, Kopie: "r-help at r-project.org" <r-help at r-project.org> Datum: 05.10.2016 15:01 Betreff: RE: [R] How to plot a bunch of dichotomous code variables in one plot using ggplot2 Dear Georg, How about barplot(colSums(ds)) ? Best, John ----------------------------- John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 Web: socserv.mcmaster.ca/jfox> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of > G.Maubach at weinwolf.de > Sent: October 5, 2016 8:47 AM > To: r-help at r-project.org > Subject: [R] How to plot a bunch of dichotomous code variables in oneplot> using ggplot2 > > Hi All, > > I have a bunch of dichotomous code variables which shall be plotted inone> graph using one of their values, this is "1" in this case. > > The dataset looks like this: > > -- cut -- > var1 <- c(1,0,1,0,0,1,1,1,0,1) > var2 <- c(0,1,1,1,1,0,0,0,0,0) > var3 <- c(1,1,1,1,1,1,1,1,0,1) > > ds <- data.frame(var1, var2, var3) > -- cut -- > > I would like to have a bar plot like this > > > > * > * > * > * > * * > * * > * * * > * * * > * * * > * * * > ------------------------- > var1 var2 var3 > > If this possible in R? If so, how can I achieve this? > > Kind regards > > Georg > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
Thierry Onkelinx
2016-Oct-05 14:17 UTC
[R] Antwort: RE: How to plot a bunch of dichotomous code variables in one plot using ggplot2
Here is a ggplot2, tidyr, dplyr solution library(tidyr) library(dplyr) library(ggplot2) ds %>% gather() %>% group_by(key) %>% summarize(total = sum(value)) %>% ggplot(aes(x = key, y = total)) + geom_bar(stat = "identity") ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey 2016-10-05 15:55 GMT+02:00 <G.Maubach at weinwolf.de>:> Hi Bob, > Hi John, > Hi readers, > > many thanks for your reply. > > I did > > barplot(colSums(dataset %>% select(FirstVar:LastVar))) > > and it worked fine. > > How would I do it with ggplot2? > > Kind regards > > Georg > > > > > Von: "Fox, John" <jfox at mcmaster.ca> > An: "G.Maubach at weinwolf.de" <G.Maubach at weinwolf.de>, > Kopie: "r-help at r-project.org" <r-help at r-project.org> > Datum: 05.10.2016 15:01 > Betreff: RE: [R] How to plot a bunch of dichotomous code variables > in one plot using ggplot2 > > > > Dear Georg, > > How about barplot(colSums(ds)) ? > > Best, > John > > ----------------------------- > John Fox, Professor > McMaster University > Hamilton, Ontario > Canada L8S 4M4 > Web: socserv.mcmaster.ca/jfox > > > > -----Original Message----- > > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of > > G.Maubach at weinwolf.de > > Sent: October 5, 2016 8:47 AM > > To: r-help at r-project.org > > Subject: [R] How to plot a bunch of dichotomous code variables in one > plot > > using ggplot2 > > > > Hi All, > > > > I have a bunch of dichotomous code variables which shall be plotted in > one > > graph using one of their values, this is "1" in this case. > > > > The dataset looks like this: > > > > -- cut -- > > var1 <- c(1,0,1,0,0,1,1,1,0,1) > > var2 <- c(0,1,1,1,1,0,0,0,0,0) > > var3 <- c(1,1,1,1,1,1,1,1,0,1) > > > > ds <- data.frame(var1, var2, var3) > > -- cut -- > > > > I would like to have a bar plot like this > > > > > > > > * > > * > > * > > * > > * * > > * * > > * * * > > * * * > > * * * > > * * * > > ------------------------- > > var1 var2 var3 > > > > If this possible in R? If so, how can I achieve this? > > > > Kind regards > > > > Georg > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting- > > guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Ulrik Stervbo
2016-Oct-05 14:34 UTC
[R] Antwort: RE: How to plot a bunch of dichotomous code variables in one plot using ggplot2
I have a version looking like the original request even library(tidyr) library(ggplot2) var1 <- c(1,0,1,0,0,1,1,1,0,1) var2 <- c(0,1,1,1,1,0,0,0,0,0) var3 <- c(1,1,1,1,1,1,1,1,0,1) ds <- data.frame(var1, var2, var3) ds %>% gather() %>% group_by(key) %>% filter(value > 0) %>% mutate(fake_y c(1:n())) %>% ggplot() + aes(x = key, y = fake_y) + geom_point() ds %>% gather() %>% group_by(key) %>% mutate(var_count = sum(value)) %>% ggplot() + aes(x = key, y = var_count) + geom_bar(stat = "identity") ds %>% gather() %>% ggplot() + aes(x = key, y = value) + stat_summary(fun.y = sum, geom = "bar") I prefer to do all manipulation before plotting as I find this more informative but having ggplot doing the sum is also possible. Best, Ulrik On Wed, 5 Oct 2016 at 16:04 <G.Maubach at weinwolf.de> wrote:> Hi Bob, > Hi John, > Hi readers, > > many thanks for your reply. > > I did > > barplot(colSums(dataset %>% select(FirstVar:LastVar))) > > and it worked fine. > > How would I do it with ggplot2? > > Kind regards > > Georg > > > > > Von: "Fox, John" <jfox at mcmaster.ca> > An: "G.Maubach at weinwolf.de" <G.Maubach at weinwolf.de>, > Kopie: "r-help at r-project.org" <r-help at r-project.org> > Datum: 05.10.2016 15:01 > Betreff: RE: [R] How to plot a bunch of dichotomous code variables > in one plot using ggplot2 > > > > Dear Georg, > > How about barplot(colSums(ds)) ? > > Best, > John > > ----------------------------- > John Fox, Professor > McMaster University > Hamilton, Ontario > Canada L8S 4M4 > Web: socserv.mcmaster.ca/jfox > > > > -----Original Message----- > > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of > > G.Maubach at weinwolf.de > > Sent: October 5, 2016 8:47 AM > > To: r-help at r-project.org > > Subject: [R] How to plot a bunch of dichotomous code variables in one > plot > > using ggplot2 > > > > Hi All, > > > > I have a bunch of dichotomous code variables which shall be plotted in > one > > graph using one of their values, this is "1" in this case. > > > > The dataset looks like this: > > > > -- cut -- > > var1 <- c(1,0,1,0,0,1,1,1,0,1) > > var2 <- c(0,1,1,1,1,0,0,0,0,0) > > var3 <- c(1,1,1,1,1,1,1,1,0,1) > > > > ds <- data.frame(var1, var2, var3) > > -- cut -- > > > > I would like to have a bar plot like this > > > > > > > > * > > * > > * > > * > > * * > > * * > > * * * > > * * * > > * * * > > * * * > > ------------------------- > > var1 var2 var3 > > > > If this possible in R? If so, how can I achieve this? > > > > Kind regards > > > > Georg > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting- > > guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Bob Rudis
2016-Oct-05 14:35 UTC
[R] Antwort: RE: How to plot a bunch of dichotomous code variables in one plot using ggplot2
No need to bring in so many dependencies for a simple ggplot2 marplot: ds <- stack(ds) ggplot(ds[ds$values==1,], aes(ind)) + geom_bar() On Wed, Oct 5, 2016 at 10:17 AM, Thierry Onkelinx <thierry.onkelinx at inbo.be> wrote:> Here is a ggplot2, tidyr, dplyr solution > > library(tidyr) > library(dplyr) > library(ggplot2) > ds %>% > gather() %>% > group_by(key) %>% > summarize(total = sum(value)) %>% > ggplot(aes(x = key, y = total)) + > geom_bar(stat = "identity") > > > ir. Thierry Onkelinx > Instituut voor natuur- en bosonderzoek / Research Institute for Nature and > Forest > team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance > Kliniekstraat 25 > 1070 Anderlecht > Belgium > > To call in the statistician after the experiment is done may be no more > than asking him to perform a post-mortem examination: he may be able to say > what the experiment died of. ~ Sir Ronald Aylmer Fisher > The plural of anecdote is not data. ~ Roger Brinner > The combination of some data and an aching desire for an answer does not > ensure that a reasonable answer can be extracted from a given body of data. > ~ John Tukey > > 2016-10-05 15:55 GMT+02:00 <G.Maubach at weinwolf.de>: > >> Hi Bob, >> Hi John, >> Hi readers, >> >> many thanks for your reply. >> >> I did >> >> barplot(colSums(dataset %>% select(FirstVar:LastVar))) >> >> and it worked fine. >> >> How would I do it with ggplot2? >> >> Kind regards >> >> Georg >> >> >> >> >> Von: "Fox, John" <jfox at mcmaster.ca> >> An: "G.Maubach at weinwolf.de" <G.Maubach at weinwolf.de>, >> Kopie: "r-help at r-project.org" <r-help at r-project.org> >> Datum: 05.10.2016 15:01 >> Betreff: RE: [R] How to plot a bunch of dichotomous code variables >> in one plot using ggplot2 >> >> >> >> Dear Georg, >> >> How about barplot(colSums(ds)) ? >> >> Best, >> John >> >> ----------------------------- >> John Fox, Professor >> McMaster University >> Hamilton, Ontario >> Canada L8S 4M4 >> Web: socserv.mcmaster.ca/jfox >> >> >> > -----Original Message----- >> > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of >> > G.Maubach at weinwolf.de >> > Sent: October 5, 2016 8:47 AM >> > To: r-help at r-project.org >> > Subject: [R] How to plot a bunch of dichotomous code variables in one >> plot >> > using ggplot2 >> > >> > Hi All, >> > >> > I have a bunch of dichotomous code variables which shall be plotted in >> one >> > graph using one of their values, this is "1" in this case. >> > >> > The dataset looks like this: >> > >> > -- cut -- >> > var1 <- c(1,0,1,0,0,1,1,1,0,1) >> > var2 <- c(0,1,1,1,1,0,0,0,0,0) >> > var3 <- c(1,1,1,1,1,1,1,1,0,1) >> > >> > ds <- data.frame(var1, var2, var3) >> > -- cut -- >> > >> > I would like to have a bar plot like this >> > >> > >> > >> > * >> > * >> > * >> > * >> > * * >> > * * >> > * * * >> > * * * >> > * * * >> > * * * >> > ------------------------- >> > var1 var2 var3 >> > >> > If this possible in R? If so, how can I achieve this? >> > >> > Kind regards >> > >> > Georg >> > >> > ______________________________________________ >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guide http://www.R-project.org/posting- >> > guide.html >> > and provide commented, minimal, self-contained, reproducible code. >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > >[[alternative HTML version deleted]]