Creating cut off points on X axis using ggplot

Hi, I'm trying to restrict my x axis to cut off points so the graph it is within 0.5 to 3. So to cut off everything that is not in the range. So for example, the x axis would end at 3 sec and the lines after that in the plot would be cut down.

Also, how would I remove the grid lines?

Any other tips to improve this plot to make it publish worthy?

Thanks

Code Below:

library(tidyverse)
RCom <-heartrateR

aggregates and creates mean across all

RCom %>%
group_by(ID Number) %>%
summarize_if(is.numeric, mean) -> RCom
View(RCom)
data2 <- RCom

pivots

data2%>%
pivot_longer(!"ID Number", names_to = "time", values_to = "HR")%>%
mutate(time = as.numeric(time)) -> data3

plot (could use "linetype here" )

data3 %>%
ggplot(aes(x = time, y = HR, color = ID Number)) +
geom_line() + labs(x = "Seconds", y = " Heart Rate (bpm)")

heartrateR <- tibble::tribble(
                       ~V1,  ~V2,  ~V3,  ~V4,  ~V5,  ~V6,  ~V7,  ~V8,  ~V9, ~V10, ~V11, ~V12,
               "ID Number",   0L,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L,  10L,
                   "CD",  66L,  66L,  64L,  64L,  58L,  58L,  58L,  57L,  56L,  56L,  57L,
                   "SS",  85L,  84L,  83L,  81L,  80L,  79L,  78L,  78L,  79L,  80L,  80L,
                   "CD", 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L,
                   "CD", 115L, 115L, 114L, 114L, 114L, 114L, 112L, 111L, 110L, 109L, 109L,
                  "SS",  63L,  63L,  63L,  63L,  77L,  78L,  80L,  77L,  76L,  67L,  67L,
                   "CD",  84L,  63L,  62L,  62L,  63L,  58L,  58L,  54L,  62L,  66L,  66L,
                  "SS",  49L,  48L,  46L,  46L,  45L,  46L,  45L,  45L,  45L,  45L,  46L,
                  "CD",  70L,  69L,  68L,  68L,  69L,  69L,  69L,  69L,  68L,  69L,  70L,
                   "CD",  83L,  84L,  84L,  84L,  84L,  84L,  82L,  82L,  82L,  82L,  81L,
                   "SS",  79L,  78L,  67L,  67L,  68L,  62L,  62L,  62L,  63L,  72L,  72L,
                   "CD", 240L, 202L, 141L, 106L,  89L,  74L,  71L,  71L,  71L,  72L,  74L,
                   "SS",  78L,  78L,  77L,  76L,  75L,  75L,  75L,  75L,  76L,  76L,  76L,
                  "CD",  66L,  66L,  66L,  66L,  66L,  66L,  67L,  67L,  67L,  67L,  68L,
                   "CD",  66L,  66L,  76L,  76L,  76L,  77L,  75L,  75L,  75L,  75L,  76L
               )

I do not think your posted heartrateR tibble is correct. It does not have a column named ID Number so the group_by() near the beginning of your code throws an error. Please just post the output of

dput(data3)

since your question is about the graph and not about how data3 is generated. Please put lines with three back ticks just before and after the pasted output of dput(), like this
```
Output of dput()

```

reprix chuck of data for communtiy

heartrateR <- tibble::tribble(
~V1, ~V2, ~V3, ~V4, ~V5, ~V6, ~V7, ~V8, ~V9, ~V10, ~V11, ~V12,
"ID Number", 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
"CD", 66L, 66L, 64L, 64L, 58L, 58L, 58L, 57L, 56L, 56L, 57L,
"SS", 85L, 84L, 83L, 81L, 80L, 79L, 78L, 78L, 79L, 80L, 80L,
"CD", 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L,
"CD", 115L, 115L, 114L, 114L, 114L, 114L, 112L, 111L, 110L, 109L, 109L,
"SS", 63L, 63L, 63L, 63L, 77L, 78L, 80L, 77L, 76L, 67L, 67L,
"CD", 84L, 63L, 62L, 62L, 63L, 58L, 58L, 54L, 62L, 66L, 66L,
"SS", 49L, 48L, 46L, 46L, 45L, 46L, 45L, 45L, 45L, 45L, 46L,
"CD", 70L, 69L, 68L, 68L, 69L, 69L, 69L, 69L, 68L, 69L, 70L,
"CD", 83L, 84L, 84L, 84L, 84L, 84L, 82L, 82L, 82L, 82L, 81L,
"SS", 79L, 78L, 67L, 67L, 68L, 62L, 62L, 62L, 63L, 72L, 72L,
"CD", 240L, 202L, 141L, 106L, 89L, 74L, 71L, 71L, 71L, 72L, 74L,
"SS", 78L, 78L, 77L, 76L, 75L, 75L, 75L, 75L, 76L, 76L, 76L,
"CD", 66L, 66L, 66L, 66L, 66L, 66L, 67L, 67L, 67L, 67L, 68L,
"CD", 66L, 66L, 76L, 76L, 76L, 77L, 75L, 75L, 75L, 75L, 76L
)
head(heartrateR)

