[R] ggplot2 shifting bars to only overlap in groups

John Kane jrkrideau at inbox.com
Tue Feb 17 17:57:36 CET 2015


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]

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



More information about the R-help mailing list