Brian-Elbel-s-Research-Projects / project-overview

A summary of current and past research projects in Elbel lab
0 stars 0 forks source link

Standardize colors in legends #26

Closed eriliawu closed 1 year ago

eriliawu commented 2 years ago

Standardize colors for sub-group analysis. i.e. make sure colors in the by-group analysis are consistent across different analysts’ project. (we can talk about this in our Monday morning meeting)

EmilHafeez commented 2 years ago

https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html ?

EmilHafeez commented 2 years ago

please see template code change below.

Replace

scale_color_manual(name="Open Time \nAfter Labeling", labels=c("12","15","18","21","24","27","30"), values=c("hotpink","olivedrab3","red","orange","grey","purple","#13B0E4")) +

with

scale_color_viridis(discrete=TRUE, option="turbo",labels = c("Lunch (11:00-13:59)", "Afternoon (14:00-16:59)", "Dinner (17:00-20:59)", "Evening (21:00-23:59)")) + guides(col=guide_legend("turbo"), size=guide_legend("turbo")) +

eriliawu commented 2 years ago
eriliawu commented 2 years ago
kritisingh21 commented 2 years ago

this code (provided above in sample) -> guides(col=guide_legend("turbo"), size=guide_legend("turbo")) -> results in the legend title being "turbo" so I changed it to "Food Category" for both instances which "fixes" (for a lack of a better word) the legend title....not sure if this correct, but please let me know!

EmilHafeez commented 2 years ago

Solution below:

First, Create a named object where each category of the interesting variable is assigned a Hex color value from the viridis color palette. The only subanalysis with differing number of factors is the by-item-category, so this only applies there. All other analyses should use the viridis color palette but do not need manual tuning. Since Lloyd's analysis includes beverages, he has six total factors, and other analysts will manually set the number of factors in the below code to replicate this, thereby keeping colors consistent.

food_category_name_vector = c("Burrito", "Dessert", "Other Entree", "Side", "Taco", "Beverage")

ggplot_palette_named = setNames(object = scales::viridis_pal()(length(unique(food_category_name_vector))), nm = unique(food_category_name_vector))

Then, use the object in the scale_color_manual values option into the ggplot script such that each named category gets the assigned Hex colors.

scale_color_manual(name = "Food Categories", labels=c("Label 1", "Label 2"), values = ggplot_palette_named) +

Note that this requires we have the factors in a consistent order between analysts, as previously discussed 6/29/22 above.

EmilHafeez commented 2 years ago

color_reference= as.data.frame(c(2,3,4,6,8)) colnames(color_reference) <- c("color_categories") ggplot_palette_named = setNames(object = scales::viridis_pal()(length(unique(color_reference$color_categories))), nm = unique(color_reference$color_categories)) ggplot_palette_named=ggplot_palette_named[-6]

EmilHafeez commented 2 years ago

Please note that the adjustment to retrieve the colors from turbo rather than base viridis is as follows

ggplot_palette_named = setNames(object = scales::viridis_pal(option = "turbo")(length(unique(color_reference$color_categories))), nm = unique(color_reference$color_categories))