1

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

enter image description here

Thanks for any help

2
  • 1
    rbind your columns %_one and %_two into one column and add a variable differentiating each other Commented Aug 12, 2022 at 8:35
  • 2
    Use multiple geom_line(aes(y="%_one")) etc Commented Aug 12, 2022 at 8:35

3 Answers 3

3

The quick way is adding two geom_lines, 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)

Sign up to request clarification or add additional context in comments.

2 Comments

@Andrew M - for some reason this code is not working for me and showing error Error in loc_validate(): ! Can't subset columns past the end.
I suspect your dataset is somehow different to the example you posted. Can you edit your question to add the output of dput(head(datasetname))?
3

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

enter image description here

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

enter image description here

4 Comments

In my dataset p_one and p_two columns have % sign after every value and so the datatype of those columns are `character
You can use a substring of your column to remove '%' sign as follows yourCol <- c("9.0%", "11.0%", "50.8%") ; substr(yourCol, 1, nchar(yourCol)-1)
Thanks @Yacine Hajji but how do I add legend to this?
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"))
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.