Customize lines in ggplot linegraph with multiple lines - r

I build this graph:
labels.minor <- c("nie","selten","manchmal", "mehrmals", "oft", "sehr oft", "immerzu")
df_ebf <- df_ebf %>%
map_df(rev)
ggplot(data=df_ebf, aes(x=forcats::fct_inorder(Skalen), y=Werte, group="")) +
geom_line(aes(y = Werte, color = "#003560")) +
geom_line(aes(y = SD_plus, color = "#8DAE10", linetype = "dashed")) +
geom_line(aes(y = SD_minus, color = "#8DAE10",linetype = "dashed")) +
geom_point(color = "#003560") +
coord_flip() +
labs(x="EBF-Skalen") +
scale_y_continuous(limits = c(0, 6), breaks = c(0,1,2,3,4,5,6), labels = paste0(0:6, "\n", labels.minor), sec.axis = sec_axis(~.x, breaks = 0:6)) +
scale_x_discrete(expand = c(0,0)) +
theme(panel.grid.major.y = element_blank(),panel.grid.minor.x = element_blank(),axis.line.x = element_line(size = 1, colour = "black", linetype=1),axis.title=element_blank())
But instead of changing the style of the lines, the styling just appears in the legend.

take them out of the aes:
aes(...), color="..", linetype=".."

Related

How to merge the legends in ggpplot

I have trouble merging the legend, the current result had assigned linetype, size, shape and colour seperately.
My data look like this:
library(ggplot2)
library(dplyr)
library(reshape2)
library(patchwork)
library(hrbrthemes)
rate="rate"
Jul="0.5‰"
Aug="0.6‰"
Sep="0.7‰"
df <- data.frame(rate,Jul,Aug,Sep)
df
d=melt(df,measure.vars = names(df)[2:4])
d
d$month=as.factor(d$variable)
d$percent=as.numeric(gsub("‰","",d$value))
And I plot the data by using ggplot2:
ggplot(d,aes(x=month,y=percent)) +
geom_point(aes(x=month,y=percent,color="Rate",shape="Rate"), size=2) +
geom_text(aes(label = paste(format(percent, digits = 4, format = "f"), "‰")),
color="black",vjust = -0.5, size = 3.5) +
geom_line(aes(x = month, y = percent, group=1, color="Rate",linetype = "Rate",size="Rate")) +
geom_hline(aes(yintercept=1,color="Target",linetype="Target",size="Target"))+
scale_y_continuous(breaks = seq(0,1.1,0.2),
labels = paste0(seq(0,1.1,0.2)," ‰")) +
expand_limits(y = c(0, 1.1)) +
labs(y="", x="") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_colour_manual(values = c(Rate = "#00BFC4", Target = "#F8766D")) +
scale_linetype_manual(values = c(Rate = "solid", Target = "dashed")) +
scale_shape_manual(values = c(Rate = 16, Target = NA)) +
scale_size_manual(values = c(Rate = 1, Target = .7)) +
theme(legend.key=element_blank(),legend.position="bottom")
The result of legend looked very messy:
I would like the legend look like as below:
So how to modify the code to merge the legend?
Thanks very much!
If we give the legends the same name, ggplot will try to merge them:
...
scale_colour_manual(values = c(Rate = "#00BFC4", Target = "#F8766D"), name = "Legend") +
scale_linetype_manual(values = c(Rate = "solid", Target = "dashed"), name = "Legend") +
scale_shape_manual(values = c(Rate = 16, Target = NA), name = "Legend") +
scale_size_manual(values = c(Rate = 1, Target = .7), name = "Legend") +
theme(legend.key=element_blank(),legend.position="bottom")

How to change the label color of hline in ggplot2

