Hello, I am trying to overlay two histograms with this: p <- ggplot(d, aes(CHR, counts, fill = name)) + geom_bar(position = "dodge") p but I am getting this error: Error: stat_count() can only have an x or y aesthetic. Run `rlang::last_error()` to see where the error occurred. my data is this:> dCHR counts name 1 1 193554 old 2 2 220816 old 3 3 174350 old 4 4 163112 old 5 5 168125 old 6 6 182366 old 7 7 143023 old 8 8 147410 old 9 9 122112 old 10 10 138394 old 11 11 130069 old 12 12 124850 old 13 13 104119 old 14 14 83931 old 15 15 72287 old 16 16 71550 old 17 17 58380 old 18 18 76812 old 19 19 37040 old 20 20 63407 old 21 21 33863 old 22 22 33812 old 23 1 202783 new 24 2 252124 new 25 3 213337 new 26 4 201001 new 27 5 207606 new 28 6 228133 new 29 7 147218 new 30 8 177518 new 31 9 121276 new 32 10 163447 new 33 11 158724 new 34 12 142183 new 35 13 111189 new 36 14 83043 new 37 15 61063 new 38 16 55439 new 39 17 32883 new 40 18 69135 new 41 19 16624 new 42 20 48541 new 43 21 25479 new 44 22 19698 new Basically I need to show counts per CHR in "old" and "new" side by side. Please advise, Ana
Hi Ana, Sorry it's not in ggplot, but it may help: d<-read.table(text="CHR counts name 1 193554 old 2 220816 old 3 174350 old 4 163112 old 5 168125 old 6 182366 old 7 143023 old 8 147410 old 9 122112 old 10 138394 old 11 130069 old 12 124850 old 13 104119 old 14 83931 old 15 72287 old 16 71550 old 17 58380 old 18 76812 old 19 37040 old 20 63407 old 21 33863 old 22 33812 old 1 202783 new 2 252124 new 3 213337 new 4 201001 new 5 207606 new 6 228133 new 7 147218 new 8 177518 new 9 121276 new 10 163447 new 11 158724 new 12 142183 new 13 111189 new 14 83043 new 15 61063 new 16 55439 new 17 32883 new 18 69135 new 19 16624 new 20 48541 new 21 25479 new 22 19698 new", header=TRUE,stingsAsFactors=FALSE) barpos<-barplot(counts~name+CHR,data=d,beside=TRUE,names.arg=rep("",22)) legend(40,220000,c("new","old"),fill=c("gray20","gray80")) library(plotrix) staxlab(1,at=colMeans(barpos),labels=1:22) Jim On Fri, Sep 18, 2020 at 8:05 AM Ana Marija <sokovic.anamarija at gmail.com> wrote:> > Hello, > > I am trying to overlay two histograms with this: > > p <- ggplot(d, aes(CHR, counts, fill = name)) + geom_bar(position = "dodge") > p > > but I am getting this error: > Error: stat_count() can only have an x or y aesthetic. > Run `rlang::last_error()` to see where the error occurred. > > my data is this: > > > d > CHR counts name > 1 1 193554 old > 2 2 220816 old > 3 3 174350 old > 4 4 163112 old > 5 5 168125 old > 6 6 182366 old > 7 7 143023 old > 8 8 147410 old > 9 9 122112 old > 10 10 138394 old > 11 11 130069 old > 12 12 124850 old > 13 13 104119 old > 14 14 83931 old > 15 15 72287 old > 16 16 71550 old > 17 17 58380 old > 18 18 76812 old > 19 19 37040 old > 20 20 63407 old > 21 21 33863 old > 22 22 33812 old > 23 1 202783 new > 24 2 252124 new > 25 3 213337 new > 26 4 201001 new > 27 5 207606 new > 28 6 228133 new > 29 7 147218 new > 30 8 177518 new > 31 9 121276 new > 32 10 163447 new > 33 11 158724 new > 34 12 142183 new > 35 13 111189 new > 36 14 83043 new > 37 15 61063 new > 38 16 55439 new > 39 17 32883 new > 40 18 69135 new > 41 19 16624 new > 42 20 48541 new > 43 21 25479 new > 44 22 19698 new > > Basically I need to show counts per CHR in "old" and "new" side by side. > > Please advise, > Ana > > ______________________________________________ > 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.
On 2020-09-17 17:01 -0500, Ana Marija wrote:> Hello, > > I am trying to overlay two histograms with this: > > p <- ggplot(d, aes(CHR, counts, fill = name)) + geom_bar(position = "dodge") > p > > but I am getting this error: > Error: stat_count() can only have an x or y aesthetic. > Run `rlang::last_error()` to see where the error occurred.Dear Ana, you need to specify stat="identity" [1], like so: mapping <- ggplot2::aes( x=CHR, y=counts, fill=name) p <- ggplot2::ggplot() + ggplot2::geom_bar( data=d, mapping = mapping, position="dodge", stat="identity") Best, Rasmus [1] https://stackoverflow.com/questions/61068031/error-stat-count-can-only-have-an-x-or-y-aesthetic -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200918/8f6f13a4/attachment.sig>
HI Jim, fantastic solution! Thank you so much!!! Ana On Thu, Sep 17, 2020 at 6:01 PM Jim Lemon <drjimlemon at gmail.com> wrote:> > Hi Ana, > Sorry it's not in ggplot, but it may help: > > d<-read.table(text="CHR counts name > 1 193554 old > 2 220816 old > 3 174350 old > 4 163112 old > 5 168125 old > 6 182366 old > 7 143023 old > 8 147410 old > 9 122112 old > 10 138394 old > 11 130069 old > 12 124850 old > 13 104119 old > 14 83931 old > 15 72287 old > 16 71550 old > 17 58380 old > 18 76812 old > 19 37040 old > 20 63407 old > 21 33863 old > 22 33812 old > 1 202783 new > 2 252124 new > 3 213337 new > 4 201001 new > 5 207606 new > 6 228133 new > 7 147218 new > 8 177518 new > 9 121276 new > 10 163447 new > 11 158724 new > 12 142183 new > 13 111189 new > 14 83043 new > 15 61063 new > 16 55439 new > 17 32883 new > 18 69135 new > 19 16624 new > 20 48541 new > 21 25479 new > 22 19698 new", > header=TRUE,stingsAsFactors=FALSE) > barpos<-barplot(counts~name+CHR,data=d,beside=TRUE,names.arg=rep("",22)) > legend(40,220000,c("new","old"),fill=c("gray20","gray80")) > library(plotrix) > staxlab(1,at=colMeans(barpos),labels=1:22) > > Jim > > On Fri, Sep 18, 2020 at 8:05 AM Ana Marija <sokovic.anamarija at gmail.com> wrote: > > > > Hello, > > > > I am trying to overlay two histograms with this: > > > > p <- ggplot(d, aes(CHR, counts, fill = name)) + geom_bar(position = "dodge") > > p > > > > but I am getting this error: > > Error: stat_count() can only have an x or y aesthetic. > > Run `rlang::last_error()` to see where the error occurred. > > > > my data is this: > > > > > d > > CHR counts name > > 1 1 193554 old > > 2 2 220816 old > > 3 3 174350 old > > 4 4 163112 old > > 5 5 168125 old > > 6 6 182366 old > > 7 7 143023 old > > 8 8 147410 old > > 9 9 122112 old > > 10 10 138394 old > > 11 11 130069 old > > 12 12 124850 old > > 13 13 104119 old > > 14 14 83931 old > > 15 15 72287 old > > 16 16 71550 old > > 17 17 58380 old > > 18 18 76812 old > > 19 19 37040 old > > 20 20 63407 old > > 21 21 33863 old > > 22 22 33812 old > > 23 1 202783 new > > 24 2 252124 new > > 25 3 213337 new > > 26 4 201001 new > > 27 5 207606 new > > 28 6 228133 new > > 29 7 147218 new > > 30 8 177518 new > > 31 9 121276 new > > 32 10 163447 new > > 33 11 158724 new > > 34 12 142183 new > > 35 13 111189 new > > 36 14 83043 new > > 37 15 61063 new > > 38 16 55439 new > > 39 17 32883 new > > 40 18 69135 new > > 41 19 16624 new > > 42 20 48541 new > > 43 21 25479 new > > 44 22 19698 new > > > > Basically I need to show counts per CHR in "old" and "new" side by side. > > > > Please advise, > > Ana > > > > ______________________________________________ > > 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.
Is this what you want? ggplot(d, aes(counts, fill = name)) + geom_bar(stat = "bin", position = "dodge") Note: You probably should play around with the "bin" width. On Thu, 17 Sep 2020 at 18:05, Ana Marija <sokovic.anamarija at gmail.com> wrote:> Hello, > > I am trying to overlay two histograms with this: > > p <- ggplot(d, aes(CHR, counts, fill = name)) + geom_bar(position > "dodge") > p > > but I am getting this error: > Error: stat_count() can only have an x or y aesthetic. > Run `rlang::last_error()` to see where the error occurred. > > my data is this: > > > d > CHR counts name > 1 1 193554 old > 2 2 220816 old > 3 3 174350 old > 4 4 163112 old > 5 5 168125 old > 6 6 182366 old > 7 7 143023 old > 8 8 147410 old > 9 9 122112 old > 10 10 138394 old > 11 11 130069 old > 12 12 124850 old > 13 13 104119 old > 14 14 83931 old > 15 15 72287 old > 16 16 71550 old > 17 17 58380 old > 18 18 76812 old > 19 19 37040 old > 20 20 63407 old > 21 21 33863 old > 22 22 33812 old > 23 1 202783 new > 24 2 252124 new > 25 3 213337 new > 26 4 201001 new > 27 5 207606 new > 28 6 228133 new > 29 7 147218 new > 30 8 177518 new > 31 9 121276 new > 32 10 163447 new > 33 11 158724 new > 34 12 142183 new > 35 13 111189 new > 36 14 83043 new > 37 15 61063 new > 38 16 55439 new > 39 17 32883 new > 40 18 69135 new > 41 19 16624 new > 42 20 48541 new > 43 21 25479 new > 44 22 19698 new > > Basically I need to show counts per CHR in "old" and "new" side by side. > > Please advise, > Ana > > ______________________________________________ > 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. >-- John Kane Kingston ON Canada [[alternative HTML version deleted]]