将密度图添加到重叠直方图

Add density plots to overlapping histograms

名为bodyfat.csv的数据集,我附上明文的数据集供大家参考。

bodydata <- read.csv("bodyfat.csv",header=T)
bodycat <- c()
bodycat[bodydata$bodyfat<14]<-"Athlete"
bodycat[bodydata$bodyfat>=14 & bodydata$bodyfat<25]<-"Average"
bodycat[bodydata$bodyfat>=25]<-"Obese"
bodydata <-cbind(bodydata,bodycat)

hist(bodydata$neck[bodydata$bodycat == "Athlete"],
     col=rgb(1,0,0,0.25), 
     xlim = c(30,55),
     ylim = c(0,25),
     main = "Neck for the three body categories", 
     xlab = "Body categories",
     ylab = "Neck",
     breaks=15)

hist(bodydata$neck[bodydata$bodycat == "Average"], 
     col=rgb(0,0,1,0.25),
     breaks=15,
     add=T)

hist(bodydata$neck[bodydata$bodycat == "Obese"], 
     col=rgb(0,1,0,0.25),
     breaks=15,
     add=T)

legend("topright", c("Athlete", "Average", "Obese"), 
       col=c(rgb(1,0,0,0.25),rgb(0,0,1,0.25),rgb(0,1,0,0.25)), lwd=15)

dens1 <- density(bodydata$bodycat == "Average", bw = 4)
lines(dens1, lwd = 2)

我收到一个错误:

Error in density.default(bodydata$bodycat == "Average", bw = 4) : 
  Parameter 'x' must be numeric

有没有办法使用 R 来实现向重叠直方图添加密度图?

bodyfat.csv 纯文本

