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!
Hörmetjan Yiltiz
2015-Feb-15 18:30 UTC
[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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: last.png
Type: image/png
Size: 5512 bytes
Desc: not available
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20150216/3ef5f36c/attachment.png>