不使用一个主数据框更改ggplot2图例顺序?

时间:2016-02-03 12:16:25

标签: r

虽然看起来很多次都会问这个问题,但显然它没有(或者至少我找不到答案)。我有几个不同的数据帧,我从中创建一个ggplot,每个数据帧提供点或行。我想重新排序图例,以便(在下面的示例中)“点”高于“lm” - 但命令“+ guides(guide = guide_legend(reverse = TRUE))”似乎不起作用。

任何想法都非常感激。

非常感谢。

编码如下:

`library("ggplot2")

nnn <- "Name"
xx<-c(1:27)
newdata<-c(2732,    2900,   3032,   2811,   2657,   2530,   2521,   2615,   2807,   3022,   3125,   3200,   3308,   3739,   4601,   4236,   3655,   3781,   4119,   4437,   4615,   5009,   5445,   5641,   5623,   5740,   6072) 
Data111yyxx<-data.frame(xx, newdata)
names(Data111yyxx) <- c("xx", "newdata")

Datalm <- lm(newdata ~ xx, data = Data111yyxx)

windows()
p1 <- ggplot() + ggtitle("mag") + theme(plot.title = element_text(size = 10)) + coord_cartesian(xlim = c(0, 30), ylim = c(0, 8000)) + scale_x_continuous(breaks = c(0, 10, 20, 30), labels = c("0", "10", "20", "30"), expand = c(0, 0)) + scale_y_continuous(breaks = c(0, 4000, 8000), labels = c("0", "4000", "8000"), expand = c(0, 0)) + ylab("Yy") + xlab("Xx") + geom_point(data = Data111yyxx, aes(x = Data111yyxx[ , 1], y = Data111yyxx[ , 2], colour = "points", shape = "points", linetype = "points")) 
xgridA <- data.frame(xx = c(seq(min(xx), max(xx), length.out = 100)))
fhatA <- as.vector(predict(Datalm, newdata = xgridA))
dataframexgridA <- data.frame(xgridA, fhatA)
colnames(dataframexgridA) <- c("xxforfit", "gomforfit")
xgridAA <- as.vector(unlist(xgridA)) 
q2 <- p1 + geom_line(data = dataframexgridA, aes(x = xgridAA, y = fhatA, colour = "lm", shape = "lm", linetype = "lm")) + scale_colour_manual( ' ', values = c("points" = "black", "lm" = "black")) + scale_shape_manual( ' ', values = c("points" = 16, "lm" = NA)) + scale_linetype_manual( ' ', values = c("points" = 0, "lm" = 1)) + guides(guide = guide_legend(reverse = TRUE)) 
q2

`

1 个答案:

答案 0 :(得分:1)

同意亚历山大所说的,你的代码需要一些关注。但这似乎根据你已经写过的内容给你你想要的东西:

library("ggplot2")

nnn <- "Name"
xx<-c(1:27)
newdata<-c(2732,    2900,   3032,   2811,   2657,   2530,   2521,   2615,   2807,   3022,   3125,   3200,   3308,   3739,   4601,   4236,   3655,   3781,   4119,   4437,   4615,   5009,   5445,   5641,   5623,   5740,   6072) 
Data111yyxx<-data.frame(xx, newdata)
names(Data111yyxx) <- c("xx", "newdata")

Datalm <- lm(newdata ~ xx, data = Data111yyxx)
xgridA <- data.frame(xx = c(seq(min(xx), max(xx), length.out = 100)))
fhatA <- as.vector(predict(Datalm, newdata = xgridA))
dataframexgridA <- data.frame(xgridA, fhatA)
colnames(dataframexgridA) <- c("xxforfit", "gomforfit")
xgridAA <- as.vector(unlist(xgridA)) 

windows()


ggplot() + 
      ggtitle("mag") + 
      theme(plot.title = element_text(size = 10)) + 
      coord_cartesian(xlim = c(0, 30), ylim = c(0, 8000)) + 
      scale_x_continuous(breaks = c(0, 10, 20, 30), labels = c("0", "10", "20", "30"), expand = c(0, 0)) +
      scale_y_continuous(breaks = c(0, 4000, 8000), labels = c("0", "4000", "8000"), expand = c(0, 0)) + 
      ylab("Yy") + 
      xlab("Xx") +
      geom_point(data = Data111yyxx, aes(x = Data111yyxx[ , 1], y = Data111yyxx[ , 2], colour = "points", shape = "points", linetype = "points")) + 
      geom_line(data = dataframexgridA, aes(x = xgridAA, y = fhatA, colour = "lm", shape = "lm", linetype = "lm")) + 
      scale_colour_manual( ' ', values = c("points" = "black", "lm" = "black"), guide = guide_legend(reverse=TRUE)) + 
      scale_shape_manual( ' ', values = c("points" = 16, "lm" = NA), guide = guide_legend(reverse=TRUE)) + 
      scale_linetype_manual( ' ', values = c("points" = 0, "lm" = 1), guide = guide_legend(reverse=TRUE))

enter image description here