本文介绍了在ggplot2中用它们的渐变标记线条并改变图例特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在研究1954 - 2000年每月最高气温的变化,使用以下数据: http://pastebin.com/37zUkaA4 我决定只绘制每个月的abline清晰的图表。我的代码如下: OxTemp $ Month p p + geom_smooth(method ='lm' ,size = 1,se = F) 这给了我下面的图: 我想知道是否有办法: a)更改Month图例中的颜色以匹配季节传说 b)让图例更宽一些,以便线型更加清晰可见 c)添加 非常感谢! 解决方案 OxTemp< - read.table(http://pastebin.com/raw.php?i=37zUkaA4, header = TRUE,stringsAsFactors = FALSE) library(ggplot2) OxTemp $ Month levels = c(Jan,Feb ,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec),ordered = TRUE) b OxTemp $ Season levels = c(Spring,Summer,Autumn,Winter),ordered = TRUE) library(plyr) slopedat y = max(预测(lm(MaxT〜Year,data = df))))) p < - ggplot(x = Year,y = MaxT))+ geom_smooth(aes(group = Month,color = Season,linetype = Month),method ='lm',size = 1, se = F)+ scale_colour_manual(values = c(Winter= 4,Spring= 1,夏季= 2,秋季= 3))+ geom_text(data = slopedat,aes(x = 2005,y = y,label = paste0(slope =,slope))+ scale_x_continuous(limits = c(1950,2010))+ guides(linetype = guide_legend(override.aes = list(color = c(Jan= 4,Feb= 4,Mar= 1 ,Apr= 1,May= 1,Jun= 2,Jul= 2,Aug= 2,Sep= 3, Oct= 3,Nov= 3,Dec= 4)),keywidth = 5)) print(p) I am looking at the change in maximum temperature per month, from 1954-2000 using data thus:http://pastebin.com/37zUkaA4 I have decided to only plot the abline for each month on the graph for clarity. My code is as follows:OxTemp$Month <- factor(OxTemp$Month, levels=c("January", "February", "March","April", "May", "June", "August", "September", "October", "November", "December"), ordered=TRUE)p<-ggplot(OxTemp, aes(x=Year, y=MaxT, group=Month, colour=Season, linetype=Month))p+geom_smooth(method = 'lm',size = 1, se = F)Which gives me the following plot:I was wondering if there was a way to:a) Change the colours in the "Month" legend to match the colours in the "Season" legendb) Make the legends a little wider so that the linetypes are more visiblec) Add a label of each line's gradient to the plot, such that to the right handside of each line the slope value is displayedMany thanks! 解决方案 OxTemp <- read.table("http://pastebin.com/raw.php?i=37zUkaA4",header=TRUE,stringsAsFactors=FALSE)library(ggplot2)OxTemp$Month <- factor(OxTemp$Month, levels=c("Jan", "Feb", "Mar","Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), ordered=TRUE)OxTemp$Season <- factor(OxTemp$Season, levels=c("Spring", "Summer", "Autumn", "Winter"), ordered=TRUE)library(plyr)slopedat <- ddply(OxTemp,.(Month),function(df) data.frame(slope=format(signif(coef(lm(MaxT~Year,data=df))[2],2),scientific=-2), y=max(predict(lm(MaxT~Year,data=df)))))p <- ggplot(OxTemp, aes(x=Year, y=MaxT)) + geom_smooth(aes(group=Month, colour=Season, linetype=Month),method = 'lm',size = 1, se = F) + scale_colour_manual(values=c("Winter"= 4, "Spring" = 1, "Summer" = 2,"Autumn" = 3)) + geom_text(data=slopedat,aes(x=2005,y=y,label=paste0("slope = ",slope))) + scale_x_continuous(limits=c(1950, 2010)) + guides(linetype=guide_legend(override.aes=list(colour=c("Jan"= 4, "Feb" = 4, "Mar" = 1, "Apr" = 1, "May" = 1, "Jun" = 2, "Jul" = 2, "Aug" = 2, "Sep" = 3, "Oct" = 3, "Nov" = 3, "Dec" = 4)),keywidth=5))print(p) 这篇关于在ggplot2中用它们的渐变标记线条并改变图例特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 09:04