Symp_plot % # begin with linelist select ( c ( case_id, fever, chills, cough, aches, vomit ) ) %>% # select columns pivot_longer ( # pivot longer cols = - case_id, names_to = "symptom_name", values_to = "symptom_is_present" ) %>% mutate ( # replace missing values symptom_is_present = replace_na ( symptom_is_present, "unknown" ) ) %>% ggplot ( # begin ggplot! mapping = aes (x = symptom_name, fill = symptom_is_present ) ) + geom_bar (position = "fill", col = "black" ) + theme_classic ( ) + theme (legend.position = "bottom" ) + labs ( x = "Symptom", y = "Symptom status (proportion)" ) symp_plot # print with default colors # print with manually-specified colors symp_plot + scale_fill_manual ( values = c ( "yes" = "black", # explicitly define colours "no" = "white", "unknown" = "grey" ), breaks = c ( "yes", "no", "unknown" ), # order the factors correctly name = "" # set legend to no title ) # print with viridis discrete colors symp_plot + scale_fill_viridis_d ( breaks = c ( "yes", "no", "unknown" ), name = "" ) Trans_matrix + scale_fill_gradient ( # 2-sided gradient scale low = "aquamarine", # low value high = "purple", # high value na.value = "grey", # value for NA name = "Density" ) + # Legend title labs (title = "Manually specify high/low colors" ) # 3+ colors to scale trans_matrix + scale_fill_gradientn ( # 3-color scale (low/mid/high) colors = c ( "blue", "yellow", "red" ) # provide colors in vector ) + labs (title = "3-color scale" ) # Use of rescale() to adjust placement of colors along scale trans_matrix + scale_fill_gradientn ( # provide any number of colors colors = c ( "blue", "yellow", "red", "black" ), values = scales :: rescale ( c ( 0, 0.05, 0.07, 0.10, 0.15, 0.20, 0.3, 0.5 ) ) # positions for colors are rescaled between 0 and 1 ) + labs (title = "Colors not evenly positioned" ) # use of limits to cut-off values that get fill color trans_matrix + scale_fill_gradientn ( colors = c ( "blue", "yellow", "red" ), limits = c ( 0, 0.0002 ) ) + labs (title = "Restrict value limits, resulting in grey space" ) # SCALES ADJUSTED ggplot (data = linelist ) + geom_bar (mapping = aes (x = outcome, fill = gender ), color = "black" ) + theme_minimal ( ) + # simplify background scale_y_continuous ( # continuous scale for y-axis (counts) expand = c ( 0, 0 ), # no padding breaks = seq (from = 0, to = 3000, by = 500 ) ) + scale_x_discrete ( # discrete scale for x-axis (gender) expand = c ( 0, 0 ), # no padding drop = FALSE, # show all factor levels (even if not in data) na.translate = FALSE, # remove NA outcomes from plot labels = c ( "Died", "Recovered" ) ) + # Change display of values scale_fill_manual ( # Manually specify fill (bar interior color) values = c ( "m" = "violetred", # reference values in data to assign colors "f" = "aquamarine" ), labels = c ( "m" = "Male", # re-label the legend (use "=" assignment to avoid mistakes) "f" = "Female", "Missing" ), name = "Gender", # title of legend na.value = "grey" # assign a color for missing values ) + labs (title = "Adjustment of scales" ) # Adjust the title of the fill legend If you have multiple scales, you may use multiple scale functions to adjust them! For example: There are others, but these are the most-often used.īe sure that you use the correct function for the scale! Otherwise your scale command will not appear to change anything. The third part, the METHOD, will be either _discrete(), continuous(), _date(), _gradient(), or _manual() depending on the class of the column and how you want to control it.
0 Comments
Leave a Reply. |