Discussion:
change bar width to prevent bigger bars when data is missing
s***@gmail.com
2018-09-10 10:00:55 UTC
Permalink
Can some of you help me with the following problem?
I have data from different time point, but at the start I only have data of
1 treatment instead of 8.

Example data:
Time Treatment response
1 1 A 5
2 2 A 7
3 2 B 8
4 2 C 6
5 2 D 8
6 2 E 3
7 2 F 9
8 2 G 6
9 2 H 5
10 3 A 2
11 3 B 5
12 3 C 8
13 3 D 6
14 3 E 7
15 3 F 9
16 3 G 2
17 3 H 5

If you plot this data you get a graph that looks like:

a = ggplot(test, aes(x = Time, y=Response, fill=Treatment))
a +
geom_bar(stat = 'identity', position = 'dodge')


I would not like to add at time 1 for all other treatments a "0", but I
would like to decrease only the bar width of treatment A at time 1 to the
same as the bar width of the other bars in the other 2 timepoints.
Is this possible in ggplot2?
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brandon Hurr
2018-09-10 16:16:02 UTC
Permalink
This one comes up quite often. When you are missing combinations in your
data set it fills the space with the available variables. Most people do
not want this behavior, but it is the default. Luckily a relatively recent
addition to tidyr is very helpful for adding these missing data.

library(tidyverse)

