Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.
Zhenguo Zhang’s Blog /2025/05/10/r-use-new-scale-xxx-function-to-add-the-same-scale-type-in-different-ggplot-layers/ –
In one ggplot figure, normally you can only use one scale for each aesthetic mapping. For example, if you use scale_color_manual()
to set the color scale for a layer, you cannot use another scale_color_manual()
for another layer, or
set the color scale more then once in the function aes()
. However, you can use the new_scale_color()
function from the ggnewscale
package to add a new scale for the same aesthetic mapping in different layers.
In this post, I will showcase how to use the new_scale_color()
function to add two different color scales in a ggplot figure. The first scale will be for a discrete variable (e.g., number of cylinders), and the second scale will be for a continuous variable (e.g., density level).
Load packages first.
library(ggplot2) library(ggnewscale)
Use the mtcars dataset for the example
data(mtcars)
Create a plot with two color scales:
1. Points colored by ‘cyl’ (discrete)
2. Density contours colored by density level (continuous)
First, let’s make a scatter plot of mpg
vs wt
with points colored by the number of cylinders (cyl
). We will use the geom_point()
function for this layer.
plt <- ggplot(mtcars, aes(x = wt, y = mpg)) + # First layer: Scatter plot colored by cylinders (discrete variable) geom_point(aes(color = factor(cyl)), size = 3) + scale_color_discrete(name = "Cylinders") plt
Set new scale for the next layer
# Reset the color scale for the next layer plt <- plt + new_scale_color()
Add a second layer: Density contours colored by density level (continuous variable)
plt <- plt + geom_density_2d(aes(color = after_stat(level))) + scale_color_viridis_c(name = "Density Level", option = "magma") + # Add labels and theme labs(title = "Dual Color Scales with new_scale_color()", x = "Weight (1000 lbs)", y = "Miles per Gallon") + theme_minimal() plt
Here I demonstrated how to use the new_scale_color()
function from the ggnewscale
package, one can also use new_scale_fill()
for fill aesthetics.
For other aesthetics, such as size
, shape
, etc., you can call
new_scale("size")
, new_scale("shape")
, etc. to add new scales.
To learn more, check the webpage https://eliocamp.github.io/ggnewscale/
Happy programming
– /2025/05/10/r-use-new-scale-xxx-function-to-add-the-same-scale-type-in-different-ggplot-layers/ –
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you’re looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.
Continue reading: [R] Use new_scale_xxx() function to add the same scale type in different ggplot layers
Key Points Analysis and Future Implications
The text elaborates on using the new_scale_color() function from the ggnewscale package to add a new scale for the same aesthetic mapping in different layers of ggplot. The feature allows for the addition of more than one scale, an action which was earlier unattainable. This function adds to the versatility of data presentation in ggplots, permitting users to map multiple variables with different scales to a single aesthetic effectively.
Long-term Implications and Future Developments
This development in enhancing the aesthetic mapping capabilities of ggplot is a significant leap toward improving the visualization tools available in R programming. In the long run, it could accelerate data science progress since better and clearer visualization tools enable data scientists and researchers to extract more insights from their data effectively and efficiently. Expect to observe more advancements in this field in the form of improved or new functions that cater to a wider range of data types and categories, resulting in more informative, visually pleasing, and comprehensive graphical representations of complex data sets.
Actionable Advice
Here are some tips for utilizing this new feature:
- Explore the Package: R users, particularly those involved in data analysis, should acquaint themselves with the ggnewscale package and its features to harness its full potential.
- Practice Implementing: Implement the new_scale_color() in your visualizations. Try to recreate your existing plots using this functionality to compare, contrast, and appreciate its advantages.
- Stay Updated: With constant updates to R packages and their functions, it’s crucial to stay current by regularly checking official documentation and community forums.
- Help Evolve: If you spot any issues or have ideas for enhancements, contribute to the R community by reporting these issues or coming up with solutions.
In a world that increasingly relies on data, tools like ggnewscale that enable clearer, more dynamic visualizations, play a vital role. Leveraging these tools efficiently can dramatically enhance the ability to interpret and draw insights from complex datasets.
Read more on integration and function usage of ggnewscale package here.