ggplot2 color gradient per group

Srecko

Here is the sample I was able to make it work:

find_hull <- function(df) df[chull(df$Sepal.Length, df$Sepal.Width), ]
hulls <- ddply(iris, "Species", find_hull)

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, shape=Species, 
                         color=Species, size=Petal.Width)) + 
  geom_point() +
  ggtitle("Sepal.Length vs. Sepal.Width - size Petal.Width") +
  scale_shape_manual(values=c(15,16,17)) +
  geom_polygon(data = hulls, alpha = 0.15, size=0.2) +
  scale_size(guide = "none")

What I would like to do now is to specify gradient for each of the groups (i.e., setosa, versicolor, and virginica), based on the Petal.Width. That is, let's say setosa group are read squares, where size of each point depends on the Petal.Width. I would like that this red color varies (from white to red), based on the petal width. If virginica are blue triangles, I would like that color of those triangles also varies with the size (petal width).

Any ideas what would be an easy way to do something like that?

Weihuang Wong

A bit hacky -- to get the "fade to white" effect, I added a white geom_point, and overlaid that with another geom_point where alpha is mapped to Petal.Width. So points with the largest values of Petal.Width will be solid colours, fading to white as Petal.Width decreases.

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, shape=Species, 
                         color=Species, size=Petal.Width)) + 
  geom_point(colour = "white") +
  geom_point(aes(alpha = Petal.Width, colour = Species)) +
  ggtitle("Sepal.Length vs. Sepal.Width - size Petal.Width") +
  scale_shape_manual(values=c(15,16,17)) +
  geom_polygon(data = hulls, alpha = 0.15, size=0.2) +
  scale_size(guide = "none") +
  scale_alpha_continuous(guide = FALSE) 

Output:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ggridges color gradient per group

Gradient with specific color on ggplot2

ggplot2: Combining group, color and linetype

How to obtain a horizontal color gradient in ggplot2?

Continuous gradient color & fixed scale heatmap ggplot2

Add gradient color within groups in ggplot2

Gradient-color a line in ggplot2 R

R - Getting correct two color gradient on ggplot2 map

ggplot2: Reversing the standard color gradient for a continuous variable

Filling bars with a color gradient in barplot ggplot2 R

How to fill shapes with color gradient in ggplot2

Assigning Individual Colors to a Gradient Color Scheme in ggplot2

Add number of observations per group in ggplot2 boxplot

add value total of column and per group in histogram ggplot2

Cumulative percentage in a barplot with multiple value per group (ggplot2)

ggplot histogram color gradient

ggplot Color by factor and gradient

Two seperate color gradient color scales on one ggplot2 map

How to color facet_grid by group in ggplot2?

ggplot2 draw individual ellipses but color by group

How to group Boxplots without use of color or fill in ggplot2

R ggplot2 Specify separate color gradients by group

Color code a scatter plot by group with a gradient

ggplot2, why so blue? How to set a different color palette in gradient

scale_fill in ggplot2 - Make a gradient color for every plot in a list of ggplots

R: Can I use scale_color_gradient to change the colour of geom_hline in ggplot2?

Extract ggplot2 color code information generated from `scale_fill_gradient`

R: Using dplyr group_by with ggplot2 and changing title per group

ggplot map fill with gradient color