Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting one variable both line-only and points-only, depending on value

Tags:

r

ggplot2

I would like to use ggplot2 to plot 3 time series in the same graph. I'd like to plot the first 2 series as solid lines without points. And I would like to plot the third series with points only and no line. How can I do that?

library(ggplot2)
library(reshape2)

d1 <- c(1, 2, 3, 2, 1, 2, 3, 4, 5, 6, 5, 4, 3, 1)
d2 <- c(0, 2, 4, 5, 4, 3, 2, 4, 6, 7, 6, 5, 3, 1)
d3 <- c(0, 1, 2, 4, 4, 2, 1, 3, 4, 7, 8, 3, 5, 0)

ts1 <- ts(d1, c(2015, 01), c(2016, 03), frequency = 12)
ts2 <- ts(d2, c(2015, 01), c(2016, 03), frequency = 12)
ts3 <- ts(d3, c(2015, 01), c(2016, 03), frequency = 12)

# prepare data for ggplot
dat <- ts.union(ts1, ts2, ts3)
dat <- melt(dat, id.vars = "x")

# add dates
dates <- seq(as.Date("2015-01-01"), as.Date("2016-03-01"), by = "months")
dat$Date <- dates

p <- ggplot(dat, aes(x = Date, y = value, col = Var2)) +
  geom_line(aes(linetype = Var2), size = 1) +
  geom_point(aes(shape = Var2), size = 2) +
  scale_linetype_manual(values = c(1, 1, 1)) +
  scale_shape_manual(values = c(0, 1, 2))
print(p)
like image 398
F. Brazil Avatar asked Dec 22 '19 06:12

F. Brazil


People also ask

What is a line plot good for?

Line graphs are used to track changes over short and long periods of time. When smaller changes exist, line graphs are better to use than bar graphs. Line graphs can also be used to compare changes over the same period of time for more than one group.

What do line plots mean?

What is a Line Plot? A Line plot can be defined as a graph that displays data as points or check marks above a number line, showing the frequency of each value. Here, for instance, the line plot shows the number of ribbons of each length. Fun Facts. – A line plot is often confused with a line graph.

How to plot multiple line plots in Matplotlib?

To plot multiple line plots in Matplotlib, you simply repeatedly call the plot () function, which will apply the changes to the same Figure object: import matplotlib.pyplot as plt x = [ 1, 2, 3, 4, 5, 6 ] y = [ 2, 4, 6, 5, 6, 8 ] y2 = [ 5, 3, 7, 8, 9, 6 ] fig, ax = plt.subplots () ax.plot (x, y) ax.plot (x, y2) plt.show ()

Why is it so hard to compare two line plots?

Sometimes, you might have two datasets, fit for line plots, but their values are significantly different, making it hard to compare both lines.

What do the coordinates of each point on the plot represent?

The coordinates of each point on the plot correspond to the values of two variables. Graphs are automatically scaled to the values of your data, although you can control the scale by specifying the coordinate axes. You can create a simple two-dimensional plot for one set of measures by using the following PLOT statement:

How to plot multiple lines for each group in ggplot2 plot?

The following syntax shows a more general approach for the plotting of multiple lines for each group in a ggplot2 plot by reshaping our data frame from wide to long format. We will use the functions of the reshape2 package to transform our data from wide to long format.


Video Answer


3 Answers

To take @Rui Barradas's solution one step further, you can use guide_legend/override.aes() to modify the legends for the lines and points by giving the lines NA shape and the points NA linetypes.

my_color <- setNames(c("red", "blue", "green"),
                     c('ts1', 'ts2', 'ts3'))
ggplot(dat, aes(x = Date, y = value, colour = Var2)) +
  geom_line(data = subset(dat, Var2 != "ts3")) +
  geom_point(data = subset(dat, Var2 == "ts3")) +
  scale_color_manual("Legend", values = my_color) +
  guides(color = guide_legend(override.aes = list(linetype = c(1,   1, NA),
                                                  shape    = c(NA, NA, 19)))) +
  theme_classic(base_size = 14)

Created on 2019-12-22 by the reprex package (v0.3.0)

like image 132
Tung Avatar answered Nov 09 '22 07:11

Tung


It is possible to keep the data in long format as long as the data argument to the two geom_ layers subsets the full data set. This will automatically create a legend with all the colors.

ggplot(dat, aes(x = Date, y = value, colour = Var2)) +
  geom_line(data = subset(dat, Var2 != "ts3")) +
  geom_point(data = subset(dat, Var2 == "ts3")) +
  scale_color_manual(values = c("red", "blue", "green"))

enter image description here

like image 35
Rui Barradas Avatar answered Nov 09 '22 07:11

Rui Barradas


In such cases it seems keeping data in wide format is beneficial.

tidyr::pivot_wider(dat, names_from = Var2, values_from = value) %>%
     ggplot() +
     geom_line(aes(Date, ts1, color = 'ts1')) + 
     geom_line(aes(Date, ts2, color = 'ts2')) + 
     geom_point(aes(Date, ts3, color = 'ts3'))

enter image description here

like image 33
Ronak Shah Avatar answered Nov 09 '22 05:11

Ronak Shah