#comic sans enabled for lack of reprex
test <- tibble(
Time = c(1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Treatment = c("A", "A", "B", "C", "D", "E", "F", "G", "H", "A", "B", "C",
"D", "E", "F", "G", "H"),
Response = c(5,7,8,6,8,3,9,6,5,2,5,8,6,7,9,2,5)
)

#verify problem, Yes
ggplot(test, aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')
[image: Screen Shot 2018-09-10 at 9.12.17 AM.png]
# use tidyr::complete to fill in the missing data
# Time is the grouping variable, with Treatment nested
test %>%
complete(Time, nesting(Treatment)) %>%
ggplot(., aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')

[image: Screen Shot 2018-09-10 at 9.12.28 AM.png]
HTH,
B
Post by s***@gmail.com
Can some of you help me with the following problem?
I have data from different time point, but at the start I only have data
of 1 treatment instead of 8.
Time Treatment response
1 1 A 5
2 2 A 7
3 2 B 8
4 2 C 6
5 2 D 8
6 2 E 3
7 2 F 9
8 2 G 6
9 2 H 5
10 3 A 2
11 3 B 5
12 3 C 8
13 3 D 6
14 3 E 7
15 3 F 9
16 3 G 2
17 3 H 5
a = ggplot(test, aes(x = Time, y=Response, fill=Treatment))
a +
geom_bar(stat = 'identity', position = 'dodge')
I would not like to add at time 1 for all other treatments a "0", but I
would like to decrease only the bar width of treatment A at time 1 to the
same as the bar width of the other bars in the other 2 timepoints.
Is this possible in ggplot2?
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
s***@gmail.com
2018-09-10 16:27:26 UTC
Permalink
Thank you!
I indeed know that this is possible, but then there appears a very big gap
between my 2 timepoints. I was wondering if it was possible to indeed
decrease the width of the bar, but not create this big gap. Do you know a
solution?
Post by Brandon Hurr
This one comes up quite often. When you are missing combinations in your
data set it fills the space with the available variables. Most people do
not want this behavior, but it is the default. Luckily a relatively recent
addition to tidyr is very helpful for adding these missing data.
library(tidyverse)
#comic sans enabled for lack of reprex
test <- tibble(
Time = c(1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Treatment = c("A", "A", "B", "C", "D", "E", "F", "G", "H", "A", "B", "C",
"D", "E", "F", "G", "H"),
Response = c(5,7,8,6,8,3,9,6,5,2,5,8,6,7,9,2,5)
)
#verify problem, Yes
ggplot(test, aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')
[image: Screen Shot 2018-09-10 at 9.12.17 AM.png]
# use tidyr::complete to fill in the missing data
# Time is the grouping variable, with Treatment nested
test %>%
complete(Time, nesting(Treatment)) %>%
ggplot(., aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')
[image: Screen Shot 2018-09-10 at 9.12.28 AM.png]
HTH,
B
Post by s***@gmail.com
Can some of you help me with the following problem?
I have data from different time point, but at the start I only have data
of 1 treatment instead of 8.
Time Treatment response
1 1 A 5
2 2 A 7
3 2 B 8
4 2 C 6
5 2 D 8
6 2 E 3
7 2 F 9
8 2 G 6
9 2 H 5
10 3 A 2
11 3 B 5
12 3 C 8
13 3 D 6
14 3 E 7
15 3 F 9
16 3 G 2
17 3 H 5
a = ggplot(test, aes(x = Time, y=Response, fill=Treatment))
a +
geom_bar(stat = 'identity', position = 'dodge')
I would not like to add at time 1 for all other treatments a "0", but I
would like to decrease only the bar width of treatment A at time 1 to the
same as the bar width of the other bars in the other 2 timepoints.
Is this possible in ggplot2?
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups
"ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Konstantinos L. Papageorgiou
2018-09-10 16:49:07 UTC
Permalink
hoping someone knows of a solution for that.
Post by s***@gmail.com
Thank you!
I indeed know that this is possible, but then there appears a very big
gap between my 2 timepoints. I was wondering if it was possible to
indeed decrease the width of the bar, but not create this big gap. Do
you know a solution?
This one comes up quite often. When you are missing combinations
in your data set it fills the space with the available variables.
Most people do not want this behavior, but it is the default.
Luckily a relatively recent addition to tidyr is very helpful for
adding these missing data.
library(tidyverse)
#comic sans enabled for lack of reprex
test <- tibble(
Time = c(1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Treatment = c("A", "A", "B", "C", "D", "E", "F", "G", "H", "A",
"B", "C", "D", "E", "F", "G", "H"),
Response = c(5,7,8,6,8,3,9,6,5,2,5,8,6,7,9,2,5)
)
#verify problem, Yes
ggplot(test, aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')
Screen Shot 2018-09-10 at 9.12.17 AM.png
# use tidyr::complete to fill in the missing data
# Time is the grouping variable, with Treatment nested
test %>%
complete(Time, nesting(Treatment)) %>%
ggplot(., aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')
Screen Shot 2018-09-10 at 9.12.28 AM.png
HTH,
B
Can some of you help me with the following problem?
I have data from different time point, but at the start I only
have data of 1 treatment instead of 8.
Time Treatment response
1 1 A 5
2 2 A 7
3 2 B 8
4 2 C 6
5 2 D 8
6 2 E 3
7 2 F 9
8 2 G 6
9 2 H 5
10 3 A 2
11 3 B 5
12 3 C 8
13 3 D 6
14 3 E 7
15 3 F 9
16 3 G 2
17 3 H 5
a = ggplot(test, aes(x = Time, y=Response, fill=Treatment))
a +
geom_bar(stat = 'identity', position = 'dodge')
I would not like to add at time 1 for all other treatments a
"0", but I would like to decrease only the bar width of
treatment A at time 1 to the same as the bar width of the
other bars in the other 2 timepoints.
Is this possible in ggplot2?
--
--
You received this message because you are subscribed to the
ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
<https://github.com/hadley/devtools/wiki/Reproducibility>
More options: http://groups.google.com/group/ggplot2
<http://groups.google.com/group/ggplot2>
---
You received this message because you are subscribed to the
Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from
For more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
Konstantinos L. Papageorgiou
Senior programmer analyst
mail: ***@eworx.gr

EWORX S.A.
66 Jean Moreas St
Athens 15231 - Greece
t: +30 210 614 8380
f: +30 210 614 8381
web: www.eworx.gr
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brandon Hurr
2018-09-10 17:34:29 UTC
Permalink
I tried playing with position_dodge2 and got something similar to what I
had above, but the single bar is centered instead of to the left:
library(tidyverse)

#comic sans enabled for lack of reprex
test <- tibble(
Time = c(1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Treatment = c("A", "A", "B", "C", "D", "E", "F", "G", "H", "A", "B", "C",
"D", "E", "F", "G", "H"),
Response = c(5,7,8,6,8,3,9,6,5,2,5,8,6,7,9,2,5)
)

ggplot(test, aes(x = as.factor(Time), y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = position_dodge2(preserve=
"single"))
[image: Screen Shot 2018-09-10 at 10.31.24 AM.png]

The closest thing I can think of to what you want is to use facets instead
of dodging:
ggplot(test, aes(x = Treatment, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', width = 0.9) +
facet_grid(.~Time, scales = "free_x", space = "free_x")
[image: Screen Shot 2018-09-10 at 10.32.48 AM.png]
I happen to think that the original is best. It clearly shows that you are
missing data. I realize this means your ink to information ratio is lower,
but knowing that you're missing data is often as important as anything else.


HTH,

B
Post by Konstantinos L. Papageorgiou
hoping someone knows of a solution for that.
Thank you!
I indeed know that this is possible, but then there appears a very big gap
between my 2 timepoints. I was wondering if it was possible to indeed
decrease the width of the bar, but not create this big gap. Do you know a
solution?
Post by Brandon Hurr
This one comes up quite often. When you are missing combinations in your
data set it fills the space with the available variables. Most people do
not want this behavior, but it is the default. Luckily a relatively recent
addition to tidyr is very helpful for adding these missing data.
library(tidyverse)
#comic sans enabled for lack of reprex
test <- tibble(
Time = c(1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Treatment = c("A", "A", "B", "C", "D", "E", "F", "G", "H", "A", "B", "C",
"D", "E", "F", "G", "H"),
Response = c(5,7,8,6,8,3,9,6,5,2,5,8,6,7,9,2,5)
)
#verify problem, Yes
ggplot(test, aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')
[image: Screen Shot 2018-09-10 at 9.12.17 AM.png]
# use tidyr::complete to fill in the missing data
# Time is the grouping variable, with Treatment nested
test %>%
complete(Time, nesting(Treatment)) %>%
ggplot(., aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')
[image: Screen Shot 2018-09-10 at 9.12.28 AM.png]
HTH,
B
Post by s***@gmail.com
Can some of you help me with the following problem?
I have data from different time point, but at the start I only have data
of 1 treatment instead of 8.
Time Treatment response
1 1 A 5
2 2 A 7
3 2 B 8
4 2 C 6
5 2 D 8
6 2 E 3
7 2 F 9
8 2 G 6
9 2 H 5
10 3 A 2
11 3 B 5
12 3 C 8
13 3 D 6
14 3 E 7
15 3 F 9
16 3 G 2
17 3 H 5
a = ggplot(test, aes(x = Time, y=Response, fill=Treatment))
a +
geom_bar(stat = 'identity', position = 'dodge')
I would not like to add at time 1 for all other treatments a "0", but I
would like to decrease only the bar width of treatment A at time 1 to the
same as the bar width of the other bars in the other 2 timepoints.
Is this possible in ggplot2?
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google
Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Konstantinos L. Papageorgiou
Senior programmer analyst
EWORX S.A.
66 Jean Moreas St
Athens 15231 - Greece
t: +30 210 614 8380
f: +30 210 614 8381
web: www.eworx.gr
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
s***@gmail.com
2018-09-10 17:51:17 UTC
Permalink
Thank you! This is very helpful!

In my real data set I actually do not have really missing data as my first
time point is at 0 days and afterwards I measured the treatments that
caused a change, that is why I wanted to know the possibilities, but indeed
I agree with you that showing missing data is most of the times better (but
one bar in the middle looks better than at the left side!)
Post by Brandon Hurr
I tried playing with position_dodge2 and got something similar to what I
library(tidyverse)
#comic sans enabled for lack of reprex
test <- tibble(
Time = c(1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Treatment = c("A", "A", "B", "C", "D", "E", "F", "G", "H", "A", "B", "C",
"D", "E", "F", "G", "H"),
Response = c(5,7,8,6,8,3,9,6,5,2,5,8,6,7,9,2,5)
)
ggplot(test, aes(x = as.factor(Time), y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = position_dodge2(preserve=
"single"))
[image: Screen Shot 2018-09-10 at 10.31.24 AM.png]
The closest thing I can think of to what you want is to use facets instead
ggplot(test, aes(x = Treatment, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', width = 0.9) +
facet_grid(.~Time, scales = "free_x", space = "free_x")
[image: Screen Shot 2018-09-10 at 10.32.48 AM.png]
I happen to think that the original is best. It clearly shows that you are
missing data. I realize this means your ink to information ratio is lower,
but knowing that you're missing data is often as important as anything else.
HTH,
B
On Mon, Sep 10, 2018 at 9:49 AM Konstantinos L. Papageorgiou <
Post by Konstantinos L. Papageorgiou
hoping someone knows of a solution for that.
Thank you!
I indeed know that this is possible, but then there appears a very big
gap between my 2 timepoints. I was wondering if it was possible to indeed
decrease the width of the bar, but not create this big gap. Do you know a
solution?
Post by Brandon Hurr
This one comes up quite often. When you are missing combinations in your
data set it fills the space with the available variables. Most people do
not want this behavior, but it is the default. Luckily a relatively recent
addition to tidyr is very helpful for adding these missing data.
library(tidyverse)
#comic sans enabled for lack of reprex
test <- tibble(
Time = c(1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Treatment = c("A", "A", "B", "C", "D", "E", "F", "G", "H", "A", "B",
"C", "D", "E", "F", "G", "H"),
Response = c(5,7,8,6,8,3,9,6,5,2,5,8,6,7,9,2,5)
)
#verify problem, Yes
ggplot(test, aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')
[image: Screen Shot 2018-09-10 at 9.12.17 AM.png]
# use tidyr::complete to fill in the missing data
# Time is the grouping variable, with Treatment nested
test %>%
complete(Time, nesting(Treatment)) %>%
ggplot(., aes(x = Time, y=Response, fill=Treatment)) +
geom_bar(stat = 'identity', position = 'dodge')
[image: Screen Shot 2018-09-10 at 9.12.28 AM.png]
HTH,
B
Post by s***@gmail.com
Can some of you help me with the following problem?
I have data from different time point, but at the start I only have
data of 1 treatment instead of 8.
Time Treatment response
1 1 A 5
2 2 A 7
3 2 B 8
4 2 C 6
5 2 D 8
6 2 E 3
7 2 F 9
8 2 G 6
9 2 H 5
10 3 A 2
11 3 B 5
12 3 C 8
13 3 D 6
14 3 E 7
15 3 F 9
16 3 G 2
17 3 H 5
a = ggplot(test, aes(x = Time, y=Response, fill=Treatment))
a +
geom_bar(stat = 'identity', position = 'dodge')
I would not like to add at time 1 for all other treatments a "0", but I
would like to decrease only the bar width of treatment A at time 1 to the
same as the bar width of the other bars in the other 2 timepoints.
Is this possible in ggplot2?
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google
Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups
"ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Konstantinos L. Papageorgiou
Senior programmer analyst
EWORX S.A.
66 Jean Moreas St
Athens 15231 - Greece
t: +30 210 614 8380
f: +30 210 614 8381
web: www.eworx.gr
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups
"ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...