More than six shapes in ggplot

后端 未结 3 677
北荒
北荒 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"))
    

提交回复
热议问题