Here is my data:
month=c("Jan","Feb","Mar","Apr","May","Jun")
rate=c(70,80,90,85,88,76)
data=data.frame(month,rate)
data$month=factor(data$month, levels = month.abb)
I created the y label as below:
library(ggplot2)
ggplot(data,aes(x=month,y=rate)) +
geom_point(aes(x=month,y=rate), color="#00BFC4", size=2) +
geom_text(aes(label = paste(format(rate, digits = 4, format = "f"), "%")),
color="black",vjust = -0.5, size = 3.5) +
geom_line(aes(x = month, y = rate, group=1), color="#00BFC4", size=1) +
geom_hline(aes(yintercept=87), color = "#F8766D", linetype = "dashed", size = 0.5) +
scale_y_continuous(breaks = sort(c(seq(0,100,20),87)),
labels = paste0(sort(c(seq(0,100,20),87)),"%")) +
expand_limits(y = c(0,100)) +
labs(y = NULL, x= NULL)
As you can see, the hline label at 87% is in grey color, I'd like to know how to change the color into the "#F8766D" as the same color of the dashed line.
In addition, I'd like to know how to change the position of 87% to the right side of the picture (i.e how to modify the position as a second y axis) and change the label color at the same time. Thanks!
To change the color of hline label as the primary axis:
ylabel=sort(c(seq(0,100,20),87))
color1 <- ifelse(ylabel == 87, "#F8766D", "grey30")
library(ggplot2)
ggplot(data,aes(x=month,y=rate)) +
geom_point(aes(x=month,y=rate), color="#00BFC4", size=2) +
geom_text(aes(label = paste(format(rate, digits = 4, format = "f"), "%")),
color="black",vjust = -0.5, size = 3.5) +
geom_line(aes(x = month, y = rate, group=1), color="#00BFC4", size=1) +
geom_hline(aes(yintercept=87), color = "#F8766D", linetype = "dashed", size = 0.5) +
scale_y_continuous(breaks = sort(c(seq(0,100,20),87)),
labels = paste0(sort(c(seq(0,100,20),87)),"%")) +
expand_limits(y = c(0,100)) +
labs(y = NULL, x= NULL) +
theme(axis.ticks.y = element_line(color = color1),
axis.text.y=element_text(color=color1))
To change the color of hline label as the second axis:
ggplot(data,aes(x=month,y=rate)) +
geom_point(aes(x=month,y=rate), color="#00BFC4", size=2) +
geom_text(aes(label = paste(format(rate, digits = 4, format = "f"), "%")),
color="black",vjust = -0.5, size = 3.5) +
geom_line(aes(x = month, y = rate, group=1), color="#00BFC4", size=1) +
geom_hline(aes(yintercept=87), color = "#F8766D", linetype = "dashed", size = 0.5) +
scale_y_continuous(breaks = seq(0,100,20),
labels = paste0(seq(0,100,20),"%"),
sec.axis = sec_axis(trans=~., breaks=87, labels=paste0(87,"%"))) +
expand_limits(y = c(0,100)) +
labs(y = NULL, x= NULL) +
theme(axis.ticks.y.right = element_line(color = "#F8766D"),
axis.text.y.right = element_text(color = "#F8766D"))

How to add the legend for hline in ggplot2

My data looks like this:
month=c("Jan","Feb","Mar","Apr","May","Jun")
rate=c(70,80,90,85,88,76)
dd=data.frame(month,rate)
dd$type="Rate"
dd$month=factor(dd$month)
I tried to create the plot like this:
ggplot(dd,aes(x=month,y=rate,color=type)) +
geom_point(aes(x=month,y=rate, group=1), size=2) +
geom_text(aes(label = paste(format(rate, digits = 4, format = "f"), "%")),
color="black",vjust = -0.5, size = 3.5) +
geom_line(aes(x = month, y = rate, group=1), size=1) +
geom_hline(aes(yintercept=85), linetype='dashed',colour="#F8766D", show.legend=T) +
labs(y="", x="") +
scale_colour_manual(values = c("#00BFC4")) +
scale_fill_discrete(limits = c("Target")) +
theme(legend.position="bottom") +
theme(legend.title = element_blank())
As you can see, the legend of Rate and Target are overlapping together (there is red dash line in the green line), I'd like to know how to create the legend for Target and Rate in the correct way. Thanks!
One option to achieve your desired result would be to map on aesthetics and make use of scale_xxx_manual instead of setting the color, linetypes, ... via arguments:
month=c("Jan","Feb","Mar","Apr","May","Jun")
rate=c(70,80,90,85,88,76)
dd=data.frame(month,rate)
dd$type="Rate"
dd$month=factor(dd$month)
library(ggplot2)
ggplot(dd,aes(x=month,y=rate, color="Rate", linetype = "Rate")) +
geom_point(aes(x=month,y=rate, shape = "Rate"), size=2) +
geom_text(aes(label = paste(format(rate, digits = 4, format = "f"), "%")),
color="black",vjust = -0.5, size = 3.5) +
geom_line(aes(x = month, y = rate, group=1, size = "Rate")) +
geom_hline(aes(yintercept=85, color = "Target", linetype = "Target", size = "Target")) +
labs(y = NULL, x= NULL, color = NULL, linetype = NULL, shape = NULL, size = NULL) +
scale_colour_manual(values = c(Rate = "#00BFC4", Target = "#F8766D")) +
scale_linetype_manual(values = c(Rate = "solid", Target = "dashed")) +
scale_shape_manual(values = c(Rate = 16, Target = NA)) +
scale_size_manual(values = c(Rate = 1, Target = .5)) +
theme(legend.position="bottom")

Add abline to legend

