More than six shapes in ggplot

后端 未结 3 635
北荒
北荒 2021-01-31 02:57

I would like to plot lines with different shapes with more than six sets of data, using discrete colors. The problems are 1) a different legend is generated for line color and

相关标签:
3条回答
  • 2021-01-31 03:41

    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"))
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2021-01-31 03:52

    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

    0 讨论(0)
提交回复
热议问题