【问题标题】:R: connect points on a graph (ggplot2)R:连接图上的点(ggplot2)
【发布时间】:2020-12-03 03:49:43
【问题描述】:

假设我有以下形式的数据:

library(ggplot2)

Data <- data.frame(
    
    "ID" = c("ABC111", "ABC111", "ABC111", "ABC111", "ABC112", "ABC112", "ABC112", "ABC113", "ABC113", "ABC114", "ABC115"),
"color" = c("red", "red", "red", "red", "blue", "blue", "blue", "green", "green", "black", "yellow"),
    "start_date" = c("2005/01/01", "2006/01/01", "2007/01/01", "2008/01/01", "2009/01/01", "2010/01/01", "2011/01/01", "2012/01/01", "2013/01/01", "2014/01/01", "2015/01/01"),
    "end_date" = c("2005/09/01", "2006/06/01", "2007/04/01", "2008/05/07", "2009/06/01", "2010/10/01", "2011/12/12", "2013/05/01", "2013/06/08", "2015/01/01", "2016/08/09")
)

Data$ID = as.factor(Data$ID)
Data$color = as.factor(Data$color)

现在我要做的是为每一行绘制 start_date 和 end_date ...然后用直线连接它们。我相信这可以通过 ggplot2 中的 geom_line() 来完成。

我想要这样的东西:

我尝试使用以下代码:

q <- qplot(start_date, end_date, data=Data)
q <- q + geom_line(aes(group = ID))
q

但图表看起来与我的预期完全不同。

谁能告诉我我做错了什么?

谢谢

【问题讨论】:

  • 在您显示的图中,y 轴值是多少?
  • 你好!该图中的 y 轴是没有意义的……如果线条可以按照它们出现的顺序“放样”就好了。这可能吗?谢谢

标签: r graph data-visualization line point


【解决方案1】:

以下内容对您有用吗?

ggplot(data = Data, aes(start_date, end_date, color = ID))+
  geom_line(aes(group = ID))+
  geom_point()

或者geom_segment

# Adding x and y coordinates for geom_segment
Data$x <- as.character(as.Date(Data$start_date) + (as.Date(Data$end_date) - as.Date(Data$start_date)))
Data$y <- 1:nrow(Data)

ggplot(data = Data, aes(x, y, colour = ID))+
  geom_segment(aes(xend = start_date, yend = end_date))

【讨论】:

  • 你好!谢谢您的回复。不完全是-我希望所有线条都是水平的。每条线仅由两个点组成(从该行开始)。这可能吗?
  • 那么,您可能没有正确的数据。您可能正在寻找类似geom_segment 的内容,在这种情况下,您需要提供有关xy 轴的更多信息。检查我的编辑是否对您有帮助。
  • 感谢李曼的所有帮助!下面的用户(Jonathan V. Solórzano)提供了我正在寻找的答案
【解决方案2】:

这是使用tidyverse 包的解决方案。我使用原始数据中每一行的数量作为图中的 y 轴值。由于这些值没有意义,我从图中删除了 y 轴标题、标签和刻度。

library(tidyverse)

Data %>%
  # Number each row in its order of appearance, 
  # save this numbers in a new column named order
  rowid_to_column("order") %>%
  # Change data from wide to long format
  pivot_longer(cols = c(start_date, end_date),
               names_to = "date_type",
               values_to = "date") %>%
  # Ggplot, use date as x, order as y, ID as col and order as group
  ggplot(aes(x = date, 
             y = order,  
             col = ID, 
             group = order)) +
  # Draw points
  geom_point()+
  # Draw lines
  geom_line() +
  # Maybe you want to remove the y axis title, text and ticks
  theme(axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        # I added a vertical format to the x axis labels 
        # it might easier to read this way
        axis.text.x = element_text(angle = 90, vjust = 0.5))

【讨论】:

  • 这太完美了!是否可以仅使用“dplyr”和“reshape2”库来做到这一点?
  • 你可以使用mutate(order = row_number())之类的东西来代替rowid_to_column,而不是pivot_longer使用melt
  • 感谢您的回复!我尝试了这些更改,但出现了一些错误……我发布了一个新问题,其中包含修改后的代码。感谢您的所有帮助
  • 这是我试过的代码:stackoverflow.com/questions/65131612/… 谢谢你的一切
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
相关资源
最近更新 更多