如何使用字符串值扩展X轴并在GGPLOT2中绘制矩形? r

发布于 2025-01-20 00:17:35 字数 1142 浏览 2 评论 0原文

我想在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)

预先感谢您!

I would like to add two more values (1 and 5000) on the x-axis and draw a rectangle using something like:

geom_rect(aes(xmin = 1, 
              xmax = 5000,
              ymin = -Inf, ymax = Inf)) 

Is there a way to do it keeping the x-axis values as strings?

 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

Source code:

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)

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2025-01-27 00:17:35

由于您的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")

输出

”在此处输入图像描述”

data

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"))

Since your testOn column is a character column, we have to add_row() two rows for "0" and "5000" (since the x axis in your plot becomes discrete). Then you can move them to the front by changing to factor and using fct_relevel from forcats.

Code

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")

Output

enter image description here

Data

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"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文