id  bodyfat density age weight  height  adiposity   neck    chest   abdomen hip thigh   knee    ankle   bicep   forearm wrist
1   12.6    1.0708  23  154.25  67.75   23.7    36.2    93.1    85.2    94.5    59  37.3    21.9    32  27.4    17.1
2   6.9 1.0853  22  173.25  72.25   23.4    38.5    93.6    83  98.7    58.7    37.3    23.4    30.5    28.9    18.2
3   24.6    1.0414  22  154 66.25   24.7    34  95.8    87.9    99.2    59.6    38.9    24  28.8    25.2    16.6
4   10.9    1.0751  26  184.75  72.25   24.9    37.4    101.8   86.4    101.2   60.1    37.3    22.8    32.4    29.4    18.2
5   27.8    1.034   24  184.25  71.25   25.6    34.4    97.3    100 101.9   63.2    42.2    24  32.2    27.7    17.7
6   20.6    1.0502  24  210.25  74.75   26.5    39  104.5   94.4    107.8   66  42  25.6    35.7    30.6    18.8
7   19  1.0549  26  181 69.75   26.2    36.4    105.1   90.7    100.3   58.4    38.3    22.9    31.9    27.8    17.7
8   12.8    1.0704  25  176 72.5    23.6    37.8    99.6    88.5    97.1    60  39.4    23.2    30.5    29  18.8
9   5.1 1.09    25  191 74  24.6    38.1    100.9   82.5    99.9    62.9    38.3    23.8    35.9    31.1    18.2
10  12  1.0722  23  198.25  73.5    25.8    42.1    99.6    88.6    104.1   63.1    41.7    25  35.6    30  19.2
11  7.5 1.083   26  186.25  74.5    23.6    38.5    101.5   83.6    98.2    59.7    39.7    25.2    32.8    29.4    18.5
12  8.5 1.0812  27  216 76  26.3    39.4    103.6   90.9    107.7   66.2    39.2    25.9    37.2    30.2    19
13  20.5    1.0513  32  180.5   69.5    26.3    38.4    102 91.6    103.9   63.4    38.3    21.5    32.5    28.6    17.7
14  20.8    1.0505  30  205.25  71.25   28.5    39.4    104.1   101.8   108.6   66  41.5    23.7    36.9    31.6    18.8
15  21.7    1.0484  35  187.75  69.5    27.4    40.5    101.3   96.4    100.1   69  39  23.1    36.1    30.5    18.2
16  20.5    1.0512  35  162.75  66  26.3    36.4    99.1    92.8    99.2    63.1    38.7    21.7    31.1    26.4    16.9
17  28.1    1.0333  34  195.75  71  27.3    38.9    101.9   96.4    105.2   64.8    40.8    23.1    36.2    30.8    17.3
18  22.4    1.0468  32  209.25  71  29.2    42.1    107.6   97.5    107 66.9    40  24.4    38.2    31.6    19.3
19  16.1    1.0622  28  183.75  67.75   28.2    38  106.8   89.6    102.4   64.2    38.7    22.9    37.2    30.5    18.5
20  16.5    1.061   33  211.75  73.5    27.6    40  106.2   100.5   109 65.8    40.6    24  37.1    30.1    18.2
21  19  1.0551  28  179 68  27.3    39.1    103.3   95.9    104.9   63.5    38  22.1    32.5    30.3    18.4
22  15.3    1.064   28  200.5   69.75   29.1    41.3    111.4   98.8    104.8   63.4    40.6    24.6    33  32.8    19.9
23  15.7    1.0631  31  140.25  68.25   21.2    33.9    86  76.4    94.6    57.4    35.3    22.2    27.9    25.9    16.7
24  17.6    1.0584  32  148.75  70  21.4    35.5    86.7    80  93.4    54.9    36.2    22.1    29.8    26.7    17.1
25  14.2    1.0668  28  151.25  67.75   23.2    34.5    90.2    76.3    95.8    58.4    35.5    22.9    31.1    28  17.6
26  4.6 1.0911  27  159.25  71.5    21.9    35.7    89.6    79.7    96.5    55  36.7    22.5    29.9    28.2    17.7
27  8.5 1.0811  34  131.5   67.5    20.3    36.2    88.6    74.6    85.3    51.7    34.7    21.4    28.7    27  16.5
28  22.4    1.0468  31  148 67.5    22.9    38.8    97.4    88.7    94.7    57.5    36  21  29.2    26.6    17
29  4.7 1.091   27  133.25  64.75   22.4    36.4    93.5    73.9    88.5    50.1    34.5    21.3    30.5    27.9    17.2
30  9.4 1.079   29  160.75  69  23.8    36.7    97.4    83.5    98.7    58.9    35.3    22.6    30.1    26.7    17.6
31  12.3    1.0716  32  182 73.75   23.6    38.7    100.5   88.7    99.8    57.5    38.7    33.9    32.5    27.7    18.4
32  6.5 1.0862  29  160.25  71.25   22.2    37.3    93.5    84.5    100.6   58.5    38.8    21.5    30.1    26.4    17.9
33  13.4    1.0719  27  168 71.25   23.3    38.1    93  79.1    94.5    57.3    36.2    24.5    29  30  18.8
34  20.9    1.0502  41  218.5   71  30.5    39.8    111.7   100.5   108.3   67.1    44.2    25.2    37.5    31.5    18.7
35  31.1    1.0263  41  247.25  73.5    32.2    42.1    117 115.6   116.1   71.2    43.3    26.3    37.3    31.7    19.7
36  38.2    1.0101  49  191.75  65  32  38.4    118.5   113.1   113.8   61.9    38.3    21.9    32  29.8    17
37  23.6    1.0438  40  202.25  70  29.1    38.5    106.5   100.9   106.2   63.5    39.9    22.6    35.1    30.6    19
38  27.5    1.0346  50  196.75  68.25   29.7    42.1    105.6   98.8    104.8   66  41.5    24.7    33.2    30.5    19.4
39  33.8    1.0202  46  363.15  72.25   48.9    51.2    136.2   148.1   147.7   87.3    49.1    29.6    45  29  21.4
40  31.3    1.0258  50  203 67  31.8    40.2    114.8   108.1   102.5   61.3    41.1    24.7    34.1    31  18.3
41  33.1    1.0217  45  262.75  68.75   39.1    43.2    128.3   126.2   125.6   72.5    39.6    26.6    36.4    32.7    21.4
42  31.7    1.025   44  205 29.5    29.9    36.6    106 104.3   115.5   70.6    42.5    23.7    33.6    28.7    17.4
43  30.4    1.0279  48  217 70  31.2    37.3    113.3   111.2   114.1   67.7    40.9    25  36.7    29.8    18.4
44  30.8    1.0269  41  212 71.5    29.2    41.5    106.6   104.3   106 65  40.2    23  35.8    31.5    18.8
45  8.4 1.0814  39  125.25  68  19.1    31.5    85.1    76  88.2    50  34.7    21  26.1    23.1    16.1
46  14.1    1.067   43  164.25  73.25   21.3    35.7    96.6    81.5    97.2    58.4    38.2    23.4    29.7    27.4    18.3
47  11.2    1.0742  40  133.5   67.5    20.6    33.6    88.2    73.7    88.5    53.3    34.5    22.5    27.9    26.2    17.3
48  6.4 1.0665  39  148.5   71.25   20.6    34.6    89.8    79.5    92.7    52.7    37.5    21.9    28.8    26.8    17.9
49  13.4    1.0678  45  135.75  68.5    20.4    32.8    92.3    83.4    90.4    52  35.8    20.6    28.8    25.5    16.3
50  5   1.0903  47  127.5   66.75   20.2    34  83.4    70.4    87.2    50.6    34.4    21.9    26.8    25.8    16.8
51  10.7    1.0756  47  158.25  72.25   21.3    34.9    90.2    86.7    98.3    52.6    37.2    22.4    26  25.8    17.3
52  7.4 1.084   40  139.25  69  20.6    34.3    89.2    77.9    91  51.4    34.9    21  26.7    26.1    17.2
53  8.7 1.0807  51  137.25  67.75   21.1    36.5    89.7    82  89.1    49.3    33.7    21.4    29.6    26  16.9
54  7.1 1.0848  49  152.75  73.5    19.9    35.1    93.3    79.6    91.6    52.6    37.6    22.6    38.5    27.4    18.5
55  4.9 1.0906  42  136.25  67.5    21.1    37.8    87.6    77.6    88.6    51.9    34.9    22.5    27.7    27.5    18.5
56  22.2    1.0473  54  198 72  26.9    39.9    107.6   100 99.6    57.2    38  22  35.9    30.2    18.9
57  20.1    1.0524  58  181.5   68  27.6    39.1    100 99.8    102.5   62.1    39.6    22.5    33.1    28.3    18.5
58  27.1    1.0356  62  201.25  69.5    29.3    40.5    111.5   104.2   105.8   61.8    39.8    22.7    37.7    30.9    19.2
59  30.4    1.028   54  202.5   70.75   28.4    40.5    115.4   105.3   97  59.1    38  22.5    31.6    28.8    18.2
60  24  1.043   61  179.75  65.75   29.2    38.4    104.8   98.3    99.6    60.6    37.7    22.9    34.5    29.6    18.5
61  25.4    1.0396  62  216 73.25   28.2    41.4    112.3   104.8   103.1   61.6    40.9    23.1    36.2    31.8    20.2
62  28.8    1.0317  56  178.75  68.5    26.8    35.6    102.9   94.7    100.8   60.9    38  22.1    32.5    29.8    18.3
63  29.6    1.0298  54  193.25  70.25   27.6    38  107.6   102.4   99.4    61  39.4    23.6    32.7    29.9    19.1
64  25.1    1.0403  61  178 67  27.9    37.4    105.3   99.7    99.7    60.8    40.1    22.7    33.6    29  18.8
65  31  1.0264  57  205.5   70  29.5    40.1    105.3   105.5   108.3   65  41.2    24.7    35.3    31.1    18.4
66  28.9    1.0313  55  183.5   67.5    28.3    40.9    103 100.3   104.2   64.8    40.2    22.7    34.8    30.1    18.7
67  21.1    1.0499  54  151.5   70.75   21.3    35.6    90  83.9    93.9    55  36.1    21.7    29.6    27.4    17.4
68  14  1.0673  55  154.75  71.5    21.3    36.9    95.4    86.6    91.8    54.3    35.4    21.5    32.8    27.4    18.7
69  7.1 1.0847  54  155.25  69.25   22.8    37.5    89.3    78.4    96.1    56  37.4    22.4    32.6    28.1    18.1
70  13.2    1.0693  55  156.75  71.5    21.6    36.3    94.4    84.6    94.3    51.2    37.4    21.6    27.3    27.1    17.3
71  23.7    1.0439  62  167.5   71.5    23.1    35.5    97.6    91.5    98.5    56.6    38.6    22.4    31.5    27.3    18.6
72  9.4 1.0788  55  146.75  68.75   21.9    38.7    88.5    82.8    95.5    58.9    37.6    21.6    30.3    27.3    18.3
73  9.1 1.0796  56  160.75  73.75   20.8    36.4    93.6    82.9    96.3    52.9    37.5    23.1    29.7    27.3    18.2
74  13.7    1.068   55  125 64  21.5    33.2    87.7    76  88.6    50.9    35.4    19.1    29.3    25.7    16.9
75  12  1.072   61  143 65.75   23.3    36.5    93.4    83.3    93  55.5    35.2    20.9    29.4    27  16.8
76  18.3    1.0666  61  148.25  67.5    22.9    36  91.6    81.8    94.8    54.5    37  21.4    29.3    27  18.3
77  9.2 1.079   57  162.5   69.5    23.7    38.7    91.6    78.8    94.3    56.7    39.7    24.2    30.2    29.2    18.1
78  21.7    1.0483  69  177.75  68.5    26.7    38.7    102 95  98.3    55  38.3    21.8    30.8    25.7    18.8
79  21.1    1.0498  81  161.25  70.25   23  37.8    96.4    95.4    99.3    53.5    37.5    21.5    31.4    26.8    18.3
80  18.6    1.056   66  171.25  69.25   25.1    37.4    102.7   98.6    100.2   56.5    39.3    22.7    30.3    28.7    19
81  30.2    1.0283  67  163.75  67.75   25.1    38.4    97.7    95.8    97.1    54.8    38.2    23.7    29.4    27.2    19
82  26  1.0382  64  150.25  67.25   23.4    38.1    97.1    89  96.9    54.8    38  22  29.9    25.2    17.7
83  18.2    1.0568  64  190.25  72.75   25.3    39.3    103.1   97.8    99.6    58.9    39  23  34.3    29.6    19
84  26.2    1.0377  70  170.75  70  24.5    38.7    101.8   94.9    95  56  36.5    24.1    31.2    27.3    19.2
85  26.1    1.0378  72  168 69.25   24.7    38.5    101.4   99.8    96.2    56.3    36.6    22  29.7    26.3    18
86  25.8    1.0386  67  167 67.5    26  36.5    98.9    89.7    96.2    54.7    37.8    33.7    32.4    27.7    18.2
87  15  1.0648  72  157.75  67.25   24.6    37.7    97.5    88.1    96.9    57.2    37.7    21.8    32.6    28  18.8
88  22.6    1.0462  64  160 65.75   26  36.5    104.3   90.9    93.8    57.8    39.5    23.3    29.2    28.4    18.1
89  8.8 1.08    46  176.75  72.5    23.7    38  97.3    86  99.3    61  38.4    23.8    30.2    29.3    18.8
90  14.3    1.0666  48  176 73  23.3    36.7    96.7    86.5    98.3    60.4    39.9    24.4    28.8    29.6    18.7
91  20.2    1.052   46  177 70  25.4    37.2    99.7    95.6    102.2   58.3    38.2    22.5    29.1    27.7    17.7
92  18.1    1.0573  44  179.75  69.5    26.2    39.2    101.9   93.2    100.6   58.9    39.7    23.1    31.4    28.4    18.8
93  9.2 1.0795  47  165.25  70.5    23.4    37.5    97.2    83.1    95.4    56.9    38.3    22.1    30.1    28.2    18.4
94  24.2    1.0424  46  192.5   71.75   26.3    38  106.6   97.5    100.6   58.9    40.5    24.5    33.3    29.6    19.1
95  9.6 1.0785  47  184.25  74.5    23.4    37.3    99.6    88.8    101.4   57.4    39.6    24.6    30.3    27.9    17.8
96  17.3    1.0991  53  224.5   77.75   26.1    41.1    113.2   99.2    107.5   61.7    42.3    23.2    32.9    30.8    20.4
97  10.1    1.077   38  188.75  73.25   24.8    37.5    99.1    91.6    102.4   60.6    39.4    22.9    31.6    30.1    18.5
98  11.1    1.073   50  162.5   66.5    25.9    38.7    99.4    86.7    96.2    62.1    39.3    23.3    30.6    27.8    18.2
99  17.7    1.0582  46  156.5   68.25   23.7    35.9    95.1    88.2    92.8    54.7    37.3    21.9    31.6    27.5    18.2
100 21.7    1.0484  47  197 72  26.7    40  107.5   94  103.7   62.7    39  22.3    35.3    30.9    18.3
101 20.8    1.0506  49  198.5   73.5    25.9    40.1    106.5   95  101.7   59  39.4    22.3    32.2    31  18.6
102 20.1    1.0524  48  173.75  72  23.6    37  99.1    92  98.3    59.3    38.4    22.4    27.9    26.2    17
103 19.8    1.053   41  172.75  71.25   24  36.3    96.7    89.2    98.3    60  38.4    23.2    31  29.2    18.4
104 21.9    1.048   49  196.75  73.75   25.5    40.7    103.5   95.5    101.6   59.1    39.8    25.4    31  30.3    19.7
105 24.7    1.0412  43  177 69.25   26  39.6    104 98.6    99.5    59.5    36.1    22  30.1    27.2    17.7
106 17.8    1.0578  43  165.5   68.5    24.8    31.1    93.1    87.3    96.6    54.7    39  24.8    31  29.4    18.8
107 19.1    1.0547  43  200.25  73.5    26  38.6    105.2   102.8   103.6   61.2    39.3    23.5    30.5    28.5    18.1
108 18.2    1.0569  52  203.25  74.25   26  42  110 101.6   100.7   55.8    38.7    23.4    35.1    29.6    19.1
109 17.2    1.0593  43  194 75.5    24  38.5    110.1   88.7    102.1   57.5    40  24.8    35.1    30.7    19.2
110 21  1.05    40  168.5   69.25   24.7    34.2    97.8    92.3    100.6   57.5    36.8    22.8    32.1    26  17.3
111 19.5    1.0538  43  170.75  68.5    25.6    37.2    96.3    90.6    99.3    61.9    38  22.3    33.3    28.2    18.1
112 27.1    1.0355  43  183.25  70  26.3    37.1    108 105 103 63.7    40  23.6    33.5    27.8    17.4
113 21.6    1.0486  47  178.25  70  25.6    40.2    99.7    95  98.6    62.3    38.1    23.9    35.3    31.1    19.8
114 20.9    1.0503  42  163 70.25   23.3    35.3    93.5    89.6    99.8    61.5    37.8    21.9    30.7    27.6    17.4
115 25.9    1.0384  48  175.25  71.75   24  38  100.7   92.4    97.5    59.3    38.1    21.8    31.8    27.3    17.5
116 16.7    1.0607  40  158 69.25   23.4    36.3    97  86.6    92.6    55.9    36.3    22.1    29.8    26.3    17.3
117 19.8    1.0529  48  177.25  72.75   23.6    36.8    96  90  99.7    58.8    38.4    22.8    29.9    28  18.1
118 14.1    1.0671  51  179 72  24.3    41  99.2    90  96.4    56.8    38.8    23.3    33.4    29.8    19.5
119 25.1    1.0404  40  191 74  24.6    38.3    95.4    92.4    104.3   64.6    41.1    24.8    33.6    29.5    18.5
120 17.9    1.0575  44  187.5   72.25   25.3    38  101.8   87.5    101 58.5    39.2    24.5    32.1    28.6    18
121 27  1.0358  52  206.5   74.5    26.2    40.8    104.3   99.2    104.1   58.5    39.3    24.6    33.9    31.2    19.5
122 24.6    1.0414  44  185.25  71.5    25.5    39.5    99.2    98.1    101.4   57.1    40.5    23.2    33  29.6    18.4
123 14.8    1.0652  40  160.25  68.75   23.9    36.9    99.3    83.3    97.5    60.5    38.7    22.6    34.4    28  17.6
124 16  1.0623  47  151.5   66.75   23.9    36.9    94  86.1    95.2    58.1    36.5    22.1    30.6    27.5    17.6
125 14  1.0674  50  161 66.5    25.6    37.7    98.9    84.1    94  58.5    36.6    23.5    34.4    29.2    18
126 17.4    1.0587  46  167 67  26.2    36.6    101 89.9    100 60.7    36  21.9    35.6    30.2    17.6
127 26.4    1.0373  42  177.5   68.75   26.4    38.9    98.7    92.1    98.5    60.7    36.8    22.2    33.8    30.3    17.2
128 17.4    1.059   43  152.25  67.75   23.4    37.5    95.9    78  93.2    53.5    35.8    20.8    33.9    28.2    17.4
129 20.4    1.0515  40  192.25  73.25   25.2    39.8    103.9   93.5    99.5    61.7    39  21.8    33.3    29.6    18.1
130 15  1.0648  42  165.25  69.75   23.9    38.3    96.2    87  97.8    57.4    36.9    22.2    31.6    27.8    17.7
131 18  1.0575  49  171.75  71.5    23.7    35.5    97.8    90.1    95.8    57  38.7    23.2    27.5    26.5    17.6
132 22.2    1.0472  40  171.25  70.5    24.3    36.3    94.6    90.3    99.1    60.3    38.5    23  31.2    28.4    17.1
133 23.1    1.0452  47  197 73.25   25.8    37.8    103.6   99.8    103.2   61.2    38.1    22.6    33.5    28.6    17.9
134 25.3    1.0398  50  157 66.75   24.8    37.8    100.4   89.4    92.3    56.1    35.6    20.5    33.6    29.3    17.3
135 23.8    1.0435  41  168.25  69.5    24.5    36.5    98.4    87.2    98.4    56  36.9    23  34  29.8    18.1
136 26.3    1.0374  44  186 69.75   26.8    37.8    104.6   101.1   102.1   58.9    37.9    22.7    30.9    28.8    17.6
137 21.4    1.0491  39  166.75  70.75   23.5    37  92.9    86.1    95.6    58.8    36.1    22.4    32.7    28.3    17.1
138 28.4    1.0325  43  187.75  74  24.1    37.7    97.8    98.6    100.6   63.6    39.2    23.8    34.3    28.4    17.7
139 21.8    1.0481  40  168.25  71.25   23.3    34.3    98.3    88.5    98.3    58.1    38.4    22.5    31.7    27.4    17.6
140 20.1    1.0522  49  212.75  75  26.6    40.8    104.7   106.6   107.7   66.5    42.5    24.5    35.5    29.8    18.7
141 24.3    1.0422  40  176.75  71  24.6    37.4    98.6    93.1    101.6   59.1    39.6    21.6    30.8    27.9    16.6
142 18.1    1.0571  40  173.25  69.5    25.3    36.5    99.5    93  99.3    60.4    38.2    22  32  28.5    17.8
143 22.7    1.0459  52  167 67.75   25.6    37.5    102.7   91  98.9    57.1    36.7    22.3    31.6    27.5    17.9
144 9.9 1.0775  23  159.75  72.25   21.6    35.5    92.1    77.1    93.9    56.1    36.1    22.7    30.5    27.2    18.2
145 10.8    1.0754  23  188.15  77.5    22.1    38  96.6    85.3    102.5   59.1    37.6    23.2    31.8    29.7    18.3
146 14.4    1.0664  24  156 70.75   21.9    35.7    92.7    81.9    95.3    56.4    36.5    22  33.5    28.3    17.3
147 19  1.055   24  208.5   72.75   27.7    39.2    102 99.1    110.1   71.2    43.5    25.2    36.1    30.3    18.7
148 28.6    1.0322  25  206.5   69.75   29.8    40.9    110.9   100.5   106.2   68.4    40.8    24.6    33.3    29.7    18.4
149 6.1 1.0873  25  143.75  72.5    19.3    35.2    92.3    76.5    92.1    51.9    35.7    22  25.8    25.2    16.9
150 24.5    1.0416  26  223 70.25   31.8    40.6    114.1   106.8   113.9   67.6    42.7    24.7    36  30.4    18.4
151 9.9 1.0776  26  152.25  69  22.5    35.4    92.9    77.6    93.5    56.9    35.9    20.4    31.6    29  17.8
152 19.1    1.0542  26  241.75  74.5    30.7    41.8    108.3   102.9   114.4   72.9    43.5    25.1    38.5    33.8    19.6
153 10.6    1.0758  27  146 72.25   19.7    34.1    88.5    72.8    91.1    53.6    36.8    23.8    27.8    26.3    17.4
154 16.5    1.061   27  156.75  67.25   24.4    37.9    94  88.2    95.2    56.8    37.4    22.8    30.6    28.3    17.9
155 20.5    1.051   27  200.25  73.5    26.1    38.2    101.1   100.1   105 62.1    40  24.9    33.7    29.2    19.4
156 17.2    1.0594  28  171.5   75.25   21.6    35.6    92.1    83.5    98.3    57.3    37.8    21.7    32.2    27.7    17.7
157 30.1    1.0287  28  205.75  69  30.4    38.5    105.6   105 106.4   68.6    40  25.2    35.2    30.7    19.1
158 10.5    1.0761  28  182.5   72.25   24.6    37  98.5    90.8    102.5   60.8    38.5    25  31.6    28  18.6
159 12.8    1.0704  30  136.5   68.75   20.3    35.9    88.7    76.6    89.8    50.1    34.8    21.8    27  34.9    16.9
160 22  1.0477  31  177.25  71.5    24.4    36.2    101.1   92.4    99.3    59.4    39  24.6    30.1    28.2    18.2
161 9.9 1.0775  31  151.25  72.25   20.4    35  94  81.2    91.5    52.5    36.6    21  27  26.3    16.5
162 14.8    1.0653  33  196 73  25.9    38.5    103.8   95.6    105.1   61.4    40.6    25  31.3    29.2    19.1
163 13.3    1.069   33  184.25  68.75   24.4    40.7    98.9    92.1    103.5   64  37.3    23.5    33.5    30.6    19.7
164 15.2    1.0644  34  140 70.5    19.8    36  89.2    83.4    89.6    52.4    35.6    20.4    28.3    26.2    16.5
165 26.5    1.037   34  218.75  72  29.7    39.5    111.4   106 108.8   63.8    42  23.4    34  31.2    18.5
166 19  1.0549  35  217 73.75   28.1    40.5    107.5   95.1    104.5   64.8    41.3    25.6    36.4    33.7    19.4
167 21.4    1.0492  35  166.25  68  25.3    38.5    99.1    90.4    95.6    55.5    34.2    21.9    30.2    28.7    17.7
168 20  1.0525  35  224.75  72.25   30.3    43.9    108.2   100.4   106.8   63.3    41.7    24.6    37.2    33.1    19.8
169 34.7    1.018   35  228.25  69.5    33.3    40.4    114.9   115.9   111.9   74.4    40.6    24  36.1    31.8    18.8
170 16.5    1.061   35  172.75  69.5    25.2    37.6    99.1    90.8    98.1    60.1    39.1    23.4    32.5    29.8    17.4
171 4.1 1.0926  35  152.25  67.75   23.4    37  92.2    81.9    92.8    54.7    36.2    22.1    30.4    27.4    17.7
172 1.9 1.0983  35  125.75  65.5    20.6    34  90.8    75  89.2    50  34.8    22  24.8    25.9    16.9
173 20.2    1.0521  35  177.25  71  24.8    38.4    100.5   90.3    98.7    57.8    37.3    22.4    31  28.7    17.7
174 16.8    1.0603  36  176.25  71.5    24.3    38.7    98.2    90.3    99.9    59.2    37.7    21.5    32.4    28.4    17.8
175 24.6    1.0414  36  226.75  71.75   31  41.5    115.3   108.8   114.4   69.2    42.4    24  35.4    21  20.1
176 10.4    1.0763  37  145.25  69.25   21.3    36  96.8    79.4    89.2    50.3    34.8    22.2    31  26.9    16.9
177 13.4    1.0689  37  151 67  23.7    35.3    92.6    83.2    96.4    60  38.1    22  31.5    26.6    16.7
178 28.8    1.0316  37  241.25  71.5    33.2    42.1    119.2   110.3   113.9   69.8    42.6    24.8    34.4    29.5    18.4
179 22  1.0477  38  187.25  69.25   27.5    38  102.7   92.7    101.9   64.7    39.5    24.7    34.8    30.3    18.1
180 16.8    1.0603  39  234.75  74.5    29.8    42.8    109.5   104.5   109.9   69.5    43.1    25.8    39.1    32.5    19.9
181 25.8    1.0387  39  219.25  74.25   28  40  108.5   104.6   109.8   68.1    42.8    24.1    35.6    29  19
182 0   1.1089  40  118.5   68  18.1    33.8    79.3    69.4    85  47.2    33.5    20.2    27.7    24.6    16.5
183 11.9    1.0725  40  145.75  67.25   22.7    35.5    95.5    83.6    91.6    54.1    36.2    21.8    31.4    28.3    17.2
184 12.4    1.0713  40  159.25  69.75   23  35.3    92.3    86.8    96.1    58  39.4    22.7    30  26.4    17.4
185 17.4    1.0587  40  170.5   74.25   21.8    37.7    98.9    90.4    95.5    55.4    38.9    22.4    30.5    28.9    17.7
186 9.2 1.0794  40  167.5   71.5    23.1    39.4    89.5    83.7    98.1    57.3    39.7    22.6    32.9    29.3    18.2
187 23  1.0453  41  232.75  74.25   29.7    41.9    117.5   109.3   108.8   67.7    41.3    24.7    37.2    31.8    20
188 20.1    1.0524  41  210.5   72  28.6    38.5    107.4   98.9    104.1   63.5    39.8    23.5    36.4    30.4    19.1
189 20.2    1.052   41  202.25  72.5    27  40.8    109.2   98  101.8   62.8    41.3    24.8    36.6    32.4    18.8
190 23.8    1.0434  41  185 68.25   28  38  103.4   101.2   103.1   61.5    40.4    22.9    33.4    29.2    18.5
191 11.8    1.0728  41  153 69.25   22.5    36.4    91.4    80.6    92.3    54.3    36.3    21.8    29.6    27.3    17.9
192 36.5    1.014   42  244.25  76  29.8    41.8    115.2   113.7   112.4   68.5    45  25.5    37.1    31.2    19.9
193 16  1.0624  42  193.5   70.5    27.4    40.7    104.9   94.1    102.7   60.6    38.6    24.7    34  30.1    18.7
194 24  1.0429  42  224.75  74.75   28.3    38.5    106.7   105.7   111.8   65.3    43.3    26  33.7    29.9    18.5
195 22.3    1.047   42  162.75  72.75   21.6    35.4    92.2    85.6    96.5    60.2    38.9    22.4    31.7    27.1    17.1
196 24.8    1.0411  42  180 68.25   27.2    38.5    101.6   96.6    100.6   61.1    38.4    24.1    32.9    29.8    18.8
197 21.5    1.0488  42  156.25  69  23.1    35.5    97.8    86  96.2    57.7    38.6    24  31.2    27.3    17.4
198 17.6    1.0583  42  168 71.5    23.1    36.5    92  89.7    101 62.3    38  22.3    30.8    27.8    16.9
199 7.3 1.0841  42  167.25  72.75   22.3    37.6    94  78  99  57.5    40  22.5    30.6    30  18.5
200 22.6    1.0462  43  170.75  67.5    26.4    37.4    103.7   89.7    94.2    58.5    39  24.1    33.8    28.8    18.8
201 12.5    1.0709  43  178.25  70.25   25.4    37.8    102.7   89.2    99.2    60.2    39.2    23.8    31.7    28.4    18.6
202 21.7    1.0484  43  150 69.25   22  35.2    91.1    85.7    96.9    55.5    35.7    22  29.4    26.6    17.4
203 27.7    1.034   43  200.5   71.5    27.6    37.9    107.2   103.1   105.5   68.8    38.3    23.7    32.1    28.9    18.7
204 6.8 1.0854  44  184 74  23.7    37.9    100.8   89.1    102.6   60.6    39  24  32.9    29.2    18.4
205 33.4    1.0209  44  223 69.75   32.3    40.9    121.6   113.9   107.1   63.5    40.3    21.8    34.8    30.7    17.4
206 16.6    1.061   44  208.75  73  27.6    41.9    105.6   96.3    102 63.3    39.8    24.1    37.3    23.1    19.4
207 31.7    1.025   44  166 65.5    27.2    39.1    100.6   93.9    100.1   58.9    37.6    21.4    33.1    29.5    17.3
208 31.5    1.0254  47  195 72.5    26.1    40.2    102.7   101.3   101.7   60.7    39.4    23.3    36.7    31.6    18.4
209 10.1    1.0771  47  160.5   70.25   22.9    36  99.8    83.9    91.8    53  36.2    22.5    31.4    27.5    17.7
210 11.3    1.0742  47  159.75  70.75   22.5    34.5    92.9    84.4    94  56  38.2    22.6    29  26.2    17.6
211 7.8 1.0829  49  140.5   68  21.4    35.8    91.2    79.4    89  51.1    35  21.7    30.9    28.8    17.4
212 26.4    1.0373  49  216.25  74.5    27.4    40.2    115.6   104 109 63.7    40.3    23.2    36.8    31  18.9
213 19.3    1.0543  49  168.25  71.75   23  38.3    98.3    89.7    99.1    56.3    38.8    23  29.5    27.9    18.6
214 18.5    1.0561  50  194.75  70.75   27.4    39  103.7   97.6    104.2   60  40.9    25.5    32.7    30  19
215 19.3    1.0543  50  172.75  73  22.8    37.4    98.7    87.6    96.1    57.1    38.1    21.8    28.6    26.7    18
216 45.1    0.995   51  219 64  37.6    41.2    119.8   122.1   112.8   62.5    36.9    23.6    34.7    29.1    18.4
217 13.8    1.0678  51  149.25  69.75   21.6    34.8    92.8    81.1    96.3    53.8    36.5    21.5    31.3    26.3    17.8
218 8.2 1.0819  51  154.5   70  22.2    36.9    93.3    81.5    94.4    54.7    39  22.6    27.5    25.9    18.6
219 23.9    1.0433  52  199.25  71.75   27.2    39.4    106.8   100 105 63.9    39.2    22.9    35.7    30.4    19.2
220 15.1    1.0646  53  154.5   69.25   22.7    37.6    93.9    88.7    94.5    53.7    36.2    22  28.5    25.7    17.1
221 12.7    1.0706  54  153.25  70.5    24.5    38.5    99  91.8    96.2    57.7    38.1    23.9    31.4    29.9    18.9
222 25.3    1.0399  54  230 72.25   31  42.5    119.9   110.4   105.5   64.2    42.7    27  38.4    32  19.6
223 11.9    1.0726  54  161.75  67.5    25  37.4    94.2    87.6    95.6    59.7    40.2    23.4    27.9    27  17.8
224 6.1 1.0874  55  142.25  67.25   22.2    35.2    92.7    82.8    91.9    54.4    35.2    22.5    29.4    26.8    17
225 11.3    1.074   55  179.75  68.75   26.8    41.1    106.9   95.3    98.2    57.4    37.1    21.8    34.1    31.1    19.2
226 12.8    1.0703  55  126.5   66.75   20  33.4    88.8    78.2    87.5    50.8    33  19.7    25.3    22  15.8
227 14.9    1.065   55  169.5   68.25   25.6    37.2    101.7   91.1    97.1    56.6    38.5    22.6    33.4    29.3    18.8
228 24.5    1.0418  55  198.5   74.25   25.3    38.3    105.3   96.7    106.6   64  42.6    23.4    33.2    30  18.4
229 15  1.0647  56  174.5   69.5    25.4    38.1    104 89.4    98.4    58.4    37.4    22.5    34.6    30.1    18.8
230 16.9    1.0601  56  167.75  68.5    25.2    37.4    98.6    93  97  55.4    38.8    23.2    32.4    29.7    19
231 11.1    1.0745  57  147.75  65.75   24.1    35.2    99.6    86.4    90.1    53  35  21.3    31.7    27.3    16.9
232 16.1    1.062   57  182.25  71.75   24.9    39.4    103.4   96.7    100.7   59.3    38.6    22.8    31.8    29.1    19
233 15.5    1.0636  58  175.5   71.5    24.2    38  100.2   88.1    97.8    57.1    38.9    23.6    30.9    29.6    18
234 25.9    1.0384  58  161.75  67.25   25.2    35.1    94.9    94.9    100.2   56.8    35.9    21  27.8    26.1    17.6
235 25.5    1.0403  60  157.75  67.5    24.1    40.4    97.2    93.3    94  54.3    35.7    21  31.3    28.7    18.3
236 18.4    1.0563  62  168.75  67.5    26.1    38.3    104.7   95.6    93.7    54.4    37.1    22.7    30.3    26.3    18.3
237 24  1.0424  62  191.5   72.25   25.8    40.6    104 98.2    101.1   59.3    40.3    23  32.6    28.5    19
238 26.4    1.0372  63  219.15  69.5    31.9    40.2    117.6   113.8   111.8   63.4    41.1    22.3    35.1    29.6    18.5
239 12.7    1.0705  64  155.25  69.5    22.6    37.9    95.8    82.8    94.5    61.2    39.1    22.3    29.8    28.9    18.3
240 28.8    1.0316  65  189.75  65.75   30.9    40.8    106.4   100.5   100.5   59.2    38.1    24  35.9    30.5    19.1
241 17  1.0599  65  127.5   65.75   20.8    34.7    93  79.7    87.6    50.7    33.4    20.1    28.5    24.8    16.5
242 33.6    1.0207  65  224.5   68.25   33.9    38.8    119.6   118 114.3   61.3    42.1    23.4    34.9    30.1    19.4
243 29.3    1.0304  66  234.25  72  31.8    41.4    119.7   109 109.1   63.7    42.4    24.6    35.6    30.7    19.5
244 31.4    1.0256  67  227.75  72.75   30.3    41.3    115.8   113.4   109.8   65.6    46  25.4    35.3    29.8    19.5
245 28.1    1.0334  67  199.5   68.5    29.9    40.7    118.3   106.1   101.6   58.2    38.8    24.1    32.1    29.3    18.5
246 15.3    1.0641  68  155.5   69.25   22.8    36.3    97.4    84.3    94.4    54.3    37.5    22.6    29.2    27.3    18.5
247 29.1    1.0308  69  215.5   70.5    30.5    40.8    113.7   107.6   110 63.3    44  22.6    37.5    32.6    18.8
248 11.5    1.0736  70  134.25  67  21.1    34.9    89.2    83.6    88.8    49.6    34.8    21.5    25.6    25.7    18.5
249 32.3    1.0236  72  201 69.75   29.1    40.9    108.5   105 104.5   59.6    40.8    23.2    35.2    28.6    20.1
250 28.3    1.0328  72  186.75  66  30.2    38.9    111.1   111.5   101.7   60.3    37.3    21.5    31.3    27.2    18
251 25.3    1.0399  72  190.75  70.5    27  38.9    108.3   101.3   97.8    56  41.6    22.7    30.5    29.4    19.8
252 30.7    1.0271  74  207.5   70  29.8    40.8    112.4   108.5   107.1   59.3    42.2    24.6    33.7    30  20.9

