Discussion:
simple bar plot
Richard Sherman
2018-11-05 16:54:42 UTC
Permalink
Hi,


I don't like being outwitted by ggplot2, but .. OK, I'm outwitted by
ggplot2.

My data are (dput-ed from a data frame called br)

structure(list(Year = c(1960L, 2017L), Under.15 = c(0.1, 0),
Age.15.19 = c(15.4, 4.1), Age.20.24 = c(36.8, 17.9), Age.25.29 = c(24.4,
28.7), Age.30.34 = c(13.8, 29.5), Age.35.39 = c(7.3, 16.2
), Age.40.44 = c(2.1, 3.3), Age.45. = c(0.1, 0.2)), row.names = 1:2, class
= "data.frame")

which I melt to get a data frame called br.melt:

structure(list(Year = c(1960L, 2017L, 1960L, 2017L, 1960L, 2017L,
1960L, 2017L, 1960L, 2017L, 1960L, 2017L, 1960L, 2017L, 1960L,
2017L), variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L), .Label = c("Under.15", "Age.15.19",
"Age.20.24", "Age.25.29", "Age.30.34", "Age.35.39", "Age.40.44",
"Age.45."), class = "factor"), value = c(0.1, 0, 15.4, 4.1, 36.8,
17.9, 24.4, 28.7, 13.8, 29.5, 7.3, 16.2, 2.1, 3.3, 0.1, 0.2)), row.names =
c(NA,
-16L), class = "data.frame")
br.melt
Year variable value
1 1960 Under.15 0.1
2 2017 Under.15 0.0
3 1960 Age.15.19 15.4
4 2017 Age.15.19 4.1
5 1960 Age.20.24 36.8
6 2017 Age.20.24 17.9
7 1960 Age.25.29 24.4
8 2017 Age.25.29 28.7
9 1960 Age.30.34 13.8
10 2017 Age.30.34 29.5
11 1960 Age.35.39 7.3
12 2017 Age.35.39 16.2
13 1960 Age.40.44 2.1
14 2017 Age.40.44 3.3
15 1960 Age.45. 0.1
16 2017 Age.45. 0.2

I want a bar graph with the "dodge" position, giving 'value' on the y-axis,
'variable' on the x-axis, and bars "dodged" by Year.

Just "stacking" the bars I try like this, giving a legend that seems weird
to me as it includes years that don't exist in the data:

ggplot( data = br.melt ) +
geom_bar(
mapping = aes( x = variable , y = value , fill = Year),
stat = 'identity' ,
)

Trying to "dodge" the bars gives me something altogether unlike what I'm
looking for, with an oddly stacked-but-not-dodged graph, and the same
strange legend:

ggplot( data = br.melt ) +
geom_bar(
mapping = aes( x = variable , y = value , fill = Year),
stat = 'identity' ,
position = 'dodge'
)

It's just a bar graph, and here I am outwitted. Egad.
--
--
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.
Zack Weinberg
2018-11-05 18:29:48 UTC
Permalink
Post by Richard Sherman
structure(list(Year = c(1960L, 2017L, 1960L, 2017L, 1960L, 2017L,
1960L, 2017L, 1960L, 2017L, 1960L, 2017L, 1960L, 2017L, 1960L,
2017L), variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L), .Label = c("Under.15", "Age.15.19",
"Age.20.24", "Age.25.29", "Age.30.34", "Age.35.39", "Age.40.44",
"Age.45."), class = "factor"), value = c(0.1, 0, 15.4, 4.1, 36.8,
17.9, 24.4, 28.7, 13.8, 29.5, 7.3, 16.2, 2.1, 3.3, 0.1, 0.2)), row.names = c(NA,
-16L), class = "data.frame")
...
Post by Richard Sherman
ggplot( data = br.melt ) +
geom_bar(
mapping = aes( x = variable , y = value , fill = Year),
stat = 'identity' ,
position = 'dodge'
)
I believe your problem is that 'Year' is a numeric vector in the data
frame, but you're trying to use it as a categorical variable. Try
converting it into a factor:

br.melt$Year <- factor(br.melt$Year)
ggplot(br.melt) + geom_bar(aes(x=variable, y=value, fill=Year),
stat='identity', position='dodge')

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