# R manually sets the shape by factor

Asked question the other day, but nobody could imagine my question, so I made an example.

``````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(15,18,16,17,19))`
```

```

In this case, you will have ae factors that have colors and shapes attributed to them.

In my code, I use scale_shape_manual to set the shapes, and they are defined by a sequence, that is, the order of the factors is a, b, c, d, e and my values ​​are 15,18,16,17,19, so a = 15 (square), b = 18, etc. Etc.

I would like to set these shapes at times. My data will change every day and the factors will be in different orders, but I always want the same factors to have the same shapes.

So obviously this code doesn't work, but something like:

`scale_shape_manual(values=('a'=15, 'b'=18, 'c'=16, 'd'=17, 'e'=19))`

It would be helpful if I could do the same for the color.

thank

+3

source to share

If I understand you correctly, there will always be (at most) five categories "a" - "e" and you want their shapes and colors to be consistent across datasets. Here's one way (note: `gg_color_hue(...)`

from here ):

``````# set up shapes
shapes <- c(15,18,16,17,19)
names(shapes) <- letters[1:5]

# set up colors
gg_color_hue <- function(n) { # ggplot default colors
hues = seq(15, 375, length=n+1)
hcl(h=hues, l=65, c=100)[1:n]
}
colors <- gg_color_hue(5)
names(colors) <- names(shapes)

# original data
ggplot(ABC, aes(x=B ,y=C ,size=D, colour=types,label=types, shape=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=shapes) + scale_color_manual(values=colors)
```

```

``````#new data
DEF <- data.frame(B,C,D,types=factor(c("a","a","a","d","e")))
ggplot(DEF, aes(x=B ,y=C ,size=D, colour=types,label=types, shape=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=shapes) + scale_color_manual(values=colors)
```

```

+3

source

All Articles