如何使用字符串值扩展 x 轴并在 ggplot2 中绘制矩形? R
How do I extend the x axis with string values and draw a rectangle in ggplot2? R
我想在 x 轴上再添加两个值(1 和 5000)并使用类似以下内容绘制一个矩形:
geom_rect(aes(xmin = 1,
xmax = 5000,
ymin = -Inf, ymax = Inf))
有没有办法将 x 轴值保持为字符串?
A tibble: 11 x 5
trainingSet testOn mean lci uci
<chr> <chr> <dbl> <dbl> <dbl>
1 1 to 5000 10000 0.930 0.927 0.934
2 1 to 5000 15000 0.932 0.930 0.935
3 1 to 5000 20000 0.932 0.929 0.936
4 1 to 5000 25000 0.935 0.931 0.938
5 1 to 5000 30000 0.934 0.930 0.939
6 1 to 5000 35000 0.488 0.486 0.490
7 1 to 5000 40000 0.498 0.496 0.500
8 1 to 5000 45000 0.489 0.487 0.491
9 1 to 5000 50000 0.484 0.481 0.487
10 1 to 5000 55000 0.493 0.490 0.496
11 1 to 5000 60000 0.481 0.478 0.484
源代码:
ggplot(data = confidence.intervals, aes(y = mean, x = testOn, color=trainingSet))+
geom_ribbon(aes(x= testOn, ymin=lci, ymax=uci, group=trainingSet, fill=trainingSet), alpha = 0.1, show.legend = FALSE)+
geom_line(aes(group=1))+
geom_point(size = 1.5)
提前致谢!
由于您的 testOn 列是一个字符列,我们必须 add_row()
两行用于“0”和“5000”(因为您的绘图中的 x 轴变得离散)。然后你可以通过更改为 factor 并使用 forcats.
中的 fct_relevel
将它们移到前面
代码
library(dplyr)
library(forcats)
library(ggplot)
confidence.intervals <- confidence.intervals %>%
add_row(trainingSet = rep("1 to 5000", 2), testOn = c("0", "5000")) %>%
mutate(testOn = fct_relevel(factor(testOn), "0", "5000"))
ggplot(data = confidence.intervals, aes(y = mean, x = testOn, color=trainingSet))+
geom_ribbon(aes(x = testOn, ymin = lci, ymax = uci, group = trainingSet, fill = trainingSet), alpha = 0.1, show.legend = FALSE)+
geom_line(aes(group = 1))+
geom_point(size = 1.5) +
geom_rect(aes(xmin = "0",
xmax = "5000",
ymin = -Inf, ymax = Inf), fill = "firebrick")
输出
数据
confidence.intervals <- structure(list(trainingSet = c("1 to 5000", "1 to 5000", "1 to 5000",
"1 to 5000", "1 to 5000", "1 to 5000", "1 to 5000", "1 to 5000",
"1 to 5000", "1 to 5000", "1 to 5000"), testOn = c("10000", "15000",
"20000", "25000", "30000", "35000", "40000", "45000", "50000",
"55000", "60000"), mean = c(0.93, 0.932, 0.932, 0.935, 0.934,
0.488, 0.498, 0.489, 0.484, 0.493, 0.481), lci = c(0.927, 0.93,
0.929, 0.931, 0.93, 0.486, 0.496, 0.487, 0.481, 0.49, 0.478),
uci = c(0.934, 0.935, 0.936, 0.938, 0.939, 0.49, 0.5, 0.491,
0.487, 0.496, 0.484)), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"))
我想在 x 轴上再添加两个值(1 和 5000)并使用类似以下内容绘制一个矩形:
geom_rect(aes(xmin = 1,
xmax = 5000,
ymin = -Inf, ymax = Inf))
有没有办法将 x 轴值保持为字符串?
A tibble: 11 x 5
trainingSet testOn mean lci uci
<chr> <chr> <dbl> <dbl> <dbl>
1 1 to 5000 10000 0.930 0.927 0.934
2 1 to 5000 15000 0.932 0.930 0.935
3 1 to 5000 20000 0.932 0.929 0.936
4 1 to 5000 25000 0.935 0.931 0.938
5 1 to 5000 30000 0.934 0.930 0.939
6 1 to 5000 35000 0.488 0.486 0.490
7 1 to 5000 40000 0.498 0.496 0.500
8 1 to 5000 45000 0.489 0.487 0.491
9 1 to 5000 50000 0.484 0.481 0.487
10 1 to 5000 55000 0.493 0.490 0.496
11 1 to 5000 60000 0.481 0.478 0.484
源代码:
ggplot(data = confidence.intervals, aes(y = mean, x = testOn, color=trainingSet))+
geom_ribbon(aes(x= testOn, ymin=lci, ymax=uci, group=trainingSet, fill=trainingSet), alpha = 0.1, show.legend = FALSE)+
geom_line(aes(group=1))+
geom_point(size = 1.5)
提前致谢!
由于您的 testOn 列是一个字符列,我们必须 add_row()
两行用于“0”和“5000”(因为您的绘图中的 x 轴变得离散)。然后你可以通过更改为 factor 并使用 forcats.
fct_relevel
将它们移到前面
代码
library(dplyr)
library(forcats)
library(ggplot)
confidence.intervals <- confidence.intervals %>%
add_row(trainingSet = rep("1 to 5000", 2), testOn = c("0", "5000")) %>%
mutate(testOn = fct_relevel(factor(testOn), "0", "5000"))
ggplot(data = confidence.intervals, aes(y = mean, x = testOn, color=trainingSet))+
geom_ribbon(aes(x = testOn, ymin = lci, ymax = uci, group = trainingSet, fill = trainingSet), alpha = 0.1, show.legend = FALSE)+
geom_line(aes(group = 1))+
geom_point(size = 1.5) +
geom_rect(aes(xmin = "0",
xmax = "5000",
ymin = -Inf, ymax = Inf), fill = "firebrick")
输出
数据
confidence.intervals <- structure(list(trainingSet = c("1 to 5000", "1 to 5000", "1 to 5000",
"1 to 5000", "1 to 5000", "1 to 5000", "1 to 5000", "1 to 5000",
"1 to 5000", "1 to 5000", "1 to 5000"), testOn = c("10000", "15000",
"20000", "25000", "30000", "35000", "40000", "45000", "50000",
"55000", "60000"), mean = c(0.93, 0.932, 0.932, 0.935, 0.934,
0.488, 0.498, 0.489, 0.484, 0.493, 0.481), lci = c(0.927, 0.93,
0.929, 0.931, 0.93, 0.486, 0.496, 0.487, 0.481, 0.49, 0.478),
uci = c(0.934, 0.935, 0.936, 0.938, 0.939, 0.49, 0.5, 0.491,
0.487, 0.496, 0.484)), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"))