我认为要解决你的错误,你应该这样写:

dens1 <- density(bodydata$neck[bodydata$bodycat == "Average"], bw = 4)

因此,稍微更改一下代码,您可以编写:

h1 <- hist(bodydata$neck[bodydata$bodycat == "Athlete"], plot = FALSE, breaks = 15)
h2 <- hist(bodydata$neck[bodydata$bodycat == "Average"], plot = FALSE, breaks = 15)
h3 <- hist(bodydata$neck[bodydata$bodycat == "Obese"], plot = FALSE, breaks = 15)

plot(h1, add = FALSE, col=rgb(1,0,0,0.25), xlim = c(30,55), ylim = c(0,25),
     main = "Neck for the three body categories", 
     xlab = "Body categories",
     ylab = "Neck")
plot(h2, add = TRUE, col = rgb(0,0,1,0.25))
plot(h3, add = TRUE, col = rgb(0,1,0,0.25))
legend("topright", c("Athlete", "Average", "Obese"), 
       col=c(rgb(1,0,0,0.25),rgb(0,0,1,0.25),rgb(0,1,0,0.25)), lwd=15)

dens1 <- density(bodydata$neck[bodydata$bodycat == "Athlete"], bw = 4)
dens2 <- density(bodydata$neck[bodydata$bodycat == "Average"], bw = 4)
dens3 <- density(bodydata$neck[bodydata$bodycat == "Obese"], bw = 4)
lines(x = dens1$x, y = dens1$y*length(bodydata$neck[bodydata$bodycat == "Athlete"])*diff(h1$breaks)[1], lwd = 2, col = rgb(1,0,0))
lines(x = dens2$x, y = dens2$y*length(bodydata$neck[bodydata$bodycat == "Average"])*diff(h2$breaks)[1], lwd = 2, col = rgb(0,0,1))
lines(x = dens3$x, y = dens3$y*length(bodydata$neck[bodydata$bodycat == "Obese"])*diff(h3$breaks)[1], lwd = 2, col = rgb(0,1,0))

如果您不必使用 R base 图,您可以使用 ggplot2,以更少的行将所有直方图和密度图放在一起,并使用三个不同的面板将它们分开facet_wrap。这将提高图表和代码的可读性:

library(ggplot2)
ggplot(bodydata, aes(neck, fill = bodycat))+
  geom_histogram(aes(y = ..count..),color = "black", alpha = 0.25)+
  geom_density(aes(y = ..count.., color = bodycat), fill = NA, alpha = 0.25)+
  facet_wrap(.~bodycat)