More than six shapes in ggplot

后端 未结 3 634
北荒
北荒 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: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

提交回复
热议问题