0
votes

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. 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.

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
It is better to create a reproducible example stackoverflow.com/help/minimal-reproducible-exampleNareman Darwish

1 Answers

2
votes

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")) 

enter image description here

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)) 

enter image description here