在ggplot中省略图例的NA值

时间:2019-05-08 15:57:14

标签: r ggplot2 scatterpie

TAI

structure(list(year = c("2018", "2018", "2018", "2018", "2018", 
"2018", "2018", "2018", "2018", "2018", "2018", "2018"), month = c(1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), Avdischarge = c(772.3, 370.3375, 
272.85, 195.5875, 100.575, 104.0125, 44.7125, 31.725, 29.6875, 
27.025, 34.30625, 250.825), IndustrialCompound = c(NA, NA, NA, 
NA, 0, 0, 0, 0, 0, 0, 0, 0), Pharmaceutical = c(NA, NA, NA, NA, 
0, 0, 0, 0, 0, 0, 0, 0), Pesticide = c(NA, NA, NA, NA, 1, 1, 
1, 1, 1, 1, 1, 1), TotalOvershootings = c(0, 0, 0, 0, 0.05, 0.1, 
0.1, 0.1, 0.1, 0.1, 0.1, 0.1)), row.names = 37:48, class = "data.frame")

我和一些散布了

piepertai<- ggplot(TAI, aes(x=month)) +
  geom_line(aes(y= as.factor(round(Avdischarge,1)), group=1, color="Discharge"), 
            size=1.5, 
            alpha=0.8) + 
  scale_x_continuous(breaks=TAI$month)+
  scale_color_manual(name= "", values=c("Discharge" = "navy"))+
  geom_scatterpie(aes(x=month, y=2, group=type, r = TotalOvershootings*5), 
                  cols= c("IndustrialCompound", "Pharmaceutical", "Pesticide"),
                  alpha= 0.7,
                  color=NA,
                  data= TAI)+
  labs(y="Average discharge (m3/s)", x= "Month", 
       title= "Discharge and Compounds percentage of exceedance per month (2018)",
       subtitle = "Tailfer")+
  theme(axis.title.x = element_text(size=13),
        axis.text.x  = element_text(size=13),
        axis.title.y = element_text(size=13),
        axis.text.y = element_text(size = 13),
        plot.title = element_text(size = 15), 
        plot.subtitle = element_text(size = 14, face = "italic"),
        legend.text=element_text(size=13))+
  geom_scatterpie_legend(TAI$TotalOvershootings*5, x= 9, y= 10, n= 5, 
                         labeller=function(x) paste0(round_any(100*x/1.5,5),"%"))

geom_scatterpie_legend之前,一切正常运行,但是在那之后,它没有运行,我认为是因为我拥有NA值。如何正确添加图例? 我尝试了TAI<-na.omit (TAI) 但是,这使我的情节仅从第5个月开始。我想显示所有月份。

1 个答案:

答案 0 :(得分:0)

当我尝试执行它时,您的代码对我来说是挂的。我删除了geom_scatterpie_legend调用中将TAI引用到新列表的部分:

labels <- (unique(TAI$TotalOvershootings)*10)/1.5

然后我更新了ggplot调用,以使用此新列表作为radius参数。您可以调整上面的标签列表以使用不同的半径大小播放。

ggplot(TAI, aes(x=month)) +
  geom_line(aes(y= as.factor(round(Avdischarge,1)), group=1, color="Discharge"), 
            size=1.5, 
            alpha=0.8) + 
  scale_x_continuous(breaks=TAI$month)+
  scale_color_manual(name= "", values=c("Discharge" = "navy"))+
  geom_scatterpie(aes(x=month, y=2, group=type, r = TotalOvershootings*5), 
                  cols= c("IndustrialCompound", "Pharmaceutical", "Pesticide"),
                  alpha= 0.7,
                  color=NA,
                  data= TAI)+
  labs(y="Average discharge (m3/s)", x= "Month", 
       title= "Discharge and Compounds percentage of exceedance per month (2018)",
       subtitle = "Tailfer")+
  theme(axis.title.x = element_text(size=13),
        axis.text.x  = element_text(size=13),
        axis.title.y = element_text(size=13),
        axis.text.y = element_text(size = 13),
        plot.title = element_text(size = 15), 
        plot.subtitle = element_text(size = 14, face = "italic"),
        legend.text=element_text(size=13)) +
  geom_scatterpie_legend(radius = labels, x= 9, y= 10, n=3,
                         labeller=function(x) paste0(round_any(100*x/1.5,5),"%"))

它正在为我执行。

enter image description here

这是您期望的输出吗?