Reputation: 2351
graph <- structure(list(Obstacle = structure(c(4L, 2L, 3L, 1L), .Label = c("Major Obstacle",
"Minor Obstacle", "Moderate Obstacle", "No Obstacle", "Total"
), class = "factor"), `Crime = 1` = c(0.842430787355193, 0.724891824185835,
0.692470837751856, 0.673805601317957), `Crime = 2` = c(0.157569212644807,
0.275108175814165, 0.307529162248144, 0.326194398682043)), row.names = c(NA,
4L), class = "data.frame")
graph <- melt(graph, id="Obstacle")
Obstacle Crime = 1 Crime = 2
1 No Obstacle 0.8424308 0.1575692
2 Minor Obstacle 0.7248918 0.2751082
3 Moderate Obstacle 0.6924708 0.3075292
4 Major Obstacle 0.6738056 0.3261944
If I do this, I get colours and a legend.
graph %>%
mutate(Obstacle = fct_relevel(Obstacle, "No Obstacle", "Minor Obstacle", "Moderate Obstacle", "Major Obstacle")) %>%
ggplot(aes(x = Obstacle, y=value, colour=variable, group = variable)) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 20)) +
geom_line()
I thought to make it black and white like this, but then the lines are identical and there is no longer a legend. What am I doing wrong?
graph %>%
mutate(Obstacle = fct_relevel(Obstacle, "No Obstacle", "Minor Obstacle", "Moderate Obstacle", "Major Obstacle")) %>%
ggplot(aes(x = Obstacle, y=value, fill=variable, group = variable)) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 20)) +
scale_fill_grey() +
geom_line()
Upvotes: 0
Views: 48
Reputation: 7858
Try this:
library(dplyr)
library(ggplot2)
library(forcats)
graph %>%
# reshape2::melt(graph, id="Obstacle") %>%
mutate(Obstacle = fct_relevel(Obstacle, "No Obstacle", "Minor Obstacle", "Moderate Obstacle", "Major Obstacle")) %>%
ggplot(aes(x = Obstacle, y=value, colour=variable, group = variable)) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 20)) +
geom_line()
graph %>%
# reshape2::melt(graph, id="Obstacle") %>%
mutate(Obstacle = fct_relevel(Obstacle, "No Obstacle", "Minor Obstacle", "Moderate Obstacle", "Major Obstacle")) %>%
ggplot(aes(x = Obstacle, y=value, colour=variable, group = variable)) +
geom_line() +
scale_y_continuous(breaks = scales::pretty_breaks(n = 20)) +
scale_colour_grey()
In your second ggplot
you mixed up fill
and colour
. Set up:
colour=variable
inside aes
instead of fill=variable
scale_colour_grey
instead of scale_fill_gray
Upvotes: 1