[R] ggplot2 shifting bars to only overlap in groups

John Kane jrkrideau at inbox.com
Sat Feb 14 17:57:57 CET 2015


John Kane
Kingston ON Canada

-----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

I think maybe it is possible to first produce a blank axis, and then splitting the data frame by the value of _direction. _Then add the goem_bar and goem_errorbar for the blank axis for the first split, then add them for the second half split. This is actually a slit-apply-combine strategy. It would be perfect if we could come up with the way to do that using _d_ply()._
==========================
john: It may be but I think it is beyond my knowledge.  I almost never would use a barchart and I really don't understand them very well.  I'd reccomend taking what you have both in barchart and scatterplot and talking to the real experts in the Google Groups ggplot group. You can get advice from Hadly and the other people who actually wrote the software.
==============================
I see where you are at when you are saying geom_point. I have adjusted it a bit so that the _direction_ factor is also labeled out with _linetype_, simply adding _linetype=direction_ in the goem_errorbar function. One problem for this is normally we use bar charts as a standard for this type of statistics, and the comparison is visually more easier using bar charts than the geom_point version. 
###################
john: Ah yes, I totally missed the up-down distinction.
I am not so sure that  the visual comparison is as informative but yes it is easier.

Perhaps the worst problem is that "...normally we use barcharts..."  Geting around some conventions can be a real &*^%. It's like broken axes and two scales on one graph.  It is accepted even if probably not at all a good idea.
################################
A question about this, though. What determines the vertical axis value for the points? I see they are layered so that they do not overlap although a lot of dots share the same horizontal value: male. Is that handled utterly by a mechanism that is not relevant with the data, but to produce less clattered plot?
########################################
john
Trial and error?  Yes it is the jitter() function. It is a very handy function to handle overlapping or very crowded data points

It took a little tweaking of the data but it seemed to work.

 I was thinking of ikoverlap problem on the way home a couple of nights ago and realised that while I could not jitter a factor I could recode it into a new numeric variable  and jitter that and use it as the x-axis rather than use Gender directly.  

So what I did was create a new variable dat1$jit  and add some jitter.  Then I plotted the data using dat1$jiit rather than dat1$gender and just re-named the Y axis to Gender.  I hope that is an answer to what you asked

See http://sape.inf.usi.ch/quick-reference/ggplot2/geom_jitter for what seems some basic info. Since this is the first time I have ever had an occasion to use them I am not really all that familiar with all the ins and outs 
####################################3
Thanks so much for your hlep! Here is the code I have tweaked based on yours, adding the dashed line for direction:
################################3

I am happy to have been of a little help and it was an interesting exercise.  Good luck with the rest of it and if you get those bars to work please post back here for our other readers to see what was done.

Gook luck
john

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,

                linetype = direction),

                  width=.2 )

bc

cf  <-  bc + coord_flip()

cf

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

祝好,
========================
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 Sat, Feb 14, 2015 at 1:41 AM, John Kane <jrkrideau at inbox.com> wrote:

	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]] [http://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]] [http://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]] [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]] [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]] [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] [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.
 Check it out at http://mysecurelogon.com/manager [http://mysecurelogon.com/manager]

____________________________________________________________
Receive Notifications of Incoming Messages
Easily monitor multiple email accounts & access them with a click.
Visit http://www.inbox.com/notifier and check it out!



More information about the R-help mailing list