Home > Software engineering >  How do I extend the x axis with string values and draw a rectangle in ggplot2? R
How do I extend the x axis with string values and draw a rectangle in ggplot2? R

Time:04-13

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!

CodePudding user response:

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