ggplot2图例未出现

时间:2018-07-12 16:26:44

标签: r ggplot2 legend

我知道这已经被问过了,我已经使用其他问题来学习如何在ggplot2中获得正确的图例。我的图例一直出现,但现在却没有出现...我在脚本中所做的唯一更改是正在读取的数据集。由于数据集的更改,绘图以相同的方式出现,但仅有一些微小的更改。 ..如果有人可以查看我的代码,看看有什么错误,因为我已经尝试了几个小时,我将不胜感激!提前致谢! 代码:

graph_theme<- theme_linedraw()+
  theme(plot.title=element_text(size=15, face="bold",vjust=0.5,hjust = 0.5),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        plot.background = element_blank(),
        legend.position = c(0.65,4),
        legend.background = element_blank(),
        plot.margin = unit(c(1.5,2,4,2), "lines"), 
        panel.background = element_blank(),
        legend.title = element_text(face="bold"),
        legend.text=element_text(size=10, face="bold"))
ggsave(file="North_South_TDS.pdf",plot=TDS_timeseriesgt, width=11.5, height=8)
TDS_north_south_plot <- ggplot(sn_data_median_TDS, aes(year,xmedian)) + 
   geom_line(aes(colour = "blue"), size=1.3) + 
   geom_line(data = north_data_median_TDS, aes(colour="red"), size = 1.3, linetype = "dotted") + 
   geom_line(data = south_data_median_TDS, aes(colour="#006400"), size = 1.3, linetype = "dashed") + 
   scale_y_continuous(expand = c(0, 0), limits = c(0, 300)) + 
   scale_x_date(date_labels = "%Y", date_breaks = "2 year") +
   ggtitle("Annual Median Total Dissolved Solids (TDS) Concentration\n (mg/L); 1997−2015") +
   xlab("Year") + 
   ylab("Median TDS Concentration (mg/L)") +
   scale_color_manual("Median TDS Concentration(mg/L);1997-2015",
                     #breaks = c("xmedian"),
                     values = c("red","blue","#006400"),
                     labels = c("South(WMA's 12-20)","Statewide","North(WMA's 1-11)"),
                     guide = guide_legend(reverse=T, 
                                          override.aes=list(linetype=c(3,1,2), 
                                          lwd=c(2,1,0.5))))+
  graph_theme
#########################################################################################################################
TDS_timeseriesgt<-add_sub(TDS_north_south_plot,label="(Data Source: NJDEP 2016 quality-assured,freshwater water quality assessment dataset)",x=0.5,y=-1.2,fontface="bold",size=11.5)
#########################################################################################################################
ggdraw(TDS_timeseriesgt)

dev.off()

使用的数据集:

south_data_median_TDS <- north_data_median_TDS <- sn_data_median_TDS <- structure(
list(year = structure(c(9862, 10227, 10592, 10957, 
11323, 11688, 12053, 12418, 12784, 13149), class = "Date"), xmedian = c(65, 
78, 103.5, 96, 78.8, 85, 76, 78, 75, 83)), .Names = c("year", 
"xmedian"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
"data.frame"))

所使用的三个数据集都具有这种结构,它们只是根据状态的不同区域进行过滤。 我得到的情节 enter image description here

0 个答案:

没有答案