简体   繁体   中英

How to plot multiple line chart in ggplot2?

How to plot multiple line chart in ggplot2.

For example:

Date       Total Count_1   %_one    Count_2  %_two
07-11-2022   368     43     11.7%    132    35.9%
07-12-2022   510     56     11.0%    177    34.7%
07-13-2022   544     62     11.4%    187    34.4%
07-14-2022   496     77     15.5%    196    39.5%
07-15-2022   320     39     12.2%    118    36.9%
07-16-2022   295     33     11.2%    99     33.6%

I want %_one and %two to be plotted as a different lines in a chart so that my output looks something like this

在此处输入图像描述

Thanks for any help

Here is another answer with 2 geom_line() as recommended by Basti

### Import library
library(ggplot2)

### Data
df <- data.frame(Date=as.Date(c("2022-07-11", "2022-07-12", "2022-07-13", "2022-07-14", "2022-07-15", "2022-07-16")), 
                 p_one=c(11.7, 11.0, 11.4, 15.5, 12.2, 11.2)/100, 
                 p_two=c(35.9, 34.7, 34.4, 39.5, 36.9, 33.6)/100)

### Display plot
ggplot(df, aes(x = Date)) + 
  geom_line(aes(x = Date, y = p_one), color="blue", size=2) + 
  geom_line(aes(x = Date, y = p_two), color="red", size=2) +
  scale_y_continuous(labels=scales::percent) + 
  ylab("%") + 
  ggtitle("Summaries")

在此处输入图像描述

Option with legend

ggplot(df, aes(x = Date)) + 
  geom_line(data=df, aes(x=Date, y=p_one, color="p_one"), size=2) + 
  geom_line(data=df, aes(x=Date, y=p_two, color="p_two"), size=2) + 
  scale_y_continuous(labels=scales::percent) + 
  ylab("%") + 
  ggtitle("Summaries") + 
  scale_color_manual(name="", values=c("p_one"="blue", "p_two"="red"))

在此处输入图像描述

The quick way is adding two geom_line s, one for each column - as @Basti and @Yacine Hajji have suggested. However, this is suboptimal (for example, it won't create a legend).

The better way is reshaping your dataset to long format, as @mfalco is suggesting. This can be done programmatically, for example with tidyr 's pivot_longer .

# Create dataset
dat <- data.table::fread("Date       Total Count_1   %_one    Count_2  %_two
07-11-2022   368     43     11.7%    132    35.9%
07-12-2022   510     56     11.0%    177    34.7%
07-13-2022   544     62     11.4%    187    34.4%
07-14-2022   496     77     15.5%    196    39.5%
07-15-2022   320     39     12.2%    118    36.9%
07-16-2022   295     33     11.2%    99     33.6%")

library(tidyr)
library(dplyr)
library(ggplot2)

dat_long <- dat |> 
    # Make names follow the same pattern
    rename(percent_1 = `%_one`, percent_2 = `%_two`) |> 
    # Reshape multiple columns at once
    pivot_longer(-c(Date, Total),
                 names_to = c(".value", "measurement"),
                 names_pattern = "(.*)_(.*)"
                 ) |> 
    # Coerce data type for the percent column from chr to numeric
    mutate(percent = gsub("%", "", percent) |> as.numeric())

# Plot
dat_long |>
    ggplot(aes(Date, percent, colour = measurement, group = measurement)) +
    geom_line() +
    coord_cartesian(ylim = c(0, 50))

Created on 2022-08-12 by the reprex package (v2.0.1)

You need to rearrange the data.frame so Count1 and Count2 info are into their own separate rows. Also add a new column indicating whether that row refers to Count1 or 2 so you can pass that information to the aes(group) in ggplot function. See this example:

df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                dose=rep(c("D0.5", "D1", "D2"),2),
                len=c(6.8, 15, 33, 4.2, 10, 29.5))

ggplot(data=df2, aes(x=dose, y=len, group=supp)) + geom_line()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM