R语言 如何在ggplot中以有限的间隔和40个单位增量自定义y轴间隔和标记?

fivyi3re  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(101)

我正在尝试绘制ggplot,但我不确定如何更改y轴间隔或在y轴上显示特定间隔。如何使ggplot的y轴上升40,最小值为40,最大值为200,但在y轴上只显示80,120和160。
预期结果:

当前图:

颜色不需要相同,但我需要相同的Y轴间隔,如何将其添加到现有的图中?
这是我到目前为止的代码:

dl %>% print(n=100)
# A tibble: 100 × 4
    question type    name     hr
    <chr>    <chr>   <chr> <dbl>
  1 Q1       control foxy    125
  2 Q1       test    lfb     141
  3 Q10      control foxy    102
  4 Q10      control lfb     110
  5 Q11      control foxy    127
  6 Q11      test    lfb     155
  7 Q12      control foxy     93
  8 Q12      control lfb     129
  9 Q13      test    foxy     96
 10 Q13      control lfb     138
 11 Q14      control foxy    127
 12 Q14      test    lfb     133
 13 Q15      test    foxy    123
 14 Q15      control lfb     128
 15 Q16      control foxy    125
 16 Q16      control lfb     148
 17 Q17      test    foxy    103
 18 Q17      control lfb     139
 19 Q18      test    foxy     96
 20 Q18      test    lfb     133
 21 Q19      test    foxy    131
 22 Q19      control lfb     123
 23 Q2       control foxy    111
 24 Q2       control lfb     142
 25 Q20      test    foxy    105
 26 Q20      control lfb     137
 27 Q21      control foxy    140
 28 Q21      test    lfb     137
 29 Q22      control foxy    132
 30 Q22      test    lfb     114
 31 Q23      control foxy    129
 32 Q23      test    lfb     133
 33 Q24      control foxy    132
 34 Q24      test    lfb     123
 35 Q25      test    foxy    106
 36 Q25      control lfb     134
 37 Q26      control foxy    116
 38 Q26      control lfb     134
 39 Q27      test    foxy    119
 40 Q27      control lfb     101
 41 Q28      control foxy    143
 42 Q28      test    lfb     120
 43 Q29      test    foxy     84
 44 Q29      test    lfb     123
 45 Q3       control foxy    107
 46 Q3       control lfb     159
 47 Q30      test    foxy     96
 48 Q30      control lfb     120
 49 Q31      control foxy    114
 50 Q31      test    lfb     143
 51 Q32      control foxy    125
 52 Q32      control lfb     133
 53 Q33      test    foxy    102
 54 Q33      test    lfb     122
 55 Q34      control foxy     98
 56 Q34      test    lfb     126
 57 Q35      control foxy    114
 58 Q35      control lfb     155
 59 Q36      test    foxy    123
 60 Q36      control lfb     130
 61 Q37      test    foxy     91
 62 Q37      test    lfb     119
 63 Q38      control foxy    114
 64 Q38      control lfb     126
 65 Q39      test    foxy    132
 66 Q39      control lfb     141
 67 Q4       control foxy    129
 68 Q4       control lfb     151
 69 Q40      test    foxy    125
 70 Q40      control lfb     162
 71 Q41      control foxy    117
 72 Q41      control lfb     123
 73 Q42      test    foxy    110
 74 Q42      control lfb     118
 75 Q43      test    foxy    124
 76 Q43      test    lfb     142
 77 Q44      control foxy     91
 78 Q44      test    lfb     149
 79 Q45      test    foxy    134
 80 Q45      test    lfb     137
 81 Q46      test    foxy    112
 82 Q46      test    lfb     103
 83 Q47      control foxy    104
 84 Q47      test    lfb     140
 85 Q48      test    foxy    138
 86 Q48      test    lfb     151
 87 Q49      control foxy    115
 88 Q49      test    lfb     155
 89 Q5       test    foxy    112
 90 Q5       test    lfb     138
 91 Q50      test    foxy     74
 92 Q50      control lfb     118
 93 Q6       test    foxy    106
 94 Q6       test    lfb     153
 95 Q7       test    foxy    128
 96 Q7       control lfb     118
 97 Q8       control foxy    112
 98 Q8       test    lfb     145
 99 Q9       test    foxy    137
