【问题标题】:how can I make a line between each data in one subfigure如何在一个子图中的每个数据之间划一条线
【发布时间】:2018-01-23 19:12:48
【问题描述】:

我的数据是这样的

 df<- structure(list(time = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
    3L, 3L), .Label = c("5", "1", "2"), class = "factor"), key = c("boy1", 
    "boy2", "boy3", "boy1", "boy2", "boy3", "boy1", "boy2", "boy3"
    ), x = c("1", "2", "3", "1", "2", "3", "1", "2", "3"), y = c(177.72258835, 
    0, 74.438539625, 134.3410045, 48915.1, 38.302204425, 97.32286187, 
    25865.25, 28.67291878), sd = c(58.852340736, 0, 21.291893740908, 
    42.92051958201, 72521.52726, 16.309811239722, 32.40355615268, 
    38347.81965, 10.34204226254)), .Names = c("time", "key", "x", 
    "y", "sd"), class = "data.frame", row.names = c(NA, -9L))

我是这样画的

ggplot(df, aes(x, y, col = key)) +
  geom_point() +
  scale_x_discrete(labels=c("first", "second", "third"), limits = c(1, 2,3)) +
  facet_grid(time ~ .) +
  geom_errorbar(aes(ymin = y-sd, ymax = y+sd)) +
  facet_grid(time ~ ., scale = "free_y")

我想将每次的三个数据按行连接在一起

我用过这个http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/ 当我使用geom_line() 时出现错误

ggplot(df, aes(x, y, col = key)) +
  geom_line() +
  geom_point() +
  scale_x_discrete(labels=c("first", "second", "third"), limits = c(1, 2,3)) +
  facet_grid(time ~ .) +
  geom_errorbar(aes(ymin = y-sd, ymax = y+sd)) +
  facet_grid(time ~ ., scale = "free_y") 

错误是这样的

geom_path: Each group consists of only one observation. Do you need to adjust the
group aesthetic?
geom_path: Each group consists of only one observation. Do you need to adjust the
group aesthetic?
geom_path: Each group consists of only one observation. Do you need to adjust the
group aesthetic?

【问题讨论】:

    标签: r


    【解决方案1】:

    您遇到了麻烦,因为您在全局范围内映射color,并且不同的颜色意味着不同的线条。因此,在geom_line() 层中,我们选择一种颜色并明确设置group(如消息所建议的那样),它告诉ggplot2 应该连接哪些点。在这种情况下,我们希望所有点都连接(在一个构面内),因此我们只需将 group 设置为单个值。

    ggplot(df, aes(x, y, col = key)) +
      geom_point() +
      geom_line(color = "black", group = 1) +
      scale_x_discrete(labels=c("first", "second", "third"), limits = c(1, 2,3)) +
      facet_grid(time ~ .) +
      geom_errorbar(aes(ymin = y-sd, ymax = y+sd)) +
      facet_grid(time ~ ., scale = "free_y")
    

    【讨论】:

    • 我喜欢它,但它不允许我接受 6 分钟
    • 很抱歉这个问题,但您认为您可以帮我转换数据,使我可以在上面的 3 个图中将所有红色、所有绿色和所有蓝色绘制在一起吗?
    • 在这种情况下,你想在 x 轴上做什么?鉴于规模差异很大,我认为它不会很好用。如果您无法弄清楚,您可能应该提出一个新问题。
    猜你喜欢
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2016-03-22
    相关资源
    最近更新 更多