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).
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)
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"))
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=".."
I have the dataframe below
GO<-c("cytosol (GO:0005829)","cytosol (GO:0005829)")
FE<-c(2.70,4.38)
FDR<-c(0.00159,0.00857)
Facet<-c("ileum 24h","ileum 72h")
CCC<-data.frame(GO,FE,FDR,Facet)
and with this code
CCC %>%
arrange(desc(CCC$GO))%>%
ggplot(aes(x = FDR, y = GO, size = FE, color = FDR)) +
geom_point(alpha = 0.5) +
scale_size(range = c(5, 8), name = "Fold enrichment") +
facet_grid(cols = vars(Facet), scales = "free") +
theme(axis.title.x=element_blank(),axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 1)) +
scale_y_discrete(name = "GO biological process complete") +
scale_x_continuous(name = "FDR") +
scale_colour_gradient(low = "yellow", high = "red", name = "FDR") +
theme_bw()
I create a bubble plot with facets. But I want to delete the x-axis title 'FDR' and display the labels with an angle but despite setting the theme() it does not change.
You have put theme_bw() at the end, which over-writes your theme call. Put your custom themes at the end:
CCC %>%
arrange(desc(CCC$GO))%>%
ggplot(aes(x = FDR, y = GO, size = FE, color = FDR)) +
geom_point(alpha = 0.5) +
scale_size(range = c(5, 8), name = "Fold enrichment") +
scale_y_discrete(name = "GO biological process complete") +
scale_x_continuous(name = "FDR") +
scale_colour_gradient(low = "yellow", high = "red", name = "FDR") +
facet_grid(cols = vars(Facet), scales = "free") +
theme_bw() +
theme(axis.title.x = element_blank(),
axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 1))
I think it is good practice to lay out your calls to ggplot in a consistent way so that this kind of thing doesn't happen:
Call ggplot +
Geom (and stat) layers, ordered depending on which ones you want on top +
Scales +
Facets +
Labels and titles +
Global themes like theme_bw() +
Individual theme tweaks via theme
Only change the position of theme_bw():
library(tidyverse)
#Data
GO<-c("cytosol (GO:0005829)","cytosol (GO:0005829)")
FE<-c(2.70,4.38)
FDR<-c(0.00159,0.00857)
Facet<-c("ileum 24h","ileum 72h")
CCC<-data.frame(GO,FE,FDR,Facet)
#Plot
CCC %>%
arrange(desc(CCC$GO))%>%
ggplot(aes(x = FDR, y = GO, size = FE, color = FDR)) +
geom_point(alpha = 0.5) +
scale_size(range = c(5, 8), name = "Fold enrichment") +
facet_grid(cols = vars(Facet), scales = "free") +
xlab('')+
theme_bw()+
theme(axis.title.x=element_blank(),
axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 1)) +
scale_y_discrete(name = "GO biological process complete") +
scale_x_continuous(name = "") +
scale_colour_gradient(low = "yellow", high = "red", name = "FDR")
Output: