You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Using `palette` and `palcolor` arguments to control the colors in the plots
45
+
46
+
Most plotting functions in `plotthis` support two arguments to control colors: `palette` and `palcolor`. These arguments provide flexible color customization while maintaining consistency with predefined palettes.
47
+
48
+
#### The `palette` argument
49
+
50
+
The `palette` argument specifies which predefined color palette to use. You can view all available palettes with `show_palettes()`. For example:
The palette serves as the foundation for color generation. Colors are automatically assigned based on the number of categories or the range of continuous values in your data.
61
+
62
+
#### The `palcolor` argument
63
+
64
+
The `palcolor` argument allows you to override specific colors from the palette. The behavior differs for discrete and continuous color scales:
65
+
66
+
##### For discrete colors (categorical data)
67
+
68
+
Use a **named vector** where names correspond to categories in your data. The function will use the palette as the base and replace only the specified colors:
69
+
70
+
```r
71
+
# Replace specific category colors
72
+
BarPlot(
73
+
data=iris,
74
+
x="Species",
75
+
y="Petal.Length",
76
+
palette="Paired",
77
+
palcolor= c("setosa"="red", "versicolor"="blue")
78
+
)
79
+
# "virginica" will still use the color from the "Paired" palette
80
+
```
81
+
82
+
##### For continuous colors (numeric data)
83
+
84
+
Use a **positional vector** where `NA` values indicate positions to keep from the palette, and non-NA values replace specific positions. The replacement happens **evenly distributed** across the base palette colors:
85
+
86
+
```r
87
+
data(dim_example)
88
+
FeatureDimPlot(
89
+
data=dim_example,
90
+
features="stochasticbasis_1",
91
+
palette="Spectral",
92
+
# The colors will be evenly replace across the palette based on the number of custom colors provided
93
+
# Here are the colors that will be used to generate the ramp colors:
## Handling NA values and unused levels of factors
282
+
283
+
## NA values
284
+
285
+
When NA values appear in the grouping variables or category variables that are used to in the plot (x-axis, fill, color, group, etc.), they will be excluded by default. You can use `keep_na` option to control whether and how to keep the NA values.
286
+
287
+
-`TRUE`: just keep the NA values as they are, and they will be treated as a separate group or category. They will be included in the plot and the legend. The color for the NA values will be `grey80` by default, but you can customize it using the `palcolor` argument (`palcolor = list("NA" = "orange")`).
288
+
-`FALSE`: drop the NA values, and they will not be included in the plot or the legend. This is the default behavior.
289
+
-`"missing"`: or other character string, will replace the NA values with the specified string, and they will be treated as a separate group or category. They will be included in the plot and the legend. The color for the values will be determined by `palette` and `palcolor` as usual.
290
+
291
+
See the above section (Using `palette` and `palcolor` arguments to control the colors in the plots) for more details on how to customize the colors for NA values.
292
+
293
+
## Unused (Empty) levels of factors
294
+
295
+
When there are unused levels of factors in the grouping variables or category variables that are used to in the plot (x-axis, fill, color, group, etc.), they will be included in the plot by default. You can use `keep_empty` option to control whether and how to keep the unused levels of factors.
296
+
297
+
`keep_empty` can take 3 values:
298
+
299
+
-`TRUE`: just keep the unused levels of factors as they are, and they will be treated as separate groups or categories. They will be included in the plot and the legend.
300
+
-`FALSE`: drop the unused levels of factors, and they will not be included in the plot or the legend. This is the default behavior.
301
+
-`"level"` or `"levels"`: The unused levels of factors will not be plotted (for example, on x-axis), but they will be included when determining the colors for the groups or categories, and they will not be included in the legend. Use `TRUE` if you want to include them in the legend.
302
+
303
+
When `keep_empty` is `TRUE` or `"level"`, the colors for the unused levels of factors will be determined by `palette` and `palcolor` as usual, even though they are not plotted (they will affect the colors of existing levels).
# Keep the unused level "C" for color assignment but not plotting
326
+
BarPlot(
327
+
data=data,
328
+
x="x", y="y",
329
+
keep_empty="level"
330
+
)
331
+
```
332
+
333
+
## Variable-level control of keeping NA values and unused levels of factors
334
+
335
+
The `keep_na` and `keep_empty` arguments can also take a named list to control the behavior for each variable separately. The names of the list should correspond to the variables in the data. For example:
0 commit comments