I'm working on the diamonds data set. I am trying to plot carats x price, with the color representing clarity. I get a beautiful color palette, but not the one I want. It looks more like a gradient, and I want the rainbow, with each clarity having a unique color (easier for such a dense data plot, I think).
When I have created my basic plot, everything works. Once I try to add in scale_colour_gradientn(colours=rainbow())
and any variants, I get an error.
#what works
ggplot(diamonds, aes(x=carat, y=price, color=clarity)) +
geom_point()
#what doesn't...
ggplot(diamonds, aes(x=carat, y=price, color=clarity)) +
geom_point() +
scale_colour_gradientn(colors=rainbow(7))
I would like to see colors but instead, I get the feedback that discrete value is supplied to a continuous scale. How would I fix this?
To specify colors of the bar in Barplot in ggplot2, we use the scale_fill_manual function of the ggplot2 package. Within this function, we need to specify a color for each of the bars as a vector. We can use colors using names as well as hex codes.
In R, it is possible to specify a color in several ways: by name, col = "red" ; by hex code, col = "#FF0000" ; or by number, col = 2 . The last of these, a numeric color specification, is a numeric index into a “color palette”, which is controlled via the palette() function.
By default, ggplot2 chooses to use a specific shade of red, green, and blue for the bars.
There are three main types of colour schemes: sequential, diverging, and qualitative and they each are best used to describe different types of data. We will now explore these colour schemes with some interactive visualizations.
Points. The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables. It can be used to compare one continuous and one categorical variable, or two categorical variables, but a variation like geom_jitter() , geom_count(), or geom_bin2d() is usually more appropriate.
The default ggplot2 setting for gradient colors is a continuous blue color. In the following example, we color points according to the variable: Sepal.Length. sp <- ggplot(iris, aes(Sepal.Length, Sepal.Width))+
The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables. It can be used to compare one continuous and one categorical variable, or two categorical variables, but a variation like geom_jitter(), geom_count(), or geom_bin2d() is usually more appropriate.
scale_colour_gradientn()
creates a colour gradient for continuous values. If you instead want discrete values to have distinct colours use scale_colour_manual()
. Further, colours are assigned using values =
:
ggplot(diamonds, aes(x = carat, y = price, colour = clarity)) +
geom_point() +
scale_colour_manual(values = rainbow(8))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With