本文主要是介绍R语言ggplot2 legend name/横坐标名字更改,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ggplot 就像PS一样,是一层一层搭建起来的,层和层之间用+连接 ,直接写成一个语句
关于aes,翻译过来叫美学特征,可以设置图形的横坐标,纵坐标,以及点的颜色,比如aes(colour = "green")
第一层,大框架,也就是第一层(坐标轴,数据)ggplot(data = ,aes(x= ,y = )) +
第二层,描点geom_point()+(当然也可以给geom_point设置颜色,比如 geom_point(color = "green"),当然也有其他属性
注:画完第一层后是没有数据的,只有一个框架,在我这连框架也没有,所以只有画完第二层才会有图形
第三层 ,stat_统计特性,比如stat_sum(****),****和point里是属性可以是相同的,也就是说有一个就行比如 stat_sum(color = "darkred",geom = "point")
当然还可以继续加
例子:
library(ggplot2)
p<- ggplot(mtcars, aes(wt, mpg))
p+geom_point(color = "green")
以上为例子图片.
第三层
setEPS()#postscript(paste("./alg_prediction/maxsat_parameters_prediction/", names(alldata.split)[alg],"_",y.plot.label[para], ".eps", sep = ""))postscript("Hello.eps")p <- ggplot(data = temp.alldata) #colour 用来映射ggplot2的legend.p <- p + geom_point(mytrain, mapping = aes(instances, mytrain[, para+1], colour = "Train Data"), cex = 2)p <- p + geom_point(mytest, mapping =aes(instances, mytest[, para+1], colour = "Test Data"), cex = 2 )p <- p + geom_point(temp.pre, mapping =aes(size, temp.pre[, para+1]), col = "red", cex = 2)p <- p + geom_point(temp.model, mapping =aes(size, temp.model[, para+1]), col = "green", cex = 2) p <- p + geom_line(temp.model, mapping =aes(size%>% as.numeric, temp.model[, para+1], colour = "Average"), lwd = 1)p <- p + geom_line(temp.pre, mapping =aes(size%>% as.numeric, temp.pre[, para+1], colour = "Prediction"), lwd = 1)#手动设置legend 的颜色p <- p + scale_color_manual(values = c( "Test Data" = "Blue", "Train Data" = "Black","Prediction" = "Red", "Average" = "Green") )#设置标题p <- p + guides( colour = guide_legend( title = ""))#横纵坐标p <- p + labs(x = "Instance Scale",y = y.label)
p<- p + scale_x_discrete(names(temp.alldata$instances %>% unique), labels = c(1:10)) #family 为字体 需要加载包library(extrafont) p <- p + theme( aspect.ratio = 1, #grid 线型linetype, 颜色 colour. 有大小grid 之分 区别自己试 panel.grid.major = element_line(linetype = 3 , colour = "lightgrey" ), panel.grid.minor = element_blank(), #主题 ggplot2 默认为灰色 panel.background = element_rect(fill = "white", colour = "black"), #横坐标大小size, 角度angle, 字体family axis.text.x = element_text(size = 15, angle = 90, family = "Arial"), axis.text.y = element_text(size = 15, hjust = 1, family = "Arial"), #大小size,字体 family axis.title.x = element_text(size = 15, family = "Arial"), axis.title.y = element_text(size = 15, family = "Arial"), #legend 大小size, 字体family legend.text = element_text(size = 15, family = "Arial") ) plot(p) dev.off()#更改横坐标!!!将横坐标刻度字符串转化为指定字符串labels, 也可以是其他自定义字符串 names为原来的横坐标刻度
纪念画图画了4个小时
legend颜色设置首先在每个图层aes里用colour系统自动映射, 最后使用
根据名字来自己设置颜色scale_color_manual
有了以上说明,就可以看懂下面链接了.下面链接详细,特别好!https://www.zhihu.com/question/24779017/answer/38750383
这篇关于R语言ggplot2 legend name/横坐标名字更改的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!