[R] ggplot2 shifting bars to only overlap in groups

John Kane jrkrideau at inbox.com
Thu Feb 19 19:42:54 CET 2015


Ah yes, with the full data set that looks rather good, indeed.  I tried it with the simulated data but there was not enough variation in the data to give any useful indication of what was happening.  

RE the quartile information, I don't know, it might be of use but it also might add to much clutter to the graph. A quick Google search suggest one approach using boxplots http://www.sthda.com/english/wiki/ggplot2-violin-plot-quick-start-guide-r-software-and-data-visualization but that might be overkill and produce too clutterd a graph in your case.  Still it looks nice :)

BTW I don't think you, strictly speaking, meant quantile information when writing about error-bars. My understanding (not good) is that they are not describing the quantiles of a distribution. Rather they are expressing information about the estimated distribution of the mean (or something like that--it's been a long time)


John Kane
Kingston ON Canada

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

The code is the same as the last one I showed, except I used geom_violin() instead.

pp <- ggplot(dat1, aes(x = condition, y = t, color = gender, linetype = direction)) +

  geom_violin() +

  facet_wrap(~ location) +

  scale_color_manual(values = c("blue", "darkorange"))+

  theme_bw()+

  scale_y_continuous(breaks=seq(0.6,1.5,0.1))

And I have attached the resulted violin plot.

One big difference is that in order to plot violin plots, we need the un-summarized original data in the long format, rather than the summarized data as we used in the example above. But I am sorry that I can not share the original data, so in order to plot a whole violin plot, you just need to simulate out the data as we previously did.

I see that violin plots by ggplot2 does not contain the quantile information, but it would be nice if it did. Maybe I should just add another geom_errorbar for that.

祝好,
========================
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 Wed, Feb 18, 2015 at 12:57 AM, John Kane <jrkrideau at inbox.com> wrote:

	I had never thought of violins.  It might be interesting. However , I still think there maybe some use out of the 4-panel approach. 

 What does your vioilin code look like?

 Using Denis' code

 gg<- ggplot(dat1, aes(x = condition, y = t, color = gender, linetype =
 direction)) +
   geom_errorbar(aes(ymin = t - ci, ymax = t + ci),
                 position = position_dodge(width = 0.6), size = 1,
                 width = 0.5) +
   geom_point(position = position_dodge(width = 0.6), size = 2.5) +
   facet_wrap(direction ~ location) +
   scale_color_manual(values = c("blue", "darkorange"))+
   theme_bw()+
   scale_y_continuous(breaks=seq(0.6,1.5,0.1))
 gg

 John Kane
 Kingston ON Canada

 -----Original Message-----
 From: hyiltiz at gmail.com

Sent: Tue, 17 Feb 2015 01:20:06 +0800
 To: jrkrideau at inbox.com
 Subject: Re: [R] ggplot2 shifting bars to only overlap in groups

 Again, I come to think about violin plots which is more informative than the error bars. But for some reason, the violin in the *west* became way too slimmer than the *east* one, though the density plot tells me that is not necessarily the case. I am still trying to figure that out, and that would be even more irrelevant as long as *shifting bars in gorups*. So maybe I will come up with another post later when I got the solution.

 祝好,
 ========================
 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 Mon, Feb 16, 2015 at 9:59 PM, John Kane <jrkrideau at inbox.com> wrote:

         Lovely, a much more elegant solution.

  John Kane
  Kingston ON Canada

  -----Original Message-----
  From: hyiltiz at gmail.com
  Sent: Mon, 16 Feb 2015 02:30:09 +0800
  To: jrkrideau at inbox.com, djmuser at gmail.com
  Subject: Re: [R] ggplot2 shifting bars to only overlap in groups

 Thanks so much, John and Dennis (who did not respond in the mailing list for some reason). I feel quite obliged to keep you thinking about this. 

  I do agree that not using the bar chart with error bars is a better option. And since *condition* is an important ordinal factor for me, it would be much better to have *condition* be positioned at a relative order. Thus, only color coding it as John's latest solution would not be optimal. 

  It would have been better with the random data, but with my actual data, it does seem necessary to do a jitter for the *male* since it got clattered in the *west*. Here is the actual data along with the solution based on Dennis' code:

  ## data

  dat1 <-  structure(list(t = c(1.2454860689532, 0.627186899108052, 0.877176019393987, 

                         1.26720638917869, 1.16906482219006, 0.889738853288831, 0.852034797572489, 

                         1.30007600828822, 1.22896141479778, 0.820236562746995, 0.822197641624559, 

                         1.39529772379005, 1.10479557445486, 0.760017179713665, 0.761340230517717, 

                         1.11132156961026, 1.30042963441715, 0.811425854755042, 0.979421690403349, 

                         1.3297658281305, 1.13377482477157, 0.895243910826397, 0.874181486658082, 

                         1.15728885642541, 1.11121780853125, 0.703348405369258, 0.850897112058048, 

                         1.14260584106012, 1.09383015337114, 0.911388765620587, 0.84622335453925, 

                         1.09847968194129), condition = structure(c(4L, 4L, 4L, 4L, 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), .Label = c("c1", 

                                                                    "c2", "c2", "c4"), 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"), location = structure(c(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, 1L), .Label = c("east", 

    "west"), class = "factor"), gender = structure(c(2L, 2L, 2L, 

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

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

    "female"), class = "factor"), ci = c(0.0307396796826649, 0.0302954863637637, 

  0.0400142340797275, 0.0527186825100342, 0.051675810189946, 0.0368383294010065, 

  0.0404823188495183, 0.0526312391852324, 0.0347332720922338, 0.0354587857740343, 

  0.0303368490163547, 0.0710445198259065, 0.0229339653012889, 0.0261217906562281, 

  0.0285673216713352, 0.0351642108247828, 0.0542657646932069, 0.0566816739316165, 

  0.0481239729953889, 0.0434272572423839, 0.0497366325101659, 0.0342004255233646, 

  0.0349733697554762, 0.0405364256564456, 0.0478372176424872, 0.0341294939361437, 

  0.0424566961614424, 0.0463489561778199, 0.0191707406475215, 0.0501106812754005, 

  0.0321562411182704, 0.0218613299095178)), .Names = c("t", "condition", 

  "direction", "location", "gender", "ci"), row.names = c(NA, -32L

  ), class = "data.frame")

  pp <- ggplot(dat1, aes(x = condition, y = t, color = gender, linetype = direction)) +

    geom_errorbar(aes(ymin = t - ci, ymax = t + ci),

                  position = position_dodge(width = 0.6), size = 1,

                  width = 0.5) +

    geom_point(position = position_dodge(width = 0.6), size = 2.5) +

    facet_wrap(~ location) +

    scale_color_manual(values = c("blue", "darkorange"))+

    theme_bw()+

    scale_y_continuous(breaks=seq(0.6,1.5,0.1))

  pp

  ## EOF

  I have also attached the output. 

   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

 ____________________________________________________________
  FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!

 Check it out at http://www.inbox.com/earth [http://www.inbox.com/earth] [http://www.inbox.com/earth [http://www.inbox.com/earth]]

 ____________________________________________________________
 FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
 Check it out at http://www.inbox.com/earth [http://www.inbox.com/earth]

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!



More information about the R-help mailing list