Sorry found the mistake . Forgot "cols = ". the code below will work

library(tidyverse)

RCom <-heartrateR

aggregates and creates mean across all

RCom %>%
group_by(ID Number) %>%
summarize_if(is.numeric, mean) -> RCom

View(RCom)
data2 <- RCom

pivots

data2%>%
pivot_longer(cols = !"ID Number", names_to = "time", values_to = "HR")%>%
mutate(time = as.numeric(time)) -> data3

plot (could use "linetype here" )

data3 %>%
ggplot(aes(x = time, y = HR, color = ID Number)) +
geom_line() + labs(x = "Seconds", y = " Heart Rate (bpm)")

The first part of your code still throws an error.

library(dplyr)
library(tibble)
heartrateR <- tibble::tribble(
  ~V1, ~V2, ~V3, ~V4, ~V5, ~V6, ~V7, ~V8, ~V9, ~V10, ~V11, ~V12,
  "ID Number", 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
  "CD", 66L, 66L, 64L, 64L, 58L, 58L, 58L, 57L, 56L, 56L, 57L,
  "SS", 85L, 84L, 83L, 81L, 80L, 79L, 78L, 78L, 79L, 80L, 80L,
  "CD", 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L,
  "CD", 115L, 115L, 114L, 114L, 114L, 114L, 112L, 111L, 110L, 109L, 109L,
  "SS", 63L, 63L, 63L, 63L, 77L, 78L, 80L, 77L, 76L, 67L, 67L,
  "CD", 84L, 63L, 62L, 62L, 63L, 58L, 58L, 54L, 62L, 66L, 66L,
  "SS", 49L, 48L, 46L, 46L, 45L, 46L, 45L, 45L, 45L, 45L, 46L,
  "CD", 70L, 69L, 68L, 68L, 69L, 69L, 69L, 69L, 68L, 69L, 70L,
  "CD", 83L, 84L, 84L, 84L, 84L, 84L, 82L, 82L, 82L, 82L, 81L,
  "SS", 79L, 78L, 67L, 67L, 68L, 62L, 62L, 62L, 63L, 72L, 72L,
  "CD", 240L, 202L, 141L, 106L, 89L, 74L, 71L, 71L, 71L, 72L, 74L,
  "SS", 78L, 78L, 77L, 76L, 75L, 75L, 75L, 75L, 76L, 76L, 76L,
  "CD", 66L, 66L, 66L, 66L, 66L, 66L, 67L, 67L, 67L, 67L, 68L,
  "CD", 66L, 66L, 76L, 76L, 76L, 77L, 75L, 75L, 75L, 75L, 76L
)
RCom <-heartrateR
#aggregates and creates mean across all

RCom %>%
  group_by(`ID Number`) %>%
  summarize_if(is.numeric, mean) -> RCom
#> Error in `group_by()`:
#> ! Must group by variables found in `.data`.
#> x Column `ID Number` is not found.

Created on 2022-04-22 by the reprex package (v0.2.1)