100 Q9       test    lfb     171 ```

dput(dl)
structure(list(question = c("Q1", "Q1", "Q10", "Q10", "Q11", 
"Q11", "Q12", "Q12", "Q13", "Q13", "Q14", "Q14", "Q15", "Q15", 
"Q16", "Q16", "Q17", "Q17", "Q18", "Q18", "Q19", "Q19", "Q2", 
"Q2", "Q20", "Q20", "Q21", "Q21", "Q22", "Q22", "Q23", "Q23", 
"Q24", "Q24", "Q25", "Q25", "Q26", "Q26", "Q27", "Q27", "Q28", 
"Q28", "Q29", "Q29", "Q3", "Q3", "Q30", "Q30", "Q31", "Q31", 
"Q32", "Q32", "Q33", "Q33", "Q34", "Q34", "Q35", "Q35", "Q36", 
"Q36", "Q37", "Q37", "Q38", "Q38", "Q39", "Q39", "Q4", "Q4", 
"Q40", "Q40", "Q41", "Q41", "Q42", "Q42", "Q43", "Q43", "Q44", 
"Q44", "Q45", "Q45", "Q46", "Q46", "Q47", "Q47", "Q48", "Q48", 
"Q49", "Q49", "Q5", "Q5", "Q50", "Q50", "Q6", "Q6", "Q7", "Q7", 
"Q8", "Q8", "Q9", "Q9"), type = c("control", "test", "control", 
"control", "control", "test", "control", "control", "test", "control", 
"control", "test", "test", "control", "control", "control", "test", 
"control", "test", "test", "test", "control", "control", "control", 
"test", "control", "control", "test", "control", "test", "control", 
"test", "control", "test", "test", "control", "control", "control", 
"test", "control", "control", "test", "test", "test", "control", 
"control", "test", "control", "control", "test", "control", "control", 
"test", "test", "control", "test", "control", "control", "test", 
"control", "test", "test", "control", "control", "test", "control", 
"control", "control", "test", "control", "control", "control", 
"test", "control", "test", "test", "control", "test", "test", 
"test", "test", "test", "control", "test", "test", "test", "control", 
"test", "test", "test", "test", "control", "test", "test", "test", 
"control", "control", "test", "test", "test"), name = c("foxy", 
"lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", 
"foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", 
"lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", 
"foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", 
"lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", 
"foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", 
"lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", 
"foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", 
"lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", 
"foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", 
"lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb", "foxy", "lfb"
), hr = c(125, 141, 102, 110, 127, 155, 93, 129, 96, 138, 127, 
133, 123, 128, 125, 148, 103, 139, 96, 133, 131, 123, 111, 142, 
105, 137, 140, 137, 132, 114, 129, 133, 132, 123, 106, 134, 116, 
134, 119, 101, 143, 120, 84, 123, 107, 159, 96, 120, 114, 143, 
125, 133, 102, 122, 98, 126, 114, 155, 123, 130, 91, 119, 114, 
126, 132, 141, 129, 151, 125, 162, 117, 123, 110, 118, 124, 142, 
91, 149, 134, 137, 112, 103, 104, 140, 138, 151, 115, 155, 112, 
138, 74, 118, 106, 153, 128, 118, 112, 145, 137, 171)), row.names = c(NA, 
-100L), spec = structure(list(cols = list(question = structure(list(), class = c("collector_character", 
"collector")), type = structure(list(), class = c("collector_character", 
"collector")), name = structure(list(), class = c("collector_character", 
"collector")), hr = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x12d977860>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))
ijxebb2r

ijxebb2r1#

这让你非常接近:

ggplot(dl, aes(name, hr, fill = type)) +
  geom_violin(color = NA, alpha = 0.5, trim = FALSE) +
  geom_boxplot(width = 0.4, position = position_dodge(width = 0.9)) +
  scale_fill_manual(values = c('#7fcab0', '#fb9e7a')) +
  scale_y_continuous(limits = c(50, 190), breaks = c(80, 120, 160)) +
  labs(x = 'Name of person', y = 'Heart rate',
       title = 'Performance on lie detector test') +
  theme_minimal(base_size = 16) +
  theme(legend.position = c(0.1, 0.85),
        legend.background = element_rect(fill = 'white', color = NA),
        panel.border = element_rect(fill = NA, color = 'gray'))

相关问题