Gunnerfan
Gunnerfan

Reputation: 123

What is wrong with my density plot function in R

I have a script I compiled (shown below) to create a plot of all the 17 Height Columns I wanted, but I am getting an error when I run it. I created the columns in excel, and then copied and pasted into the script. Is there an error with the way I created the columns?

df <- read.table(text = "Height Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height
    147 800 1500    3077    4299    5704    7361    9388    10610   12051   13855   16354   18549   20643   23860   26471   31004
    141 797 1503    3096    4325    5733    7388    9404    10616   12051   13853   16352   18552   20649   23891   26504   31055
    148 806 1515    3112    4342    5753    7409    9425    10638   12072   13874   16374   18572   20670   23896   26505   31050
    145 808 1525    3136    4369    5779    7435    9449    10661   12096   13903   16418   18624   20710   23916   26511   31048
    146 807 1523    3143    4390    5815    7489    9527    10751   12191   13984   16451   18613   20677   23871   26462   30989
    151 807 1512    3101    4327    5730    7380    9392    10604   12037   13843   16361   18572   20672   23897   26499   31031
    150 808 1517    3119    4355    5772    7439    9469    10689   12123   13914   16396   18581   20670   23901   26528   31097
    136 798 1515    3127    4365    5783    7448    9474    10693   12136   13949   16449   18638   20723   23943   26548   31086
    128 789 1502    3108    4345    5759    7423    9450    10672   12116   13925   16419   18605   20684   23886   26477   30989
    144 805 1517    3122    4355    5763    7418    9432    10645   12081   13886   16385   18565   20640   23838   26428   30926
    129 786 1492    3085    4315    5725    7384    9406    10624   12064   13871   16379   18581   20676   23894   26486   30983
    142 802 1513    3124    4369    5793    7467    9502    10720   12150   13940   16422   18598   20678   23895   26501   31041
    141 797 1504    3098    4327    5736    7394    9417    10637   12081   13891   16396   18590   20674   23883   26479   31005
    139 797 1503    3097    4329    5742    7405    9432    10653   12095   13900   16391   18571   20654   23873   26482   30989
    144 800 1504    3093    4320    5727    7385    9406    10623   12064   13882   16404   18609   20703   23918   26515   31012
    152 813 1527    3143    4388    5810    7482    9516    10737   12173   13965   16423   18583   20649   23847   26450   30973
    126 783 1489    3081    4306    5708    7356    9361    10573   12013   13830   16340   18542   20633   23850   26457   30975
    142 803 1517    3131    4374    5795    7468    9504    10726   12163   13960   16437   18613   20686   23886   26482   30989
    135 793 1502    3103    4333    5737    7387    9395    10604   12034   13837   16343   18538   20619   23819   26429   30977
    139 801 1516    3133    4378    5804    7480    9520    10748   12194   13997   16471   18629   20689   23864   26431   30888
    115 772 1479    3068    4293    5694    7341    9348    10562   12006   13831   16356   18563   20654   23861   26460   30964
    140 797 1504    3101    4334    5748    7413    9440    10659   12100   13917   16422   18614   20701   23916   26491   30963
    137 795 1504    3104    4336    5746    7401    9414    10625   12058   13863   16377   18577   20661   23860   26453   30967
    146 802 1505    3095    4323    5732    7391    9414    10635   12089   13920   16423   18603   20681   23881   26450   30915
    160 819 1529    3133    4368    5784    7448    9474    10693   12131   13933   16424   18607   20684   23878   26465   30964
    134 791 1497    3089    4318    5729    7387    9410    10629   12070   13881   16389   18582   20665   23872   26466   30972
    142 801 1511    3109    4338    5746    7406    9431    10652   12098   13910   16414   18609   20692   23877   26449   30943
    145 804 1514    3117    4353    5767    7430    9455    10675   12113   13913   16408   18596   20680   23876   26452   30938
    140 800 1511    3114    4348    5760    7421    9447    10667   12106   13911   16399   18571   20634   23828   26427   30943
    147 803 1510    3108    4340    5753    7412    9426    10642   12082   13895   16399   18582   20659   23861   26450   30942", header = TRUE)

    densities <- lapply(df, density)
    plot(densities[[1]], main = "height")
    plot(densities[[2]], col="red")
    plot(densities[[3]], col="blue")
    plot(densities[[4]], col = "green")
    plot(densities[[5]], col="orange")
    plot(densities[[6]], col="yellow")
    plot(densities[[7]], col = "cyan")
    plot(densities[[8]], col="khaki")
    plot(densities[[9]], col="purple")
    plot(densities[[10]], col = "brown")
    plot(densities[[11]], col="olive")
    plot(densities[[12]], col="royalblue")
    plot(densities[[13]], col = "pink")
    plot(densities[[14]], col="seagreen")
    plot(densities[[15]], col="plum")
    plot(densities[[16]], col = "tan")
    plot(densities[[17]], col="black")

shown below is the error error from script

Upvotes: 1

Views: 372

Answers (2)

eipi10
eipi10

Reputation: 93761

To answer Gunnerfan's question in chat, here's how to melt the data and plot it in ggplot2.

First, "melting" just means converting your data from "wide" format (in this case one height per column) to long format (in this case, one column with all the heights and a separate column identifying which "person" the heights refer to). The opposite of melting is "casting" from long to wide format. ggplot2 generally prefers data in long (melted) format. If you haven't used R before, this will all probably seem very confusing at first. In any case, here's some code to melt and plot the data.

# Note: This code assumes you've loaded a wide-format data frame already 
# (using read.table, read.csv, etc.) and called it "df". 

# Load packages we'll need
library(reshape2)
library(ggplot2)

# melt data from wide to long format
df.m = melt(df)

# Plot, separate panel for each density
ggplot(df.m, aes(value)) +
  geom_density() +
  facet_wrap( ~ variable, scale="free_x")

# Plot, one panel with different colors for each density
ggplot(df.m, aes(value, colour=variable)) +
  geom_density() 

Upvotes: 1

Hack-R
Hack-R

Reputation: 23216

I have no problem running the code that you supplied, except that one of your color names doesn't work (Olive):

> df <- read.table(text = "Height Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height  Height
+     147 800 1500    3077    4299    5704    7361    9388    10610   12051   13855   16354   18549   20643   23860   26471   31004
+     141 797 1503    3096    4325    5733    7388    9404    10616   12051   13853   16352   18552   20649   23891   26504   31055
+                  148 806 1515    3112    4342    5753    7409    9425    10638   12072   13874   16374   18572   20670   23896   26505   31050
+                  145 808 1525    3136    4369    5779    7435    9449    10661   12096   13903   16418   18624   20710   23916   26511   31048
+                  146 807 1523    3143    4390    5815    7489    9527    10751   12191   13984   16451   18613   20677   23871   26462   30989
+                  151 807 1512    3101    4327    5730    7380    9392    10604   12037   13843   16361   18572   20672   23897   26499   31031
+                  150 808 1517    3119    4355    5772    7439    9469    10689   12123   13914   16396   18581   20670   23901   26528   31097
+                  136 798 1515    3127    4365    5783    7448    9474    10693   12136   13949   16449   18638   20723   23943   26548   31086
+                  128 789 1502    3108    4345    5759    7423    9450    10672   12116   13925   16419   18605   20684   23886   26477   30989
+                  144 805 1517    3122    4355    5763    7418    9432    10645   12081   13886   16385   18565   20640   23838   26428   30926
+                  129 786 1492    3085    4315    5725    7384    9406    10624   12064   13871   16379   18581   20676   23894   26486   30983
+                  142 802 1513    3124    4369    5793    7467    9502    10720   12150   13940   16422   18598   20678   23895   26501   31041
+                  141 797 1504    3098    4327    5736    7394    9417    10637   12081   13891   16396   18590   20674   23883   26479   31005
+                  139 797 1503    3097    4329    5742    7405    9432    10653   12095   13900   16391   18571   20654   23873   26482   30989
+                  144 800 1504    3093    4320    5727    7385    9406    10623   12064   13882   16404   18609   20703   23918   26515   31012
+                  152 813 1527    3143    4388    5810    7482    9516    10737   12173   13965   16423   18583   20649   23847   26450   30973
+                  126 783 1489    3081    4306    5708    7356    9361    10573   12013   13830   16340   18542   20633   23850   26457   30975
+                  142 803 1517    3131    4374    5795    7468    9504    10726   12163   13960   16437   18613   20686   23886   26482   30989
+                  135 793 1502    3103    4333    5737    7387    9395    10604   12034   13837   16343   18538   20619   23819   26429   30977
+                  139 801 1516    3133    4378    5804    7480    9520    10748   12194   13997   16471   18629   20689   23864   26431   30888
+                  115 772 1479    3068    4293    5694    7341    9348    10562   12006   13831   16356   18563   20654   23861   26460   30964
+                  140 797 1504    3101    4334    5748    7413    9440    10659   12100   13917   16422   18614   20701   23916   26491   30963
+                  137 795 1504    3104    4336    5746    7401    9414    10625   12058   13863   16377   18577   20661   23860   26453   30967
+                  146 802 1505    3095    4323    5732    7391    9414    10635   12089   13920   16423   18603   20681   23881   26450   30915
+                  160 819 1529    3133    4368    5784    7448    9474    10693   12131   13933   16424   18607   20684   23878   26465   30964
+                  134 791 1497    3089    4318    5729    7387    9410    10629   12070   13881   16389   18582   20665   23872   26466   30972
+                  142 801 1511    3109    4338    5746    7406    9431    10652   12098   13910   16414   18609   20692   23877   26449   30943
+                  145 804 1514    3117    4353    5767    7430    9455    10675   12113   13913   16408   18596   20680   23876   26452   30938
+                  140 800 1511    3114    4348    5760    7421    9447    10667   12106   13911   16399   18571   20634   23828   26427   30943
+                  147 803 1510    3108    4340    5753    7412    9426    10642   12082   13895   16399   18582   20659   23861   26450   30942", header = TRUE)
> head(df)
  Height Height.1 Height.2 Height.3 Height.4 Height.5 Height.6 Height.7 Height.8 Height.9 Height.10 Height.11 Height.12 Height.13 Height.14 Height.15 Height.16
1    147      800     1500     3077     4299     5704     7361     9388    10610    12051     13855     16354     18549     20643     23860     26471     31004
2    141      797     1503     3096     4325     5733     7388     9404    10616    12051     13853     16352     18552     20649     23891     26504     31055
3    148      806     1515     3112     4342     5753     7409     9425    10638    12072     13874     16374     18572     20670     23896     26505     31050
4    145      808     1525     3136     4369     5779     7435     9449    10661    12096     13903     16418     18624     20710     23916     26511     31048
5    146      807     1523     3143     4390     5815     7489     9527    10751    12191     13984     16451     18613     20677     23871     26462     30989
6    151      807     1512     3101     4327     5730     7380     9392    10604    12037     13843     16361     18572     20672     23897     26499     31031
> densities <- lapply(df, density)
> densities
$Height

Call:
    density.default(x = X[[1L]])

Data: X[[1L]] (30 obs.);    Bandwidth 'bw' = 2.892

       x               y            
 Min.   :106.3   Min.   :5.163e-05  
 1st Qu.:121.9   1st Qu.:2.766e-03  
 Median :137.5   Median :6.527e-03  
 Mean   :137.5   Mean   :1.602e-02  
 3rd Qu.:153.1   3rd Qu.:2.525e-02  
 Max.   :168.7   Max.   :5.634e-02  

$Height.1

Call:
    density.default(x = X[[2L]])

Data: X[[2L]] (30 obs.);    Bandwidth 'bw' = 2.636

       x               y            
 Min.   :764.1   Min.   :5.689e-05  
 1st Qu.:779.8   1st Qu.:3.005e-03  
 Median :795.5   Median :6.958e-03  
 Mean   :795.5   Mean   :1.590e-02  
 3rd Qu.:811.2   3rd Qu.:2.374e-02  
 Max.   :826.9   Max.   :5.675e-02  

$Height.2

Call:
    density.default(x = X[[3L]])

Data: X[[3L]] (30 obs.);    Bandwidth 'bw' = 4.337

       x              y            
 Min.   :1466   Min.   :3.442e-05  
 1st Qu.:1485   1st Qu.:2.819e-03  
 Median :1504   Median :8.363e-03  
 Mean   :1504   Mean   :1.314e-02  
 3rd Qu.:1523   3rd Qu.:2.503e-02  
 Max.   :1542   Max.   :3.410e-02  

$Height.3

Call:
    density.default(x = X[[4L]])

Data: X[[4L]] (30 obs.);    Bandwidth 'bw' = 8.932

       x              y            
 Min.   :3041   Min.   :1.732e-05  
 1st Qu.:3073   1st Qu.:1.288e-03  
 Median :3106   Median :6.945e-03  
 Mean   :3106   Mean   :7.768e-03  
 3rd Qu.:3138   3rd Qu.:1.352e-02  
 Max.   :3170   Max.   :1.833e-02  

$Height.4

Call:
    density.default(x = X[[5L]])

Data: X[[5L]] (30 obs.);    Bandwidth 'bw' = 11.58

       x              y            
 Min.   :4258   Min.   :1.555e-05  
 1st Qu.:4300   1st Qu.:1.030e-03  
 Median :4342   Median :5.304e-03  
 Mean   :4342   Mean   :6.000e-03  
 3rd Qu.:4383   3rd Qu.:1.027e-02  
 Max.   :4425   Max.   :1.446e-02  

$Height.5

Call:
    density.default(x = X[[6L]])

Data: X[[6L]] (30 obs.);    Bandwidth 'bw' = 14.26

       x              y            
 Min.   :5651   Min.   :1.181e-05  
 1st Qu.:5703   1st Qu.:8.090e-04  
 Median :5754   Median :4.250e-03  
 Mean   :5754   Mean   :4.835e-03  
 3rd Qu.:5806   3rd Qu.:8.152e-03  
 Max.   :5858   Max.   :1.210e-02  

$Height.6

Call:
    density.default(x = X[[7L]])

Data: X[[7L]] (30 obs.);    Bandwidth 'bw' = 17.26

       x              y            
 Min.   :7289   Min.   :9.247e-06  
 1st Qu.:7352   1st Qu.:6.671e-04  
 Median :7415   Median :3.541e-03  
 Mean   :7415   Mean   :3.970e-03  
 3rd Qu.:7478   3rd Qu.:6.597e-03  
 Max.   :7541   Max.   :1.028e-02  

$Height.7

Call:
    density.default(x = X[[8L]])

Data: X[[8L]] (30 obs.);    Bandwidth 'bw' = 19.9

       x              y            
 Min.   :9288   Min.   :8.371e-06  
 1st Qu.:9363   1st Qu.:6.267e-04  
 Median :9438   Median :2.951e-03  
 Mean   :9438   Mean   :3.348e-03  
 3rd Qu.:9512   3rd Qu.:5.425e-03  
 Max.   :9587   Max.   :9.110e-03  

$Height.8

Call:
    density.default(x = X[[9L]])

Data: X[[9L]] (30 obs.);    Bandwidth 'bw' = 20.84

       x               y            
 Min.   :10499   Min.   :8.462e-06  
 1st Qu.:10578   1st Qu.:6.410e-04  
 Median :10656   Median :2.767e-03  
 Mean   :10656   Mean   :3.181e-03  
 3rd Qu.:10735   3rd Qu.:5.180e-03  
 Max.   :10814   Max.   :8.568e-03  

$Height.9

Call:
    density.default(x = X[[10L]])

Data: X[[10L]] (30 obs.);   Bandwidth 'bw' = 19.48

       x               y            
 Min.   :11948   Min.   :1.019e-05  
 1st Qu.:12024   1st Qu.:7.631e-04  
 Median :12100   Median :2.711e-03  
 Mean   :12100   Mean   :3.277e-03  
 3rd Qu.:12176   3rd Qu.:5.453e-03  
 Max.   :12252   Max.   :8.338e-03  

$Height.10

Call:
    density.default(x = X[[11L]])

Data: X[[11L]] (30 obs.);   Bandwidth 'bw' = 17.69

       x               y            
 Min.   :13777   Min.   :9.179e-06  
 1st Qu.:13845   1st Qu.:8.426e-04  
 Median :13914   Median :3.204e-03  
 Mean   :13914   Mean   :3.657e-03  
 3rd Qu.:13982   3rd Qu.:6.143e-03  
 Max.   :14050   Max.   :8.714e-03  

$Height.11

Call:
    density.default(x = X[[12L]])

Data: X[[12L]] (30 obs.);   Bandwidth 'bw' = 15.09

       x               y            
 Min.   :16295   Min.   :1.002e-05  
 1st Qu.:16350   1st Qu.:7.212e-04  
 Median :16406   Median :3.833e-03  
 Mean   :16406   Mean   :4.509e-03  
 3rd Qu.:16461   3rd Qu.:7.778e-03  
 Max.   :16516   Max.   :1.107e-02  

$Height.12

Call:
    density.default(x = X[[13L]])

Data: X[[13L]] (30 obs.);   Bandwidth 'bw' = 11.76

       x               y            
 Min.   :18503   Min.   :1.383e-05  
 1st Qu.:18545   1st Qu.:9.345e-04  
 Median :18588   Median :5.071e-03  
 Mean   :18588   Mean   :5.856e-03  
 3rd Qu.:18631   3rd Qu.:1.130e-02  
 Max.   :18673   Max.   :1.331e-02  

$Height.13

Call:
    density.default(x = X[[14L]])

Data: X[[14L]] (30 obs.);   Bandwidth 'bw' = 10.21

       x               y            
 Min.   :20588   Min.   :0.0000148  
 1st Qu.:20630   1st Qu.:0.0009466  
 Median :20671   Median :0.0044708  
 Mean   :20671   Mean   :0.0060456  
 3rd Qu.:20712   3rd Qu.:0.0106032  
 Max.   :20754   Max.   :0.0165834  

$Height.14

Call:
    density.default(x = X[[15L]])

Data: X[[15L]] (30 obs.);   Bandwidth 'bw' = 11.48

       x               y            
 Min.   :23785   Min.   :1.303e-05  
 1st Qu.:23833   1st Qu.:9.047e-04  
 Median :23881   Median :3.397e-03  
 Mean   :23881   Mean   :5.179e-03  
 3rd Qu.:23929   3rd Qu.:9.035e-03  
 Max.   :23977   Max.   :1.482e-02  

$Height.15

Call:
    density.default(x = X[[16L]])

Data: X[[16L]] (30 obs.);   Bandwidth 'bw' = 14.04

       x               y            
 Min.   :26385   Min.   :0.0000107  
 1st Qu.:26436   1st Qu.:0.0008417  
 Median :26488   Median :0.0038815  
 Mean   :26488   Mean   :0.0048669  
 3rd Qu.:26539   3rd Qu.:0.0088588  
 Max.   :26590   Max.   :0.0118268  

$Height.16

Call:
    density.default(x = X[[17L]])

Data: X[[17L]] (30 obs.);   Bandwidth 'bw' = 15.99

       x               y            
 Min.   :30840   Min.   :9.374e-06  
 1st Qu.:30916   1st Qu.:8.649e-04  
 Median :30993   Median :2.424e-03  
 Mean   :30993   Mean   :3.276e-03  
 3rd Qu.:31069   3rd Qu.:4.831e-03  
 Max.   :31145   Max.   :9.747e-03  

> plot(densities[[1]], main = "height")
>     plot(densities[[2]], col="red")
>     plot(densities[[3]], col="blue")
>     plot(densities[[4]], col = "green")
>     plot(densities[[5]], col="orange")
>     plot(densities[[6]], col="yellow")
>     plot(densities[[7]], col = "cyan")
>     plot(densities[[8]], col="khaki")
>     plot(densities[[9]], col="purple")
>     plot(densities[[10]], col = "brown")
>     plot(densities[[11]], col="olive")
Error in plot.xy(xy, type, ...) : invalid color name 'olive'
>     plot(densities[[12]], col="royalblue")
>     plot(densities[[13]], col = "pink")
>     plot(densities[[14]], col="seagreen")
>     plot(densities[[15]], col="plum")
>     plot(densities[[16]], col = "tan")
>     plot(densities[[17]], col="black")

enter image description here

Your lapply statement works for me but it looks like it had unexpected results for you. You should check

class(densities) 

and

head(densities)

because the error message indicates that the object densities does not have the expected dimensions in your case.

Upvotes: 1

Related Questions