Stress<-tibble::tribble(
  ~V1,  ~V2,  ~V3,  ~V4,  ~V5,  ~V6,  ~V7,  ~V8,  ~V9, ~V10, ~V11, ~V12, ~V13, ~V14, ~V15, ~V16, ~V17,
  "ID Number",   0L,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L,  10L,  11L,  12L,  13L,  14L,  15L,
  "CD-Female",  66L,  66L,  64L,  64L,  58L,  58L,  58L,  57L,  56L,  56L,  57L,  57L,  57L,  57L,  57L,  57L,
  "CD-Female", 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 104L,
  "CD-Female", 115L, 115L, 114L, 114L, 114L, 114L, 112L, 111L, 110L, 109L, 109L, 108L, 109L, 110L, 111L, 112L,
  "CD-Female",  84L,  63L,  62L,  62L,  63L,  58L,  58L,  54L,  62L,  66L,  66L,  60L,  60L,  51L,  54L,  54L,
  "CD-Female",  83L,  84L,  84L,  84L,  84L,  84L,  82L,  82L,  82L,  82L,  81L,  82L,  83L,  82L,  82L,  82L,
  "CD-Female", 240L, 202L, 141L, 106L,  89L,  74L,  71L,  71L,  71L,  72L,  74L,  77L,  77L,  70L,  68L,  68L,
  "CD-Female",  66L,  66L,  76L,  76L,  76L,  77L,  75L,  75L,  75L,  75L,  76L,  76L,  77L,  79L,  79L,  77L,
  "CD-Female",  66L,  68L,  68L,  67L,  66L,  67L,  68L,  69L,  69L,  68L,  68L,  68L,  70L,  72L,  71L,  69L,
  "CD-Female",  65L,  65L,  65L,  65L,  65L,  65L,  66L,  66L,  65L,  65L,  66L,  66L,  67L,  67L,  68L,  69L,
  "CD-Male",  70L,  69L,  68L,  68L,  69L,  69L,  69L,  69L,  68L,  69L,  70L,  71L,  72L,  72L,  73L,  74L,
  "CD-Male",  66L,  66L,  66L,  66L,  66L,  66L,  67L,  67L,  67L,  67L,  68L,  68L,  68L,  67L,  67L,  67L,
  "SS-Female",  67L,  65L,  65L,  64L,  58L,  61L,  55L,  60L,  60L,  60L,  60L,  66L,  66L,  72L,  72L,  82L,
  "SS-Female",  78L,  78L,  77L,  76L,  75L,  75L,  75L,  75L,  76L,  76L,  76L,  76L,  77L,  76L,  76L,  77L,
  "SS-Female",  79L,  78L,  67L,  67L,  68L,  62L,  62L,  62L,  63L,  72L,  72L,  82L,  81L,  82L,  83L,  82L,
  "SS-Female",  85L,  84L,  83L,  81L,  80L,  79L,  78L,  78L,  79L,  80L,  80L,  81L,  81L,  81L,  80L,  80L,
  "SS-Male",  63L,  63L,  63L,  63L,  77L,  78L,  80L,  77L,  76L,  67L,  67L,  62L,  57L,  57L,  57L,  48L,
  "SS-Male",  49L,  48L,  46L,  46L,  45L,  46L,  45L,  45L,  45L,  45L,  46L,  46L,  47L,  48L,  48L,  50L
)
head(Stress)
#> # A tibble: 6 × 17
#>   V1        V2    V3    V4    V5    V6    V7    V8    V9   V10   V11   V12   V13
#>   <chr>  <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1 ID Nu…     0     1     2     3     4     5     6     7     8     9    10    11
#> 2 CD-Fe…    66    66    64    64    58    58    58    57    56    56    57    57
#> 3 CD-Fe…   103   103   103   103   103   103   103   103   103   103   103   103
#> 4 CD-Fe…   115   115   114   114   114   114   112   111   110   109   109   108
#> 5 CD-Fe…    84    63    62    62    63    58    58    54    62    66    66    60
#> 6 CD-Fe…    83    84    84    84    84    84    82    82    82    82    81    82
#> # … with 4 more variables: V14 <int>, V15 <int>, V16 <int>, V17 <int>

Stress %>%
group_by(V1) %>%
summarize_if(is.numeric, mean) %>%
pivot_longer(-V1) %>%
filter(V1 != "ID Number") %>%
mutate(time = str_sub(name, 2) %>% as.numeric()) %>%
ggplot() +
aes(time, value, color = V1) +
geom_line()

I changed it up because that code wasn't consistently working. How would you cut it at 5 and 15. In reference to the orginal question

Here are three versions of the plot with some different options for setting the x axis limits and removing the grid. You can see the many elements of the theme that can be changed at

library(dplyr)
library(tibble)
library(ggplot2)
library(tidyr)
library(stringr)
Stress<-tibble::tribble(
  ~V1,  ~V2,  ~V3,  ~V4,  ~V5,  ~V6,  ~V7,  ~V8,  ~V9, ~V10, ~V11, ~V12, ~V13, ~V14, ~V15, ~V16, ~V17,
  "ID Number",   0L,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L,  10L,  11L,  12L,  13L,  14L,  15L,
  "CD-Female",  66L,  66L,  64L,  64L,  58L,  58L,  58L,  57L,  56L,  56L,  57L,  57L,  57L,  57L,  57L,  57L,
  "CD-Female", 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 103L, 104L,
  "CD-Female", 115L, 115L, 114L, 114L, 114L, 114L, 112L, 111L, 110L, 109L, 109L, 108L, 109L, 110L, 111L, 112L,
  "CD-Female",  84L,  63L,  62L,  62L,  63L,  58L,  58L,  54L,  62L,  66L,  66L,  60L,  60L,  51L,  54L,  54L,
  "CD-Female",  83L,  84L,  84L,  84L,  84L,  84L,  82L,  82L,  82L,  82L,  81L,  82L,  83L,  82L,  82L,  82L,
  "CD-Female", 240L, 202L, 141L, 106L,  89L,  74L,  71L,  71L,  71L,  72L,  74L,  77L,  77L,  70L,  68L,  68L,
  "CD-Female",  66L,  66L,  76L,  76L,  76L,  77L,  75L,  75L,  75L,  75L,  76L,  76L,  77L,  79L,  79L,  77L,
  "CD-Female",  66L,  68L,  68L,  67L,  66L,  67L,  68L,  69L,  69L,  68L,  68L,  68L,  70L,  72L,  71L,  69L,
  "CD-Female",  65L,  65L,  65L,  65L,  65L,  65L,  66L,  66L,  65L,  65L,  66L,  66L,  67L,  67L,  68L,  69L,
  "CD-Male",  70L,  69L,  68L,  68L,  69L,  69L,  69L,  69L,  68L,  69L,  70L,  71L,  72L,  72L,  73L,  74L,
  "CD-Male",  66L,  66L,  66L,  66L,  66L,  66L,  67L,  67L,  67L,  67L,  68L,  68L,  68L,  67L,  67L,  67L,
  "SS-Female",  67L,  65L,  65L,  64L,  58L,  61L,  55L,  60L,  60L,  60L,  60L,  66L,  66L,  72L,  72L,  82L,
  "SS-Female",  78L,  78L,  77L,  76L,  75L,  75L,  75L,  75L,  76L,  76L,  76L,  76L,  77L,  76L,  76L,  77L,
  "SS-Female",  79L,  78L,  67L,  67L,  68L,  62L,  62L,  62L,  63L,  72L,  72L,  82L,  81L,  82L,  83L,  82L,
  "SS-Female",  85L,  84L,  83L,  81L,  80L,  79L,  78L,  78L,  79L,  80L,  80L,  81L,  81L,  81L,  80L,  80L,
  "SS-Male",  63L,  63L,  63L,  63L,  77L,  78L,  80L,  77L,  76L,  67L,  67L,  62L,  57L,  57L,  57L,  48L,
  "SS-Male",  49L,  48L,  46L,  46L,  45L,  46L,  45L,  45L,  45L,  45L,  46L,  46L,  47L,  48L,  48L,  50L
)

TheData <- Stress %>%
  group_by(V1) %>%
  summarize_if(is.numeric, mean) %>%
  pivot_longer(-V1) %>%
  filter(V1 != "ID Number") %>%
  mutate(time = str_sub(name, 2) %>% as.numeric())
  
#cutting off data with xlim()
ggplot(TheData) +
  aes(time, value, color = V1) +
  geom_line() +
  xlim(5,15) 
#> Warning: Removed 20 rows containing missing values (geom_path).


#Zooming in to x = [5, 15] with coord_cartesian
ggplot(TheData) +
  aes(time, value, color = V1) +
  geom_line() +
  coord_cartesian(xlim = c(5,15))


#Change the legend label and remove the grid
ggplot(TheData) +
  aes(time, value, color = V1) +
  geom_line() +
  xlim(5,15) +
  labs(color = "Population") +
  theme(panel.grid = element_blank())
#> Warning: Removed 20 rows containing missing values (geom_path).

Created on 2022-04-22 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.