0

I have created a ggplot of points that show the mean and sd of the variable "y-axis" in each level of x_axis, and have different shapes according to cat.1 and different colors according to cat.2. There are 3 panels according to "time"

the dataframe "example" can be downloaded from here:

https://drive.google.com/file/d/1fJWp6qoSYgegivA5PgNsQkVFkVlT4qcC/view?usp=sharing

plot1<-ggplot(example,aes(x=x_axis,y=mean , shape = cat.1)) +  theme_bw() +
  facet_wrap(~time,dir = "h")+
  geom_point(aes(color=cat.2), position = position_jitter(0), size=4)+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  geom_errorbar(aes(x_axis, ymin=mean-sd, ymax=mean+sd),
                position = position_jitter(0), width=0.1)

The plot is like this:

plot1

Since I preferred the points to have a black border, I have added color="black", and have replaced the previous "color= cat.2", by "fill=cat.2". I realize that the correct way is to use "fill" instead of "color", but the fill function does not seem to work! All the points are black:

plot2<-ggplot(example,aes(x=x_axis,y=mean , shape = cat.1)) +  theme_bw() +
  facet_wrap(~time,dir = "h")+
  geom_point(aes(fill=cat.2), position = position_jitter(0), size=4, color="black")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  geom_errorbar(aes(x_axis, ymin=mean-sd, ymax=mean+sd),
                position = position_jitter(0), width=0.1)

plot2

I have tried adding "shape=21" to the geom_point layer, and it gives the dots filled according to cat.2 and with the black border, but the plot does not show the shapes according to cat.1.

How can I create the scatterplot with shapes and fills according to two factors, and also add a black border to the points?

4

1 回答 1

0

Now with scale_shape_manual as indicated by @erc, it worked:

plot3<-ggplot(example,aes(x=x_axis,y=mean , shape = cat.1)) +  theme_bw() +
  facet_wrap(~time,dir = "h")+
  geom_jitter(aes(fill=cat.2), position = position_jitter(0), size=4, color="black")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  geom_errorbar(aes(x_axis, ymin=mean-sd, ymax=mean+sd),
                position = position_jitter(0), width=0.1) +
  scale_shape_manual( values =c("x"=24,"y"=21))

plot3

于 2021-11-24T14:19:10.403 回答