# Set color and linetype of circle by group

Hi, community
I met an intersesting question when practicing with R. I draw the circle, and have the id. Is it possible to draw different color and line type? Like when the id are 1，2，3，4，5，6 the color is black, lty is dashed. For other number the color is red, lty is solid.
Many thanks!

``````x1<-c(10.3,13.4,14.3,16.4,17.4,19.1,19.2,18.5,18.3,16.8,16.7,15.5)
y1<-c(10.1,13.3,13.3,13.7,14.2,11.4,12.2,14.9,16.4,15.8,17.8,16.5)
r<-c(1.00,0.95,0.83,0.88,1.05,0.48,0.93,0.90,0.93,0.53,0.83,0.68)
id<-c(1:12)
library(ggplot2)
library(ggforce)
circles<-data.frame(
x0=x1,
y0=y1,
r=r,
id=id
)
p<-ggplot(data = circles)+
geom_point(aes(x=x0,y=y0),color="black",shape=1)+
geom_circle(aes(x0=x0,y0=y0,r=r),
data = circles,
color = "red",
alpha=0.05,
linetype="dashed")+
geom_text(aes(circles\$x0,circles\$y0,label=circles\$id,size=3,angle=0,family="mono"))+
theme(legend.position = 'none')
p
``````

you can take complete control with scale_*_manual functions.

``````x1<-c(10.3,13.4,14.3,16.4,17.4,19.1,19.2,18.5,18.3,16.8,16.7,15.5)
y1<-c(10.1,13.3,13.3,13.7,14.2,11.4,12.2,14.9,16.4,15.8,17.8,16.5)
r<-c(1.00,0.95,0.83,0.88,1.05,0.48,0.93,0.90,0.93,0.53,0.83,0.68)
id<-c(1:12)
library(ggplot2)
library(ggforce)
circles<-data.frame(
x0=x1,
y0=y1,
r=r,
id=id
)
(p<-ggplot(data = circles)+
geom_circle(aes(x0=x0,y0=y0,r=r,
linetype=factor(id),
color=factor(id)),
alpha=0.05)+
geom_text(aes(x0,y0,label=id,size=3,angle=0,family="mono"))+
theme(legend.position = 'none')+
scale_linetype_manual(values=c("1"="dashed",
"2"="dotted",
"3"="dotdash",
"4"="solid")) +
scale_color_manual(values=c("1"="red",
"2"="green",
"3"="blue",
"4"="black")))``````
1 Like

Thank you. While if there are many circles, more than 100, just point them one by one? If i want to set the circle(id is between 1~50) is one type. circle(id is between 51~100) is another type?

construct the scales programattically like :

``````# ids 1 to 4 should be dashed, others solid
(t_scale <- rep("solid",12))
t_scale[1:4] <- "dashed"
t_scale
names(t_scale) <- 1:12
t_scale
# ids 4 to 8 should red others blues
(c_scale <- rep("blue",12))
c_scale[4:8] <- "red"
c_scale
names(c_scale) <- 1:12
c_scale
``````

and use them in place

``````+ scale_linetype_manual(values = t_scale) +
scale_color_manual(values = c_scale))``````
1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.