First, sorry for posting without reproducible data. Hope you guys understand my question. This is my code. At the end of the code, I am trying to add abline. With the code, I am trying to add the name of abline to the legend but it does not work.
ggplot(aes(x = week_id2, y = Index, color = Chain2, linetype = Chain2, group = Chain2),
data = data00 +
geom_point(aes(shape=Chain2), size = 3) +
geom_line() +
scale_linetype_manual(values=c("twodash", "dashed", "dotted", "dotdash", "longdash")) +
scale_shape_manual(values=c(1:5)) +
xlab("Week") +
ylab("Index") +
geom_hline(aes(yintercept=1))
As shown, I just simply add a name of the abline (let's say the name is "add") in the legend. How should I do it with my current code?
You can add either color or linetype to aes then use scale_color_xxx or scale_linetype_xxx to fine tune the legend. Here is an example using economics dataset
library(tidyverse)
df <- economics %>%
select(date, psavert, uempmed) %>%
gather(key = "variable", value = "value", -date)
ggplot(df, aes(x = date, y = value)) +
geom_line(aes(color = variable), size = 1) +
geom_hline(aes(yintercept = 10, color = "My line")) +
scale_color_brewer(palette = "Dark2",
breaks = c("psavert", "uempmed", "My line")) +
theme_minimal()
ggplot(df, aes(x = date, y = value)) +
geom_line(aes(color = variable, linetype = variable), size = 1) +
geom_hline(aes(yintercept = 10, color = "My line", linetype = "My line")) +
scale_color_brewer(palette = "Dark2",
breaks = c("psavert", "uempmed", "My line")) +
scale_linetype_manual(values = c("twodash", "dashed", "dotted"),
breaks = c("psavert", "uempmed", "My line")) +
theme_minimal()
Edit: per OP's request, we separate linetype & color/shape legends
ggplot(df, aes(x = date, y = value)) +
geom_line(aes(color = variable), size = 0.75) +
geom_point(aes(color = variable, shape = variable)) +
geom_hline(aes(yintercept = 10, linetype = "My line")) +
scale_color_brewer(palette = "Dark2",
breaks = c("psavert", "uempmed")) +
scale_linetype_manual("", values = c("twodash"),
breaks = c("My line")) +
scale_shape_manual(values = c(17, 19)) +
# Set legend order
guides(colour = guide_legend(order = 1),
shape = guide_legend(order = 1),
linetype = guide_legend(order = 2)) +
theme_classic() +
# Move legends closer to each other
theme(legend.title = element_blank(),
legend.justification = "center",
legend.spacing = unit(0.1, "cm"),
legend.spacing.y = unit(0.05, "cm"),
legend.margin = margin(0, 0, 0, 0),
legend.box.margin = margin(0, 0, 0, 0))
Created on 2018-05-08 by the reprex package (v0.2.0).

Add legend to manually added lines using ggplot

I'm trying to add the corresponding legend for 3 manually added lines using ggplot. My code is the following:
library(ggplot2)
df = data.frame(error = c(0.0832544999, 0.0226680026, 0.0082536264, 0.0049199958, 0.0003917755, 0.0003859976, 0.0003888253, 0.0003953918, 0.0003958398), sDev = c(8.188111e-03, 2.976161e-03, 1.466221e-03, 2.141425e-03, 2.126976e-05, 2.139364e-05, 2.169059e-05, 2.629895e-05, 2.745938e-05))
minimum <- 6
best.model <- 5
gplot <- ggplot(df, aes(x=1:length(error), y=error)) +
scale_x_continuous(breaks = seq_along(df$error)) +
geom_point(size = 3) +
geom_line() +
geom_errorbar(data = df, aes(x = 1:length(error), ymin = error - sDev, ymax = error + sDev),
width = 0.1) +
geom_hline(data = df, aes(yintercept = error[minimum] + sDev[minimum]), linetype = "dashed") +
geom_vline(xintercept = minimum, linetype = "dotted", color = "red", size = 1) +
geom_vline(xintercept = best.model, linetype = "dotted", color = "blue", size = 1) +
theme_gray(base_size = 18) +
theme(axis.text = element_text(color = "black")) +
labs(x = "# of parameters", fontface = "bold") +
labs(y = "CV error") +
labs(title = "Cross-validation error curve")
I'd like to know how to add the legends for the 3 dotted lines in black, red, and blue.
Thanks a lot in advance!
The trick is to use appropriate mapping:
gplot <- ggplot(df, aes(x=1:length(error), y=error)) +
scale_x_continuous(breaks = seq_along(df$error)) +
geom_point(size = 3) +
geom_line() +
geom_errorbar(data = df, aes(x = 1:length(error), ymin = error - sDev, ymax = error + sDev),
width = 0.1) +
geom_hline(data = df, aes(yintercept = error[minimum] + sDev[minimum], linetype="a", colour="a")) +
geom_vline(data= data.frame(type="b", col="b", minimum=minimum),
aes(linetype=type, colour=col, xintercept = minimum), size = 1, show_guide = TRUE) +
geom_vline(data= data.frame(type="b", col="b", best.model=best.model),
aes(linetype="c", colour="c", xintercept = best.model), size = 1, show_guide = TRUE) +
scale_colour_manual(name="Legend", values = c("a" = "black", "b" = "red", "c" = "blue")) +
scale_linetype_manual(name="Legend", values = c("a" = "dashed", "b" = "dotted", "c" = "dotted")) +
theme_gray(base_size = 18) +
theme(axis.text = element_text(color = "black"),
legend.key.height = grid::unit(0.1, "npc")) +
labs(x = "# of parameters", fontface = "bold") +
labs(y = "CV error") +
labs(title = "Cross-validation error curve")

Resources