[R] ggplot2 shifting bars to only overlap in groups

John Kane jrkrideau at inbox.com
Fri Feb 13 18:41:18 CET 2015


Both files came through.  The R-help list is picky. For example it will accept cat.txt but not cat.csv.

Now I see what you are after. and I must admit I haven't a clue at the moment. I suspect others who know more about ggplot can help.  If not there is ggplot2 Google Groups that has a lot of knowledge and you might want to post there.  It accepts all kinds of file types. :)

On the other hand, I don't like dynamite plots (what you have) and wondered if it was possible to do something with geom_point() instead.

It was, in a bit of a half-assed way so I'll pass on my raw code. It's ulgy but works. I don't know if I'd call it pub-quality but perhaps it can be tweaked (wrenched? , bludgened?) into something acceptable.

BTW, I changed your data.frame name to dat1.  df is an R function.  Type df and you will see what I mean. I've also converted the data to a dput() file. Not needed as you supplied a perfectly good data set but generally good practice.

Sorry I was not able to be of more help

John Kane
Kingston ON Canada

############Begin code ##############
library(ggplot2)
library(scales)
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")


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

dat1$jit  <-  jitter(dat1$jit)

x  <-  "male"
y  <-  "female"############Begin code ##############
ab <-  ggplot(dat1, aes (jit, t)) +
       geom_point(aes(colour = condition)) +
        theme(axis.ticks = element_blank()) +
        scale_x_continuous(breaks=c(1, 2), 
                           labels=c("male", "female"),
                           name="Gender")
ab


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

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

cf  <-  bc + coord_flip()
cf

############End code ###############


John Kane
Kingston ON Canada

-----Original Message-----
From: hyiltiz at gmail.com
Sent: Fri, 13 Feb 2015 02:28:17 +0800
To: jrkrideau at inbox.com
Subject: Re: [R] ggplot2 shifting bars to only overlap in groups

I did not know the SVG file did not come through. I thought SVG should be able to pass through the filter. Here is a PDF file along with an PNG. Guess one of them should be able to pass.

祝好,
========================
He who is worthy to receive his days and nights is worthy to receive* all 
else* from you (and me).
                                                 The Prophet, Gibran Kahlil 

On Fri, Feb 13, 2015 at 12:04 AM, John Kane <jrkrideau at inbox.com> wrote:

 I'm a bit blind today. I read df as a dput() .

 John Kane
 Kingston ON Canada

 -----Original Message-----
 From: hyiltiz at gmail.com
 Sent: Thu, 12 Feb 2015 23:38:01 +0800
 To: jrkrideau at inbox.com
 Subject: Re: [R] ggplot2 shifting bars to only overlap in groups

 You are most likely simply not running the whole lines of code: note that the first line is:

 N = 32

  Best
 ,
 ========================
 He who is worthy to receive his days and nights is worthy to receive* all
 else* from you (and me).
                                                  The Prophet, Gibran Kahlil

 On Thu, Feb 12, 2015 at 11:31 PM, John Kane <jrkrideau at inbox.com> wrote:

         I am gettting the error"
  Error in rep_len(rep.int [http://rep.int] [http://rep.int [http://rep.int]](seq_len(n), rep.int [http://rep.int] [http://rep.int [http://rep.int]](k, n)), length) :

   object 'N' not found

  Also your image did not come through.  Try sending it as a pdf file.

  when I try to create
  df<- data.frame(gender=gl(2,1,N, c("male","female")),
            direction=gl(2,2,N, c("up","down")),
            condition=gl(4,4,N, c("c1","c2","c3","c4")),
            location=gl(2,16,N, c("east","west")),
            t=rnorm(N, 1, 0.5),
            ci=abs(rnorm(N, 0, 0.2)))

  John Kane
  Kingston ON Canada

  > -----Original Message-----
  > From: hyiltiz at gmail.com
  > Sent: Thu, 12 Feb 2015 22:08:36 +0800
  > To: r-help at r-project.org
  > Subject: [R] ggplot2 shifting bars to only overlap in groups
  >
  > Hi all,
  >
  > I have four factors for a continuous time variable along with its
  > confidence interval. I would like to produce a publication quality error
  > bar chart that is clear to understand. For now, I used colors, x axis
  > position, facets and alpha level to distinguish them.
  >
  > I would like to overlap each pairs of bars with the same color a bit as a
  > group, but not overlap each and every bars with each other.
  >
  > Here is a minimal example:
  >
  > N = 32
  > df<- data.frame(gender=gl(2,1,N, c("male","female")),
  >           direction=gl(2,2,N, c("up","down")),
  >           condition=gl(4,4,N, c("c1","c2","c3","c4")),
  >           location=gl(2,16,N, c("east","west")),
  >           t=rnorm(N, 1, 0.5),
  >           ci=abs(rnorm(N, 0, 0.2)))
  > pp <-
  >   ggplot(df, aes(x=gender, y=t, fill=condition, alpha=direction)) +
  >   facet_grid(location~.) +
  >   geom_bar(position=position_dodge(.9), stat="identity", color="black") +
  >   geom_errorbar(aes(ymin=t-ci, ymax=t+ci),
  >                 width=.2,                    # Width of the error bars
  >                 position=position_dodge(.9)) +
  >   scale_alpha_discrete(range= c(0.4, 1))
  > pp
  >
  >
  >
  > In the attachment, I have added the output figure, while manually editing
  > the SVG file to make the lower-left group of bars to make them as I
  > wanted.
  > (The spacing in between each pair is not necessarily required.)
  >
  >
  > Best
  > ,
  > ========================
  > He who is worthy to receive his days and nights is worthy to receive* all
  > else* from you (and me).
  >                                                  The Prophet, Gibran
  > Kahlil

 > ______________________________________________
  > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see

 > https://stat.ethz.ch/mailman/listinfo/r-help [https://stat.ethz.ch/mailman/listinfo/r-help] [https://stat.ethz.ch/mailman/listinfo/r-help [https://stat.ethz.ch/mailman/listinfo/r-help]]
  > PLEASE do read the posting guide
  > http://www.R-project.org/posting-guide.html [http://www.R-project.org/posting-guide.html] [http://www.R-project.org/posting-guide.html [http://www.R-project.org/posting-guide.html]]
  > and provide commented, minimal, self-contained, reproducible code.

  ____________________________________________________________
  Can't remember your password? Do you need a strong and secure password?
  Use Password manager! It stores your passwords & protects your account.
  Check it out at http://mysecurelogon.com/password-manager [http://mysecurelogon.com/password-manager] [http://mysecurelogon.com/password-manager [http://mysecurelogon.com/password-manager]]

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

____________________________________________________________
Can't remember your password? Do you need a strong and secure password?
Use Password manager! It stores your passwords & protects your account.



More information about the R-help mailing list