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