Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Putting different custom colors in different facets in multiple density plots using geom_density()

$
0
0

I am trying to create a faceted density plot using geom_density using a data a part of which is shown below

  x     |   cl   |   |type   | ccl
 0.919 |Group 8 |Candidate |#BCBDDC
 0.538 |Group 4 |Candidate |#FDAE6B
 0.715 |Group 3 |Candidate |#A1D99B
 0.849 |Group 2 |Candidate |#FC9272
 0.710 |Group 2 |Candidate |#FC9272
 0.836 |Group 2 |Candidate |#FC9272

Here each observation belongs to one of 8 groups (given by "cl") and each x is either of type "candidate" or type "Non-candidate" (given by "type"). The column "ccl" gives the color preference for a particular x. I want to do a faceted density plot where the facets will be given by the groups and each facet will have 2 density plots, one for "candidate" and the other for "Non-candidate". To do it I use the following code

jcl <- dd_f_c$ccl
names(jcl) <- dd_f_c$type

p <- ggplot(data = dd_f_c, aes(x=x)) + geom_density(aes(x = x), alpha = 0.4)+ facet_wrap( ~ cl, ncol = 4) +aes(fill = type)+ scale_fill_manual(values = jcl) + ylim(0, 11) + theme_bw()
p <- p  + theme(strip.background = element_rect(fill = alpha('black', 0.1)))
p +labs(x="Classification probability",y="Density")+theme(legend.title = element_blank(), legend.position ="bottom")

Here dd_f_c denotes the dataframe containing the data shown above.

Using the above code I get the following figure

Density plot

I want different colors for the different facets, I want to indicate the candidates using a dark custom color and the non candidate by a light custom color (the preferred colors being given in ccl). For example I want the density plot of candidates in group 1 to be light red and the non candidates to be dark red, the density plot candidates in group 2 to be light green and the non candidates in group 2 dark green etc.

structure(list(x = c(0.919, 0.538, 0.715, 0.849, 0.71, 0.836, 
0.992, 0.644, 0.997, 0.99, 0.961, 0.662, 0.551, 0.966, 0.384, 
0.998, 0.672, 0.946, 0.754, 0.995, 0.404, 0.716, 0.833, 0.438, 
0.953, 0.843, 0.988, 0.954, 0.834, 0.959, 0.766, 0.99, 0.967, 
0.985, 0.662, 0.779, 0.992, 0.989, 0.838, 0.739, 0.984, 0.994
), cl = structure(c(8L, 4L, 3L, 2L, 2L, 2L, 1L, 4L, 3L, 4L, 2L, 
2L, 3L, 5L, 6L, 3L, 1L, 1L, 2L, 1L, 6L, 1L, 2L, 1L, 2L, 2L, 1L, 
2L, 1L, 2L, 3L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 1L, 2L, 2L, 1L), .Label = c("Group 1", 
"Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", 
"Group 8"), class = "factor"), type = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Candidate", "Non-Candidate"
), class = "factor"), ccl = structure(c(8L, 13L, 7L, 12L, 12L, 
12L, 10L, 13L, 7L, 13L, 12L, 12L, 7L, 11L, 11L, 7L, 10L, 10L, 
12L, 10L, 11L, 10L, 12L, 10L, 12L, 12L, 10L, 12L, 10L, 12L, 7L, 
13L, 13L, 13L, 12L, 12L, 13L, 13L, 10L, 12L, 12L, 10L), .Label = c("#00441B", 
"#08306B", "#525252", "#67000D", "#7F2704", "#8C510A", "#A1D99B", 
"#BCBDDC", "#C51B7D", "#DCDCDC", "#F7CBE4", "#FC9272", "#FDAE6B"
), class = "factor")), class = "data.frame", row.names = c(636L, 
637L, 638L, 639L, 640L, 641L, 642L, 643L, 644L, 645L, 835L, 836L, 
837L, 838L, 839L, 840L, 841L, 842L, 843L, 844L, 845L, 846L, 847L, 
848L, 849L, 850L, 851L, 852L, 853L, 854L, 855L, 1635L, 1636L, 
1637L, 1638L, 1639L, 1640L, 1641L, 1642L, 1643L, 1644L, 1645L
))

Any help with this will be highly appreciated.


Viewing all articles
Browse latest Browse all 201839

Trending Articles