xx <- seq(1:12)
yy_1 <- 2.4310522 / (1 + exp(2.9500379 - xx) / 0.7346499)
yy_2 <- 3.0789415 / (1 + exp(3.1014717 - xx) / 0.7414208)
log_data_1 <- cbind.data.frame(xx, yy_1, yy_2)
library(ggplot2)
ggplot() +
  geom_line(data = log_data_1, aes(x = xx, y = yy_1), colour = "green", linetype = 2, size = 1.1) +
  geom_point(data = log_data_1, aes(x = xx, y = yy_1), size = 1.4) +
  geom_line(data = log_data_1, aes(x = xx, y = yy_2), colour = "red", size = 1.1) +
  geom_point(data = log_data_1, aes(x = xx, y = yy_2), size = 1.4) +
  scale_x_continuous(name = "Age ( month )", limits = c(1, 12), breaks = seq(1, 12, 1)) +
  scale_y_continuous(name = " Wet weight ( g )", limits = c(0, 3.5), breaks = seq(0, 3.5, 0.5))

Rplot

请教下大家,为什么这个画图没有生成图例呢?排错了好久,没找到原因

    ffrcjim 因为你这里并没有用到图例,给点和线的上色,并伴随图例,需要改动数据集。可以参考示例

    P.S. 这次帮你修改了发帖内容,麻烦下次格式化代码,提供完整可复现的代码,如果不知道怎么操作,可以看下左上角的新手须知。根据经验,一般内容越清晰越快得到响应!

      直接原因是 ggplot2 认为没有必要加图例。本质原因在于以 ggplot2 的思路,你的数据中要把颜色单独定义为一列,然后颜色根据这一列来定才会自动生成图例,在画图图层里改颜色是没用的。感觉你是看了个用 base R 思路教 ggplot2 的教程,当然 ggplot2 也支持完全自定义的图例,不过要是走那条路就真不如直接上 base R 了。

      下面是 ggplot2 的解法:

      xx <- seq(1:12)
      yy_1 <- 2.4310522 / (1 + exp(2.9500379 - xx) / 0.7346499)
      yy_2 <- 3.0789415 / (1 + exp(3.1014717 - xx) / 0.7414208)
      log_data_1 <- cbind.data.frame(x=rep(xx,2), y=c(yy_1, yy_2),color=c(rep('group1',length(xx)),rep('group2',length(xx))))
      library(ggplot2)
      ggplot(data = log_data_1, aes(x = x, y = y,color=color)) +
        geom_line( linetype = 2, size = 1.1) +
        geom_point( size = 1.4) +
        scale_x_continuous(name = "Age ( month )", limits = c(1, 12), breaks = seq(1, 12, 1)) +
        scale_y_continuous(name = " Wet weight ( g )", limits = c(0, 3.5), breaks = seq(0, 3.5, 0.5))

        yufree 正如您所说的。plot()比较熟悉。想搞点花哨的ggplot2做的图,毕竟看脸的时代 😄
        yy_1和yy_2本来就是两个性别的数据。我尝试过新建一个gender的列,可能代码写的不对。我再学习下您的写法。

        yufree 出于节省版面费的角度,只能用实线和虚线表示不同组。用颜色区分的图,一页好多钱。我再改改 😀 先谢谢您了 😄

        6 天 后
        2 个月 后

        你这里只是单独的可视化了两条线,并没有为两条线设置图例,在ggplot2包中有相应的函数,可以额外的添加图例。例如:scale_colour_manual()等函数。