[英]Legend dissapearing when trying to change colours to grey scale ggplot2
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
如果我这样做,我会得到颜色和图例。
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()
我想把它变成这样的黑白,但后来线条相同,不再有传说。 我究竟做错了什么?
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()
尝试这个:
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()
在您的第二个ggplot
,您混合了fill
和colour
。 设置:
colour=variable
在aes
中而不是fill=variable
scale_colour_grey
而不是scale_fill_gray
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.