如何使用ggplot在不同列上连接两个点的线。

huangapple go评论81阅读模式
英文:

How to connect two points with a line from different columns on ggplot

问题

我有一个数据集,包含三列:年份、最小值和最大值。我想要为每年的最小值和最大值之间绘制一条线。

这个图表展示了一个geom_point图,显示了每年的最小值和最大值,但我不知道如何连接这两个值,因为它们来自不同的列。

这是ggplot的代码:

# 绘制年份(y)和最大值与最小值的点(x)
ggplot(phen2, aes(y=Year)) + 
  geom_point(aes(x = max, color = "max")) + 
  geom_point(aes(x = min, color = "min"))
英文:

I have a dataset that has a three columns: a year, a minimum value, and a maximum value. I want to plot a line between the min and max value for each year.

This plot shows a geom_point plot with the min and max values from each year but I do not know how to connect the two values with a line since they come from different columns.
如何使用ggplot在不同列上连接两个点的线。

Here is the code for the ggplot:

#plot year (y) and max and min points (x)
ggplot(phen2, aes(y=Year)) + 
  geom_point(aes(x = max, color = "max")) + 
  geom_point(aes(x = min, color = "min"))

答案1

得分: 2

如果您只需要将一列的数据连接到另一列的数据,您可以使用geom_segment来指定每个线段的起始点和终点:

library(ggplot2)
ggplot(tail(msleep), aes(y = name)) + 
  geom_segment(aes(x = awake, xend = bodywt, yend = name)) +
  geom_point(aes(x = awake, color = "awake")) +
  geom_point(aes(x = bodywt, color = "bodywt"))

通常使用ggplot2的更典型方式是将数据转换为长格式,每个观察值占据一行。较长的数据通常在ggplot2中具有更简单的绘图代码,并且更容易处理图例。可以使用dplyrtidyr这两个包中包含的函数来重塑数据,这两个包都包含在library(tidyverse)中。

library(dplyr)
library(tidyr)

# 数据准备
tail(msleep) %>%
  select(name, awake, bodywt) %>%
  pivot_longer(-name, names_to = "stat", values_to = "val") %>%

# 稍微简化的绘图并将颜色映射到源列
  ggplot(aes(x = val, y = name)) +
  geom_line() +
  geom_point(aes(color = stat))

如何使用ggplot在不同列上连接两个点的线。

如何使用ggplot在不同列上连接两个点的线。

英文:

If you just need to connect the data from one column to the data from another column, you can use geom_segment to specify the beginning and end of each segment:

library(ggplot2)
ggplot(tail(msleep), aes(y = name)) + 
  geom_segment(aes(x = awake, xend = bodywt, yend = name)) +
  geom_point(aes(x = awake, color = "awake")) +
  geom_point(aes(x = bodywt, color = "bodywt")) 

如何使用ggplot在不同列上连接两个点的线。

The more typical way to work with ggplot2 is to convert your data into longer format, with each observation in its own row. Longer data often has simpler plotting code with ggplot2, and it often takes care of legends more easily. Reshaping the data can be done with dplyr and tidyr, two other of the packages included in library(tidyverse).

library(dplyr); library(tidyr)

# data prep
tail(msleep) %>%
  select(name, awake, bodywt) %>%
  pivot_longer(-name, names_to = "stat", values_to = "val") %>%

# slightly simpler plotting & mapping color to source column
  ggplot(aes(x = val, y = name)) +
  geom_line() +
  geom_point(aes(color = stat)) 

如何使用ggplot在不同列上连接两个点的线。

huangapple
  • 本文由 发表于 2020年1月4日 01:40:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定