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
CodePudding user response:
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))
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"))
CodePudding user response:
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()