I have a dataframe which is something like this:
#sample dataframe
set.seed(42)
dates <- seq.Date(as.Date("2020-01-01"), as.Date("2021-12-30"), "day")
n <- length(dates)
dat <- data.frame(created_at = dates,
category = rep(LETTERS[1:4], n/2),
With_Outlier = sample(18:100, n, replace=TRUE),
count = sample(10:40, n, replace=TRUE))
I am using the following code to plot the data in a way I desire:
ggplot(data = dat, aes(x = created_at))
geom_line(aes(y = With_Outlier, colour = paste0(category," With_Outliers")))
geom_line(aes(y = count, colour = paste0(category," Value Counts")))
facet_wrap(~ category, ncol = 1, scales = "free_x")
labs(title = 'Data Visualization',
x = 'Month',
y = 'Count')
theme_bw()
scale_x_date(date_breaks = '1 month', date_labels = "%b-%y", expand = expansion(0,0))
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))
theme(text=element_text(size=13),panel.spacing.x=unit(0.6, "lines"),
panel.spacing.y=unit(1, "lines"), legend.position="bottom")
scale_linetype_manual(name = NULL, values = 2) labs(color = NULL)
And this is the output I am getting:
Now I do some kind of operation on the data and for category
B, the values are updated (I'd call it Clean_Values and Clean_Count, for ease, I save them in another dataframe). For the rest of the categories, everything remains same. This way, I need to plot two lines for two columns just for Category B.
Any guidance please?
CodePudding user response:
I did some work and came up with this solution, posting here so it might help someone else.
Creating sample new dataframe with Clean_Values and Clean_Count.
dat1 <- data.frame(created_at = dates,
category = rep(LETTERS[2:2], n/2),
Clean_Values = sample(200:400, n, replace=TRUE),
Clean_Count = sample(500:600, n, replace=TRUE))
)
Now to plot the data, I've added following two lines:
geom_line(data = dat1, aes(x = created_at, y = Clean_Values, colour = paste0(" Clean Values")))
geom_line(data = dat1, aes(x = created_at, y = Clean_Count, colour = paste0(" Clean Count")))
Here's the complete code:
ggplot(data = dat, aes(x = created_at))
geom_line(aes(y = With_Outlier, colour = paste0(category," With_Outliers")))
geom_line(aes(y = count, colour = paste0(category," Value Counts")))
facet_wrap(~ category, ncol = 1, scales = "free_x")
geom_line(data = dat1, aes(x = created_at, y = Clean_Values, colour = paste0(" Clean Values")))
geom_line(data = dat1, aes(x = created_at, y = Clean_Count, colour = paste0(" Clean Count")))
labs(title = 'Data Visualization',
x = 'Month',
y = 'Count')
theme_bw()
scale_x_date(date_breaks = '1 month', date_labels = "%b-%y", expand = expansion(0,0))
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))
theme(text=element_text(size=13),panel.spacing.x=unit(0.6, "lines"),
panel.spacing.y=unit(1, "lines"), legend.position="bottom")
scale_linetype_manual(name = NULL, values = 2) labs(color = NULL)
And below is the output with two new lines as desired also in the legend too: