Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

r - Change continuous to discrete axis and set axis limits

I have some problems with the following code:

Vo2Max.Auswertung %>% 
group_by(group) %>% 
summarise(across(everything(), mean)) %>% 
pivot_longer(cols = -group, names_to = "test", values_to = "mean") %>% 
mutate(test = as.numeric(gsub("[^0-9]","",test))) %>% 
ggplot(aes(x=test, y = mean, group = group, color = group)) + 
geom_line(aes(),size = 1.1) +
geom_point(aes(), size = 3)+
scale_colour_manual(values = c("lightskyblue", "royalblue"), name = "Legende") +                                                                                
ggtitle("Mittelwerte VO2 Max Vergleich nach Gruppen") +                                                                                                        
theme_bw() +                                                                                                                                                    
theme(axis.text.x = element_text(size = 12)) +                                                                                                                  
theme(axis.title.y = element_text(size = 15, angle = 90)) +                                                                                                     
theme(axis.text.y = element_text(size = 12, hjust=1)) +                                                                                                         
theme(axis.title.x = element_text(size = 15)) +                                                                                                                 
scale_x_continuous(name = "Testzeitpunkt") +
scale_y_continuous(name = "Mittelwerte VO2 max") +
theme(plot.title = element_text(hjust = 0.5))

This is the result

This is the result

I have to scale the x axes into 3 timepoints (t0, t1, t2). The y axes has to have an other scale (from 40 to 45).

Can someone help me to find the mistaces?

Here are my datas:

> Vo2Max.Auswertung
# A tibble: 18 x 4
 group              `t0 VO2 max` `t1 VO2 max` `t2 VO2 max`
 <chr>                     <dbl>        <dbl>        <dbl>
 1 Experimentalgruppe         47.6         47.9         48.7
 2 Kontrollgruppe             47.6         46.5         43.0
 3 Experimentalgruppe         47.6         48.7         48.7
 4 Kontrollgruppe             46.8         47.6         46.2
 5 Kontrollgruppe             44.6         46.2         47.9
 6 Experimentalgruppe         41.3         42.1         42.4
 7 Kontrollgruppe             38           40.7         38.6
 8 Experimentalgruppe         43.5         44.6         42.7
 9 Experimentalgruppe         41.9         43.2         43.8
10 Kontrollgruppe             45.1         47.9         49.2
11 Experimentalgruppe         44.1         44.3         44.9
12 Kontrollgruppe             28.5         30.9         30.3
13 Kontrollgruppe             38.6         41.6         42.1
14 Kontrollgruppe             44.6         45.4         47.6
15 Kontrollgruppe             40.4         43.0         42.4
16 Experimentalgruppe         32.6         33.3         33.3
17 Experimentalgruppe         40.4         38.6         43.0
18 Kontrollgruppe             44.3         40.1         42.7

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sorry I didn't get this with the x axis. Below a suggestion including some tips to improve your plotting.

Check mainly how I label title, x axis and y axis, and how I summarise the theme specs to one single call to theme

library(tidyverse)

foo %>%
  group_by(group) %>%
  summarise(across(everything(), mean)) %>%
  pivot_longer(cols = -group, names_to = "test", values_to = "mean") %>%

# the following is quite a change. I am using a factor instead of numeric
# Also I am using str_sub to extract the number
  mutate(test = as.factor(str_sub(test, 2, 2))) %>%
  ggplot(aes(x = test, y = mean, group = group, color = group)) +
  geom_line(aes(), size = 1.1) +
  geom_point(aes(), size = 3) +
  scale_colour_manual(values = c("lightskyblue", "royalblue"), name = "Legende") +
  labs(
    x = "Testzeitpunkt", y = "Mittelwerte VO2 max",
    title = "Mittelwerte VO2 Max Vergleich nach Gruppen"
  ) +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 12),
    axis.title.y = element_text(size = 15, angle = 90),
    axis.text.y = element_text(size = 12, hjust = 1),
    axis.title.x = element_text(size = 15),
    plot.title = element_text(hjust = 0.5)
  )
#> `summarise()` ungrouping output (override with `.groups` argument)

as per (now deleted) comments, you can simply extract the additional "t" by changing test =str_sub(test, 1, 2). because you're adding the character t, R will recognise it as a character vector and you don't need to factorise anymore

Created on 2021-01-06 by the reprex package (v0.3.0)

data

# devtools::install_github("alistaire47/read.so")

foo <- read.so::read_so("group t0_VO2_max t1_VO2_max t2_VO2_max
<chr>                     <dbl>        <dbl>        <dbl>
  1 Experimentalgruppe         47.6         47.9         48.7
2 Kontrollgruppe             47.6         46.5         43.0
3 Experimentalgruppe         47.6         48.7         48.7
4 Kontrollgruppe             46.8         47.6         46.2
5 Kontrollgruppe             44.6         46.2         47.9
6 Experimentalgruppe         41.3         42.1         42.4
7 Kontrollgruppe             38           40.7         38.6
8 Experimentalgruppe         43.5         44.6         42.7
9 Experimentalgruppe         41.9         43.2         43.8
10 Kontrollgruppe             45.1         47.9         49.2
11 Experimentalgruppe         44.1         44.3         44.9
12 Kontrollgruppe             28.5         30.9         30.3
13 Kontrollgruppe             38.6         41.6         42.1
14 Kontrollgruppe             44.6         45.4         47.6
15 Kontrollgruppe             40.4         43.0         42.4
16 Experimentalgruppe         32.6         33.3         33.3
17 Experimentalgruppe         40.4         38.6         43.0
18 Kontrollgruppe             44.3         40.1         42.7")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...