R: About the color and size classification of the scatterplot using ggplot

Asked 2 years ago, Updated 2 years ago, 118 views

In the scatterplot using the following data,
I would like to specify any color and size.
Reg takes an integer from 1 to 6, but increases the size of 1 and
I would like to reduce the size of 6.
Data Name Res

Reg hw Sex
11 180 60 M
22 15550 F
33 16055F
42170 65 M
....

when executed with the following code:
p_colour<-c("red", "darkorange", "yellow", "green", "blue", "grey40")
p_size<-c(6,5,4,3,2,1)

base<-ggplot(Res,aes(x=h,y=w,size=Reg,colour=as.factor(Reg)))))
points<-base+geom_point()
change_colors<-points+scale_colour_manual(values=p_colour)
change_size<-change_colors+scale_size_manual(values=p_size)
plot(change_sizes)

The following error indicates that the discrete value has been given a continuous value.

Error: Continuous value supplied to discard scale

How can I change it?Thank you for your cooperation.

r ggplot2

2022-09-30 21:27

1 Answers

This is because when creating a base with the ggplot function, size = Reg (in this case, the int type is included in the size).
So if you modify the line as follows, you will probably be able to draw the plot you want:

base<-ggplot(Res,aes(x=h,y=w,size=as.factor(Reg),colour=as.factor(Reg))))


2022-09-30 21:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.