我试图改变我的传奇的顺序,但没有成功。它应该去:S8 S9 S10,但是去:S10 S8 S9。我认为问题出在S10的“ 1”上,我已经尝试了多种解决方案,但是似乎没有任何效果。
我的代码和数据:
library(ggplot2)
library(dplyr)
library(tidyverse)
data <- structure(list(S8 = c(23.887553, 23.91512, 23.893336, 23.902068,
23.990071, 23.901245, 23.893439, 23.883056, 23.966471, 23.909143,
23.925841, 23.907297, 23.923717, 23.924523, 23.930051, 23.940652,
23.906786, 23.895772, 23.937286, 23.936345, 23.942015, 23.924175,
23.957059, 23.923146, 23.889771, 23.925399, 23.931965, 23.909739,
23.91116, 23.870361),
S9 = c(23.914722, 23.895847, 23.916466,
23.927832, 23.940209, 23.904665, 23.921734, 23.938238, 23.938333,
23.862013, 23.899884, 23.960764, 23.912089, 23.963192, 23.925241,
23.899518, 23.893405, 23.924374, 23.946418, 23.915391, 23.92748,
23.869415, 23.914131, 23.916271, 23.923982, 23.922528, 23.872999,
23.931974, 23.883386, 23.869507),
S10 = c(23.91312, 23.876808,
23.899456, 23.919904, 23.857124, 23.881269, 23.896357, 23.876635,
23.853636, 23.86339, 23.905828, 23.883948, 23.888408, 23.87092,
23.865198, 23.870296, 23.892394, 23.847406, 23.894526, 23.825204,
23.831528, 23.860533, 23.86715, 23.902507, 23.846072, 23.849864,
23.864199, 23.872008, 23.865567, 23.822329),
year = c(1L,1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L)),
class = "data.frame", row.names = c(NA, -30L))
df <- data %>%
pivot_longer(cols = c(S8,S9,S10), names_to = "variable", values_to = "value")
ggplot(df[df$year>1,], aes(x = year, y = value, color = variable)) +
stat_summary(geom = "line", fun = mean, linetype = "solid") +
stat_summary(geom = "ribbon", fun.min = function(z) { quantile(z,0.05) },
fun.max = function(z) { quantile(z,0.95) }, alpha=.1)+
labs(x="Year",y="Total Points (millions)")+
theme(axis.title.y = element_text(size="12"),axis.title.x = element_text(size="12"),
legend.text = element_text(size = 12),axis.text.x=element_text(size=10),
axis.text.y=element_text(size=10),legend.title=element_blank(),legend.position="top",
legend.box.background = element_rect(),legend.box.margin = margin(1, 1, 1, 1))
答案 0 :(得分:1)
将variable
转换为一个因子并手动设置级别顺序:
library(ggplot2)
library(dplyr)
#>
#> Attache Paket: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyverse)
data <- structure(list(S8 = c(23.887553, 23.91512, 23.893336, 23.902068,
23.990071, 23.901245, 23.893439, 23.883056, 23.966471, 23.909143,
23.925841, 23.907297, 23.923717, 23.924523, 23.930051, 23.940652,
23.906786, 23.895772, 23.937286, 23.936345, 23.942015, 23.924175,
23.957059, 23.923146, 23.889771, 23.925399, 23.931965, 23.909739,
23.91116, 23.870361),
S9 = c(23.914722, 23.895847, 23.916466,
23.927832, 23.940209, 23.904665, 23.921734, 23.938238, 23.938333,
23.862013, 23.899884, 23.960764, 23.912089, 23.963192, 23.925241,
23.899518, 23.893405, 23.924374, 23.946418, 23.915391, 23.92748,
23.869415, 23.914131, 23.916271, 23.923982, 23.922528, 23.872999,
23.931974, 23.883386, 23.869507),
S10 = c(23.91312, 23.876808,
23.899456, 23.919904, 23.857124, 23.881269, 23.896357, 23.876635,
23.853636, 23.86339, 23.905828, 23.883948, 23.888408, 23.87092,
23.865198, 23.870296, 23.892394, 23.847406, 23.894526, 23.825204,
23.831528, 23.860533, 23.86715, 23.902507, 23.846072, 23.849864,
23.864199, 23.872008, 23.865567, 23.822329),
year = c(1L,1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L)),
class = "data.frame", row.names = c(NA, -30L))
df <- data %>%
pivot_longer(cols = c(S8,S9,S10), names_to = "variable", values_to = "value")
df <- df %>%
mutate(variable = factor(variable, levels = c("S8", "S9", "S10")))
ggplot(df[df$year>1,], aes(x = year, y = value, color = variable)) +
stat_summary(geom = "line", fun = mean, linetype = "solid") +
stat_summary(geom = "ribbon", fun.min = function(z) { quantile(z,0.05) },
fun.max = function(z) { quantile(z,0.95) }, alpha=.1)+
labs(x="Year",y="Total Points (millions)")+
theme(axis.title.y = element_text(size="12"),axis.title.x = element_text(size="12"),
legend.text = element_text(size = 12),axis.text.x=element_text(size=10),
axis.text.y=element_text(size=10),legend.title=element_blank(),legend.position="top",
legend.box.background = element_rect(),legend.box.margin = margin(1, 1, 1, 1))
由reprex package(v0.3.0)于2020-03-18创建