【问题标题】:Add multiple line segments in a plots between many points in R在 R 中的许多点之间的图中添加多个线段
【发布时间】:2018-02-28 08:56:21
【问题描述】:

我正在使用geom_segment 在 R 中的绘图上添加线段。由于我需要添加许多线段(总共 80 条),因此我似乎无法使用“+”添加更多线。是因为 R 中的层数有限吗?无论如何,我想知道是否有任何方法可以在绘图中添加这 80 条线段。

以下是我在情节中使用的代码:

ggplot(df_var_g1,aes(Name,PrePost, fill=type)) + 
  geom_bar(colour="black", width=0.7, stat="identity",position=position_dodge(width=0.8)) + 
  scale_fill_manual(values=c("#F6D3D1","#C9CFE9")) + 
  geom_segment(aes(x = 0.8, y = 0.8525, xend = 1.2, yend = 0.8665, colour = 'b'),size=1) + 
  geom_segment(aes(x = 1.8, y = 0.8525, xend = 2.2, yend = 0.8665, colour = 'c'),size=1) + 
  geom_segment(aes(x = 2.8, y = 0.8525, xend = 3.2, yend = 0.8665, colour = 'c'),size=1) +
  geom_segment(aes(x = 3.8, y = 0.8525, xend = 4.2, yend = 0.8665, colour = 'c'),size=1) + 
  geom_segment(aes(x = 4.8, y = 0.8525, xend = 5.2, yend = 0.8665, colour = 'c'),size=1) + 
  geom_segment(aes(x = 5.8, y = 0.8525, xend = 6.2, yend = 0.8665, colour = 'c'),size=1) +
  ...(perhaps 80 geom_segment here...) + 
  scale_color_manual(values=c("#FF6666","#0000FF")) + 
  geom_errorbar(aes(ymin=SDmin, ymax=SDmax), width=.1, position=position_dodge(1)) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"), axis.ticks.length=unit(-0.15, "cm"), axis.text.x = element_text(margin=margin(15,5,10,5,"pt")), axis.text.y = element_text(margin=margin(5,8,10,5,"pt"))) + 
  coord_cartesian(ylim=c(0.7,1.0))

剧情如下:

谁能解决这个问题?或者干脆去掉geom_segment。我只想添加那 80 行...非常感谢!

【问题讨论】:

  • 您能否通过粘贴dput(df_var_g1) 的输出或df_var_g1 的子集来提供您的示例数据,您应该不需要添加80 行。还有什么决定了线条的颜色——红色还是蓝色?
  • 如果您的数据在 data.frame 中,您应该可以使用单个 geom_segment 来执行此操作

标签: r plot ggplot2


【解决方案1】:

不要使用geom_segment - 使用geom_line 与群体审美。您将需要在数据框中有一列来标识要配对的点; geom_line 可以连接该列具有相同值的行中的点

ggplot(df_var_g1, aes(Name,PrePost, fill=type)) + 
    geom_bar(colour="black", width=0.7, 
        stat="identity",position=position_dodge(width=0.8)) + 
    scale_fill_manual(values=c("#F6D3D1","#C9CFE9")) + 
    # draw lines connecting points with the same value in group_column
    geom_line(aes(group_by = group_column)) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2016-08-14
    • 2020-12-11
    • 2020-05-07
    • 2016-01-05
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多