不将几何点与图例中的几何线合并

问题描述 投票:0回答:1

我正在尝试只获得一个形状与图例颜色相同但只有黑色的图例:

type1    <-c("tmax","tmax","tmax","tmin","tmin","tmin","tmax","tmax","tmax","tmin","tmin","tmin") 
station1 <-c("Anda","Anda","Anda","Anda","Anda","Anda","Mach","Mach","Mach","Mach","Mach","Mach") 
date1    <-c(2001,2002,2003,2001,2002,2003,2002,2003,2004,2002,2003,2004) 
meanTemp1<-c(15,16,15.5,5,7,8,13,14,12,9,9,7)

data11 <- data.frame(type1,station1,date1,meanTemp1)

plot1<- ggplot(data11, aes(x=date1, y=meanTemp1,group = station1,colour=station1,shape=station1)) +
        geom_line () + guides(colour=FALSE)+
        geom_point() + 
        xlab("year") + ylab("°C") + 
        labs(shape = "Station")+
        facet_wrap(~type1,scales = "free")+
        theme(axis.text.x = element_text(angle = 60,hjust = 1))
plot1

“输出图”

如何使图例填充为与图形相同的颜色而不是“黑色”?

r ggplot2 legend
1个回答
0
投票

当您在shape中重命名labs图例时,还需要使用相同的名称重命名colour图例以使其合并。

代替使用guides(colour = FALSE),您可以传入geom_line,参数show.legend = FALSE

plot1<- ggplot(data11, aes(x=date1, y=meanTemp1, group = station1,
                           colour=station1,
                           shape=station1)) +
  geom_line (show.legend = FALSE) +
  geom_point() + 
  xlab("year") + ylab("°C") + 
  labs(shape = "Station", colour = "Station")+
  facet_wrap(~type1,scales = "free")+
  theme(axis.text.x = element_text(angle = 60,hjust = 1))
plot1

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.