为什么图例中的形状与ggplot2中的图形中的形状不同?

时间:2016-07-31 15:51:41

标签: r ggplot2

我正在使用ggplot2版本2.1.0。以下是我创建的情节的截图:

enter image description here

以下是代码:

ggplot()+
  geom_point(data = BSX_SDX_5 %>% filter(CC1_BX1>0, sp_BX>1,
                                         !(CC1_BX1 %in% c(Inf, -Inf)), 
                                         dV_BX>=-1 & dV_BX<=0,
                                         BX1<=40) ,
             aes(y = dV_BX, x = BX1, shape = "Observed"), alpha=0.4)+
  geom_point(data = modi_CC1 %>% filter(CC1>0, sp_BX>1,
                                        !(CC1 %in% c(Inf, -Inf)),dV<=0,
                                        min_spacing<=76) ,
             aes(y = dV, x = min_spacing, shape = "SIMDATA1"), size=3)+
  geom_point(data = def_CC1 %>% filter(CC1>0, sp_BX>1,
                                        !(CC1 %in% c(Inf, -Inf)),dV<=0,
                                       min_spacing<=76) ,
             aes(y = dV, x = min_spacing, shape = "SIMDATA2"), size=3) + 
  scale_y_reverse()+
  scale_shape_manual(values = c(17, 0, 16))+
  theme_bw()+
  theme(axis.title.x = element_text(face="bold", size=20, vjust=0.5),
        axis.title.y = element_text(face="bold", size=20, vjust=0.5),
        axis.text  = element_text(size=20),
        legend.title=element_blank(),
        legend.text = element_text(size = 15, face = "bold"),
        strip.text.x = element_text(size = 20, face = "bold"),
        legend.position = c(0.9,0.8),
        legend.key.height = unit(1, "cm"),
        legend.key.width = unit(1.5, "cm")) 

我没有共享数据,因为似乎问题与数据无关。如何确保图例中的形状与图中的形状正确对应?

1 个答案:

答案 0 :(得分:2)

由于三个geom层中明确的 size 规范,第一个没有任何规格,第二个和第三个geom的大小设置为3。

这说明了我的观点:

set.seed(1)
n = 100
shapes = c("Observed","SIMDATA1","SIMDATA2")
df   = data.frame(x=runif(n),y=runif(n),shape=sample(shapes,n,replace=T))
base = ggplot(df,aes(x,y,shape=shape)) + 
  scale_y_reverse()+
  scale_shape_manual(values = c(17, 0, 16))+
  theme_bw()+
  theme(axis.title.x = element_text(face="bold", size=20, vjust=0.5),
        axis.title.y = element_text(face="bold", size=20, vjust=0.5),
        axis.text  = element_text(size=20),
        legend.title=element_blank(),
        legend.text = element_text(size = 15, face = "bold"),
        strip.text.x = element_text(size = 20, face = "bold"),
        legend.position = c(0.9,0.8),
        legend.key.height = unit(1, "cm"),
        legend.key.width = unit(1.5, "cm")) 

#BAD, AS PER YOUR PROBLEM
base + 
  geom_point(data=df[1:(n/2),])+
  geom_point(data=df[(n/2):n,],size=3) 

#GOOD
base + 
  geom_point(data=df[1:(n/2),])+
  geom_point(data=df[(n/2):n,]) 

#GOOD
base + 
  geom_point(data=df[1:(n/2),],size=3)+
  geom_point(data=df[(n/2):n,],size=3)

如果您想拥有不同的尺寸,可以将变量映射到尺寸,然后使用scale_size_manual,如下所示:

base = ggplot(df,aes(x,y,shape=shape,size=shape)) + 
  scale_y_reverse()+
  scale_shape_manual(values = c(17, 0, 16)) +
  scale_size_manual(values = c(3,6,6)) +
  theme_bw()+
  theme(axis.title.x = element_text(face="bold", size=20, vjust=0.5),
        axis.title.y = element_text(face="bold", size=20, vjust=0.5),
        axis.text  = element_text(size=20),
        legend.title=element_blank(),
        legend.text = element_text(size = 15, face = "bold"),
        strip.text.x = element_text(size = 20, face = "bold"),
        legend.position = c(0.9,0.8),
        legend.key.height = unit(1, "cm"),
        legend.key.width = unit(1.5, "cm")) 

base + 
  geom_point(data=df[1:(n/2),])+
  geom_point(data=df[(n/2):n,])