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")
Related
I have horizontal dots plot visualized by Plotly in R.
My data set contains 3 numerical and 1 categorical variable.
'origin' is on y-axis. 'change' and 'rate' variables are visualized into circles. Now I want to put 'Percentage' variable on right axis in the circles
df <- data.frame (origin = c("A","B","C","D","E","F","G","H","I","J"),
Percentage = c(23,16,32,71,3,60,15,21,44,60),
rate = c(10,12,20,200,-25,12,13,90,-105,23),
change = c(10,12,-5,12,6,8,0.5,-2,5,-2))
library(ggplot2)
ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
geom_hline(aes(yintercept = origin), color = 'gray') +
geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
geom_point(aes(color = 'Rate'), size = 10) +
geom_text(aes(label = rate), color = 'white') +
geom_point(aes(x = change, color = 'Change'), size = 10) +
geom_text(aes(label = change, x = change)) +
theme_minimal(base_size = 16) +
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
scale_color_manual(values = c('#aac7c4', '#5f9299')) +
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50')) +
labs(color = NULL, y = NULL)
Output:
Expected Output:
You could do
ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
geom_hline(aes(yintercept = origin), color = 'gray') +
geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
geom_point(aes(color = 'Rate'), size = 10) +
geom_text(aes(label = rate), color = 'white') +
geom_point(aes(x = change, color = 'Change'), size = 10) +
geom_text(aes(label = change, x = change)) +
geom_point(aes(x = 220, fill = "Percentage"), color = "blue",
size = 12, shape = 21) +
geom_text(aes(x = 220, label = paste0(Percentage, "%"))) +
theme_minimal(base_size = 16) +
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
scale_color_manual(values = c('#aac7c4', '#5f9299')) +
scale_fill_manual(values = "white", name = NULL) +
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50')) +
coord_cartesian(xlim = c(-120, 230), ylim = c(0, 11),
expand = FALSE) +
labs(color = NULL, y = NULL)
I'm trying to modify the legend of the hline.
Here is my data:
mydata=data.frame(month = c("Jan-1","Jan-10","Jan-15","Jan-20","Jan-25","Jan-30"),
rate = c(88.8,86,88.5,90,89,87))
And here is the code I used to create the figure:
library(ggplot2)
library(ggforce)
ggplot(mydata, aes(x = month, y = rate)) +
geom_point(aes(group = 1,colour = after_stat(y < 88),
shape=after_stat(y < 88)),size=2) +
geom_text(aes(label = paste(format(rate, digits = 4, format = "f"), "%")),
color="black",vjust = -0.5, size = 3.5) +
geom_link2(aes(group = 1,colour = after_stat(y < 88)),size=1) +
geom_hline(aes(yintercept=88,color="Target",linetype="Target"),size=0.7)+
labs(y = NULL, x= NULL, color = NULL, linetype = NULL, shape = NULL, size = NULL) +
scale_colour_manual(values = c("TRUE"="#F8766D","FALSE"="#00BFC4","Target"="#619CFF")) +
scale_shape_manual(values = c("TRUE"= 16, "FALSE"=16, "Target" = NA)) +
scale_linetype_manual(values = c("TRUE"="solid","FALSE"="solid","Target" = "dashed"))
As you can see, the legend of "Target" is still a solid line, I'd like to know how to change it into a dashed line.
In addition, I'd like to know why it gave me errors when I tried to modify the linetype in geom_link2():
ggplot(d1, aes(x = month, y = percent)) +
geom_point(aes(group = 1,colour = after_stat(ifelse(y > 88,"qualified","unqualified")),
shape=after_stat(ifelse(y > 88,"qualified","unqualified"))), size=2) +
geom_text(aes(label = paste(format(percent, digits = 4, format = "f"), "%")),
color="black",vjust = -0.5, size = 3.5) +
geom_link2(aes(group=1, colour = after_stat(ifelse(y > 88,"qualified","unqualified")),
linetype = after_stat(ifelse(y > 88,"qualified","unqualified"))),size=1) +
geom_hline(aes(yintercept=88,color="Target",linetype="Target"),size=0.7)+
scale_y_continuous(breaks = sort(c(seq(80,100,10),88)),
labels = paste0(sort(c(seq(80,100,10),88))," %")) +
expand_limits(y = c(80, 100)) +
labs(y = NULL, x= NULL, color = NULL, linetype = NULL, shape = NULL, size = NULL) +
scale_colour_manual(values = c(qualified="#00BFC4",unqualified="#F8766D",Target="#F8766D")) +
scale_shape_manual(values = c(qualified= 16, unqualified=16, Target = NA)) +
scale_linetype_manual(values = c(qualified="solid", unqualified="solid", Target="dashed"))
Error: geom_path_interpolate: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
One option would be to override the aesthetics via guide_legend(override.aes = ...) like so:
library(ggplot2)
library(ggforce)
ggplot(mydata, aes(x = month, y = rate)) +
geom_point(aes(group = 1,colour = after_stat(y < 88),
shape=after_stat(y < 88)),size=2) +
geom_text(aes(label = paste(format(rate, digits = 4, format = "f"), "%")),
color="black",vjust = -0.5, size = 3.5) +
geom_link2(aes(group = 1, colour = after_stat(y < 88)), size=1) +
geom_hline(aes(yintercept=88, color="Target", linetype="Target"),size=0.7)+
labs(y = NULL, x= NULL, color = NULL, linetype = NULL, shape = NULL, size = NULL) +
scale_colour_manual(values = c("TRUE"="#F8766D","FALSE"="#00BFC4","Target"="#619CFF")) +
scale_shape_manual(values = c("TRUE"= 16, "FALSE"=16, "Target" = NA)) +
scale_linetype_manual(values = c("TRUE"="solid","FALSE"="solid","Target" = "dashed")) +
guides(color = guide_legend(override.aes = list(linetype = c("solid", "solid", "dashed"))))
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")
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=".."
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).