[R] ggplot2 shifting bars to only overlap in groups

John Kane jrkrideau at inbox.com
Sun Feb 15 17:39:31 CET 2015


 I was thinking about this last night and decided to try more facetingi in the scatterplot.  (No data incuded as it is in your post below.)

 I have deleted the rest of our converstions as the mailer is refusing to mail this; the error message makes no sence; and so my best guess is that we have too large a post.

Have a look at this.  I may be predjudiced (well I am) as I do not like bar charts in most instances but I think this gives a better read of the data. It still needs tweaking but anyway.... I don't think the plot needs to be rotated now. I only did it to get a clear view as we had so many data points in only two panels.

John Kane
Kingston ON Canada

######################################

library(ggplot2)
library(scales)

dat1$jit <- ifelse( dat1$gender == "male",  1,
        ifelse( dat1$gender == 'female',  2,
          NA) )
dat1$jit  <-  as.numeric(dat1$jit)
dat1$jit  <-  jitter(dat1$jit)

ab <-  ggplot(dat1, aes (jit, t)) +
       geom_point(aes(colour = condition)) +
       geom_vline(xintercept = 1.5, colour = "grey")  +
        theme(axis.ticks = element_blank()) +
        scale_x_continuous(breaks=c(1, 2), 
                           labels=c("male", "female"),
                           name="Gender")
ab

bb  <-  ab + facet_grid(location~direction)
bb

bc  <-  bb + 
    geom_errorbar(data = dat1, aes(ymin=t-ci, ymax=t+ci,         
                colour = condition),
                  width=.1 )
bc
 
cf  <-  bc + coord_flip()
cf


######################################

-----Original Message-----
From: hyiltiz at gmail.com
Sent: Sat, 14 Feb 2015 16:32:03 +0800
To: jrkrideau at inbox.com
Subject: Re: [R] ggplot2 shifting bars to only overlap in groups

dat1  <-  structure(list(gender = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,

2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,

2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("male", "female"

), class = "factor"), direction = structure(c(1L, 1L, 2L, 2L,

1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,

1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("up",

"down"), class = "factor"), condition = structure(c(1L, 1L, 1L,

1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L,

1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("c1",

"c2", "c3", "c4"), class = "factor"), location = structure(c(1L,

1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,

2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("east",

"west"), class = "factor"), t = c(1.78664348823968, 1.045971213672,

1.45271943418506, 1.52433880441405, 0.894240903766416, 1.04200421306615,

0.992602172725307, 1.35686661120166, 1.15664717132331, 1.78519605814623,

1.3131987417228, 1.23649081362245, 1.33657440193627, 1.39069933103098,

1.16990353110185, 1.50384132346169, 0.240063246756554, 0.151918103772423,

1.26918566082989, 1.44462610872269, 0.944676078996681, 0.945358342820427,

0.68274449456263, 0.983609699924918, 1.06442538569853, 0.917922814494952,

1.06681054493614, 0.899670881737641, 0.639091165646195, 1.81227533189609,

1.02711921654525, 2.05244515236416), ci = c(0.199453475099606,

0.0208699634619525, 0.0267762622040696, 0.0719683008799792, 0.0388022593655329,

0.0873965412159785, 0.0828671112758008, 0.556676454332325, 0.109726976194332,

0.237352334670391, 0.202173510668684, 0.104263016807603, 0.0174283081233597,

0.027601059580507, 0.118300511535772, 0.272210060810133, 0.210343075045509,

0.010793003362928, 0.241665829872765, 0.387877941848338, 0.230361471258575,

0.233088662079594, 0.0956745517473407, 0.187969512005399, 0.0041769632082831,

0.26242665290992, 0.297793257986101, 0.14520541873456, 0.123447338902161,

0.10109002280374, 0.332925731545975, 0.434868806611465)), .Names = c("gender",

"direction", "condition", "location", "t", "ci"), row.names = c(NA,

-32L), class = "data.frame")

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!



More information about the R-help mailing list