Reputation: 3310
I'm trying to plot answers on a scale from 1 to 5, and I would like my plot in ggplot2
to range from 1 to 5. When I change scale_y_continuous(limits = c(1, 5))
, however, the data disappear. Any ideas how to fix this (other than the hack-y way of subtracting 1 from my values and relabeling)?
Reproducible example:
dat <- structure(list(year = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L), .Label = c("2011", "2012", "2013", "2015"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L), .Label = c("instructor.knowledge",
"instructor.enthusiastic", "instructor.clear", "instructor.prepared",
"instructor.feedback", "instructor.out.of.class", "class.dynamic"
), class = "factor"), value = c(5, 4.75, 5, 4.75, 5, 5, 4.85714285714286,
4.75, 4.75, 4.75, 4.71428571428571, 3.75, 5, 4.75, 5, 4.5, 5,
4.75, NA, 5, 5, 5, NA, 4.5, 5, 5, NA, 4.5)), row.names = c(NA,
-28L), .Names = c("year", "variable", "value"), class = "data.frame")
library(ggplot2)
ggplot(dat, aes(x = variable, y = value, fill = year)) +
geom_bar(position = "dodge", stat = "identity") +
scale_y_continuous(name = "Average score across all respondents",
limits = c(1, 5), # fails
# limits = c(0, 5), # succeeds
breaks = 1:5)
Upvotes: 2
Views: 590
Reputation: 93811
Just as another option, your data might be easier to understand as a faceted line plot:
ggplot(dat, aes(x = year, y = value, group=1)) +
geom_point() +
geom_line() +
scale_y_continuous(name = "Average score across all respondents",
limits = c(1, 5), # fails
# limits = c(0, 5), # succeeds
breaks = 1:5) +
facet_grid(. ~ variable)
Upvotes: 3
Reputation: 37879
You just need to set the oob = rescale_none
argument and it will work:
library(scales)
library(ggplot2)
ggplot(dat, aes(x = variable, y = value, fill = year)) +
geom_bar(position = "dodge", stat = "identity") +
scale_y_continuous(name = "Average score across all respondents",
limits = c(1, 5),
oob = rescale_none)
Make sure to attach the scales
package for the oob = rescale_none
to work.
Upvotes: 4