'Percent scale exceed 100% than color specified in aesthetic

I have a data like this (this is a sample, my real data is very much larger)

structure(list(sample = structure(c(7L, 6L, 6L, 1L, 2L, 6L, 3L, 
6L, 6L, 6L, 6L, 4L, 6L, 6L, 6L, 5L, 3L, 6L, 6L, 6L, 2L, 6L, 2L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 1L, 6L, 2L, 6L, 6L, 6L, 7L, 
6L, 6L, 6L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 2L, 6L, 2L, 6L, 6L, 6L, 6L, 6L, 7L, 6L, 6L, 6L, 3L, 6L, 6L, 
6L, 6L, 2L, 6L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("D", 
"B", "C", "E", "F", "A", "G"), class = "factor"), value = c(1.57560386370811, 
2.74767123459724, 1.48766063814693, 0.261551226670014, 1.00306151063661, 
1.02247863217139, 1.76353144287261, 2.23587290569418, 1.49168023330427, 
2.11621214818055, 1.27094679058081, 2.73839511842485, 1.87583038042473, 
1.59641420780167, 2.19836509026318, 0.635473395290403, 0.192993714489996, 
1.92481798200276, 1.58194775012715, 1.26479921517767, 0.327744730973777, 
1.11285902151151, 0.699530216689122, 1.59166071568725, 1.77676535187062, 
1.75986030495188, 1.31865020998268, 1.34421317790374, 1.03179959677329, 
1.43498038031802, 2.1072043118662, 1.14001243767691, 2.58744366379114, 
1.53324102734542, 1.34507268247757, 1.84174496717647, 1.90615474653985, 
1.7210096880912, 1.41695785259264, 0.124163802789654, 2.06258611055419, 
0.788038275478947, 1.32879855922399, 2.58393870879032, 2.26249595219018, 
2.05452405403729, 0.832090014427472, 1.4257137557524, 0.901623893164089, 
2.68079592002146, 1.05703668787781, 2.28339182829257, 0.890065229700846, 
1.63963247229921, 0.276160235737066, 1.38070909797962, 1.74373583704741, 
2.11923229219318, 1.77562483894366, 2.6259241843592, 1.34597293869339, 
1.97292044285211, 1.08219553003877, 2.09203186946828, 1.497866136777, 
0.331141762468312, 0.181744531972837, 2.22002119299133, 1.23579133952328, 
1.40361027815778, 1.16220617126205, 2.05121351532439, 0.885674235983964, 
0.373502647617544, 1.18266440387724, 1.77656044315736, 1.25666361193225, 
1.2378693071306, 2.01761459936079, 2.57802071105584, 1.65847348138692, 
1.63150167437282, 1.49009197829826, 0.120728043996333, 1.48892124132217, 
1.85182701968466, 1.87423503572575, 0.117067941873975, 0.442659100919283, 
1.19701680876233, 1.966186777295, 1.03264695689504, 1.8188161314621, 
1.70641589964924, 1.37212626434516, 1.90022561488552, 1.22146530281275, 
1.39344122533287, 1.9761657281558, 1.22903027342813)), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"))

When I create plot without a color in aes it creates in expected way

ggplot(data = data, aes(x = value)) +
    geom_density() +
    scale_y_continuous(labels = percent)

enter image description here

If I add sample variable for defining color in aesthetics the Y axis exceeds limit in 100%

ggplot(data = data, aes(x = value, color = sample)) +
    geom_density() +
    scale_y_continuous(labels = percent)

enter image description here

I have tried to group dataframe and use sample variable as factor but always get this unexpected behaviour. I am out of ideas how to fix it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source