Open In App

How to custom fill color in ggvis

Last Updated : 01 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The popular ggplot2 package served as an inspiration for the development of this package, which shared its fundamental principles of layered graphics, aesthetic mapping of variables, and plot composition using various geometric objects.A web-based graphics system called ggvis offered the ability to create interactive graphics that could be examined and changed in real-time. The JavaScript programming language and the Reactive programming paradigm were combined to accomplish this. A variety of graphics, including scatter plots, bar charts, line charts, and more, could be rendered by the package.

The customize the fill color of a visual element using the fill aesthetic. The fill aesthetic specifies the fill color of  visual element such as a point, line, or  shape.

To customize the fill color in the ggvis, you can use  scale_*_manual() function to create a custom scale for the fill aesthetic. The scale_*_manual() function allows  to specify the mapping between the values of the fill variable.

Steps :

1. Load the ggvis package.

R




library(ggvis)


2. Create the  data frame with the variables you want to plot.

R




df <- data.frame(
  x = c(1, 2, 3),
  y = c(2, 3, 1),
  color = c("red", "green", "blue")
)


3. A data frame named df is created by this line, and it has three columns: x, y, and colour. There are three numeric or character data entries in each column.

R




plot <- df %>%
  ggvis(x = ~x, y = ~y)


4. The ggvis tool is used in this line to generate a scatter plot. The symbols x and y denote that the values for the x-axis and y-axis should, respectively, originate from the x and y columns of the df data frame. With the fill parameter set to map each point’s colour to its associated value in the data frame’s colour column, the layer_points function adds new points to the plot.

R




plot <- plot %>%
  layer_points(fill := ~color)


5.By mapping each distinct value in the colour column of df to a darker shade of the associated color, this line generates a named vector called fill_colors.

This line gives the map produced in step 3 a unique fill scale. Each distinct value in the fill_colors vector is mapped to a matching value in the colour column of df by the scale_nominal function. The darker shades defined by fill_colors will be used to colour the points in the final plot.

R




fill_colors <- c("red" = "darkred", "green" = "darkgreen", "blue" = "darkblue")
 
plot <- plot %>%
  scale_nominal("fill", domain = unique(df$color), range = fill_colors)


6. Render  plot using the print() function.

print(plot)

Example :

R




library(ggvis)
 
df <- data.frame(
  x = c(1, 2, 3),
  y = c(2, 3, 1),
  color = c("red", "green", "blue")
)
 
# Create the ggvis plot
plot <- df %>%
  ggvis(~x, ~y) %>%
  layer_points(fill := ~color)
 
# Define a custom fill scale
fill_colors <- c("red" = "darkred", "green" = "darkgreen", "blue" = "darkblue")
plot %>% scale_nominal("fill", domain = unique(df$color), range = fill_colors)
 
plot


Output :

 

Bar Chart Example

R




library(ggvis)
 
# Create a data frame with two columns
df <- data.frame(category = c("A", "B", "C"), value = c(10, 20, 30), color = c("red", "green", "blue"))
 
# Create a ggvis plot with bars and custom fill colors
fill_colors <- c("red" = "darkred", "green" = "darkgreen", "blue" = "darkblue")
plot <- df %>%
  ggvis(x = ~category, y = ~value) %>%
  layer_bars(fill = ~color) %>%
  scale_nominal("fill", domain = unique(df$color), range = fill_colors)
 
# Print the resulting plot
plot


output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads