Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_point() rainbow colors

Tags:

r

ggplot2

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?

like image 961
Lani Avatar asked Mar 31 '19 16:03

Lani


People also ask

How do I specify colors in ggplot2?

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.

How do you set a color palette in R?

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.

What are the default Ggplot colors?

By default, ggplot2 chooses to use a specific shade of red, green, and blue for the bars.

What are the different types of color scales?

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.

What is a point Geom used for in Python?

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.

What is the default color of gradient in ggplot2?

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))+

What is the difference between Point Geom and scatterplot?

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.


1 Answers

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)) 

enter image description here

like image 53
erc Avatar answered Oct 21 '22 06:10

erc