Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than six shapes in ggplot

People also ask

What is the maximum number of shapes that you are allowed to use in ggplot2 by default?

ggplot2 will only use six shapes at a time. By default, additional groups will go unplotted when you use this aesthetic. For each aesthetic you use, the aes() to associate the name of the aesthetic with a variable to display.

How do I assign shapes in ggplot2?

You can change the number to plot different shapes, i.e. geom_point(shape = x) . If you want to change point shapes based on a grouping variable, then first set the shape with the grouping variable in geom_point and then use scale_shape_manual to choose the desired shapes (optional).

What is geom_point?

The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.


First, it would be easier to convert sn to a factor.

df$sn <- factor(df$sn)

Then, you need to use scale_shape_manual to specify your shapes to use.

gp <- ggplot(df,aes(x=t, y=y, group=sn,color=sn, shape=sn)) +
             scale_shape_manual(values=1:nlevels(df$sn)) +
             labs(title = "Demo more than 6 shapes", x="Theat (deg)", y="Magnitude") +
             geom_line() + 
             geom_point(size=3)
gp

This should give you what you want. You need to use scale_shape_manual because, even with sn as a factor, ggplot will only add up to 6 different symbols automatically. After that you have to specify them manually. You can change your symbols in a number of ways. Have a look at these pages for more information on how: http://sape.inf.usi.ch/quick-reference/ggplot2/shape
http://www.cookbook-r.com/Graphs/Shapes_and_line_types/

enter image description here


For me, the key to the error message about the 6 shapes is the part that says Consider specifying shapes manually..

If you add in the values in scale_shape_manual, I believe you'll get what you want. I made sn a factor in the dataset first.

df$sn = factor(df$sn)

ggplot(df, aes(x = t, y = y, group = sn, color = sn, shape = sn)) +
    geom_point() +
    geom_line() +
    scale_shape_manual(values = 0:10)

I go to the Cookbook for R site when I need to remember which numbers correspond to which shapes.

Edit The example above shows adding 11 symbols, the same number of symbols in your example dataset. Your comments indicate that you have many more unique values for the sn variable than in your example. Be careful with using a long series of numbers in values, as not all numbers are defined as symbols.

Ignoring whether it is a good idea to have so many shapes in a single graphic or not, you can use letters and numbers as well as symbols as shapes. So if you wanted, say, 73 unique shapes based on a factor with 73 levels, you could use 19 symbols, all upper and lower case letters, and the numbers 0 and 1 as your values.

scale_shape_manual(values = c(0:18, letters, LETTERS, "0", "1"))

you can get about a hundred different shapes if you need them. good.shapes is a vector of the shape numbers that render on my screen without any fill argument.

library(ggplot2)
N = 100; M = 1000
good.shapes = c(1:25,33:127)
foo = data.frame( x = rnorm(M), y = rnorm(M), s = factor( sample(1:N, M, replace = TRUE) ) )
ggplot(aes(x,y,shape=s ), data=foo ) +
    scale_shape_manual(values=good.shapes[1:N]) +
        geom_point()