R manually set shape by factor

后端 未结 2 1794
粉色の甜心
粉色の甜心 2021-01-17 16:25

Asked this question the other day but no one could visualize my question so ive made an example.

A <- c(\'a\',\'b\', \'c\',\'d\',\'e\')
types <- fa         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 16:38

    I'm certain this is no longer relevant for the OP but the best solution I found to this problem is simpler than what is currently posted and is almost written into the question itself.

    The OP's wish of assigning a manualy defined shape or colour using something like
    "scale_shape_manual(values=('a'=15, 'b'=18, 'c'=16, 'd'=17, 'e'=19))"
    only requires the assignments to be passed as a vector as in,
    scale_shape_manual(values = c('a'=15, 'b'=18, 'c'=16, 'd'=17, 'e'=19))

    jlhoward's answer is better if you want autogenerated colours. Whereas the script I offer bellow requires fewer lines of code. Users choice.

    A <- c('a','b', 'c','d','e')
    types <- factor(A)
    B <- c(1,2,3,4,5)
    C <- c(6,7,8,9,10)
    D <- c(1,2,1,2,3)
    ABC <- data.frame(B,C,D,types)
    
    library(ggplot2)
    
    ggplot(ABC, aes(x=B ,y=C ,size=D, colour=as.factor(types),label=types, shape=as.factor(types))) +
    geom_point()+geom_text(size=2, hjust=0,colour="black", vjust=0) +
    scale_size_area(max_size=20, "D", breaks=c(100,500,1000,3000,5000))  +
    scale_x_log10(lim=c(0.05,10),breaks=c(0.1,1,10))+
    scale_y_continuous(lim=c(0,30000000)) +
    scale_shape_manual(values = c('a'=15, 'b'=18, 'c'=16, 'd'=17, 'e'=19)) +
    scale_colour_manual(values = c('a'="tomato", 'b'="yellow4", 'c'="palegreen2", 'd'="deepskyblue1", 'e'="orchid3"))`
    

提交回复
热议问题