How can I add an arrow next to the x and y axis titles, such as the picture below?
quadrant <- ggplot(quadrants, aes(x=Ampolla, y=Energia, label=Branca_percIPI))+
coord_fixed() +
coord_cartesian(clip = 'off') +
scale_x_continuous(expand = c(0, 0), limits = c(-5, 50)) +
scale_y_continuous(expand = c(0, 0), limits = c(0,50))+
geom_vline(xintercept = 11.3, color = "grey50", size=1.2) +
geom_hline(yintercept = 17.7, color = "grey50", size=1.2) +
geom_text_repel(size = 7,
colour = "#2896BA",
min.segment.length = Inf, hjust="right", nudge_x=0.9, nudge_y=1.5, force=1,
arrow = arrow(length=unit(0.5,"cm"), ends="first"), lineheight = 1)+
geom_point(colour="#2896BA", size=3.5)+
labs(title = "Incidència dels colls d'ampolla i dependència energètica total",
subtitle="(% de variació interanual de l'IPI*, % d'empreses afectades pels colls d'ampolla i % de dependència energètica)",
x = "Colls d'ampolla (% d'empreses afectades)**",
y = "Dependència energètica total (%)")+
annotation_custom(segmentsGrob(c(0.3, -0.1), c(-0.085, 0.28),
c(1, -0.1), c(-0.085, 0.28), gp = gpar(lwd = 2),
arrow = arrow(length = unit(2.5, 'mm'))))
You can turn clipping off inside coord_cartesian and add custom annotations for the arrows using segmentsGrob from the grid package inside annotation_custom:
library(ggplot2)
library(grid)
ggplot(iris, aes(Sepal.Length, Petal.Width)) +
geom_point(color = '#2896ba') +
geom_vline(xintercept = 5.5, color = 'gray50') +
geom_hline(yintercept = 0.8, color = 'gray50') +
coord_cartesian(clip = 'off') +
theme_minimal(base_size = 16) +
theme(axis.title = element_text(hjust = 0),
plot.caption = element_text(hjust = 0),
panel.grid = element_blank()) +
annotation_custom(segmentsGrob(c(0.3, -0.1), c(-0.085, 0.28),
c(1, -0.1), c(-0.085, 1), gp = gpar(lwd = 2),
arrow = arrow(length = unit(2.5, 'mm')))) +
labs(caption = paste0("Here is a very long caption to demonstrate that ",
"it is possible\nto add a very long caption ",
'underneath the x axis, thereby\n',
'emulating the caption in the plot in the question.'),
title = 'Another iris plot', subtitle = 'Just in case you need one')
Related
here is my data and code for chart line:
df<- data.frame(direct= 10:85, indirect= 55:130, age=15:90)
ggplot(data=df)+
geom_line(mapping=aes(y=direct,x= age,color="direct"),linetype="solid" ) +
geom_line(mapping=aes(y=indirect,x= age,color="indirect"),linetype="dashed") +
scale_color_manual(values = c(
'direct' = 'black',
'indirect' = 'black')) +
labs(color = NULL)+
scale_x_continuous(breaks = seq(15, 90, by = 5))+
labs(y= "Time Spent (in minutes)")+
guides(color = guide_legend(override.aes = list(linetype = c("solid","dashed"))))+
theme_classic()+
theme(plot.title = element_text(hjust = 0.5, size=9, face="bold"),legend.position=c(.90,.90))
I want to put legend in the middle of my each line as the picture:
You can add annotate to your lines. You can use the following code:
library(tidyverse)
df<- data.frame(direct= 10:85, indirect= 55:130, age=15:90)
ggplot(data=df)+
geom_line(mapping=aes(y=direct,x= age,color="direct"),linetype="dashed" ) +
geom_line(mapping=aes(y=indirect,x= age,color="indirect"),linetype="solid") +
scale_color_manual(values = c(
'direct' = 'black',
'indirect' = 'black')) +
labs(color = NULL)+
scale_x_continuous(breaks = seq(15, 90, by = 5))+
labs(y= "Time Spent (in minutes)")+
guides(color = guide_legend(override.aes = list(linetype = c("solid","dashed"))))+
theme_classic()+
theme(plot.title = element_text(hjust = 0.5, size=9, face="bold"), legend.position = "none") +
annotate('text', x=50, y=55, label = "direct")+
annotate('text', x=50, y=100, label = "indirect")
Output:
Not 100% what you desired. But one option would be the geomtextpath package which allows to easily add direct labels to lines or ...
library(ggplot2)
library(geomtextpath)
df <- data.frame(direct = 10:85, indirect = 55:130, age = 15:90)
ggplot(data = df) +
geom_textline(mapping = aes(y = direct, x = age, color = "direct",
label = "direct"), linetype = "solid", offset = unit(5, "pt"), gap = FALSE) +
geom_textline(mapping = aes(y = indirect, x = age, color = "indirect",
label = "indirect"), linetype = "dashed", offset = unit(5, "pt"), gap = FALSE) +
scale_color_manual(values = c(
"direct" = "black",
"indirect" = "black"
)) +
labs(color = NULL) +
scale_x_continuous(breaks = seq(15, 90, by = 5)) +
labs(y = "Time Spent (in minutes)") +
guides(color = guide_legend(override.aes = list(linetype = c("solid", "dashed")))) +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5, size = 9, face = "bold"), legend.position = c(.90, .90)) +
guides(color = "none")
I'm trying to make a world map with a custom legend on the right side. The legend should be with the prepared text on the left and the generated numbers on the right. I tried but to no avail. I need help My code is as follows:
library(dplyr)
library(ggplot2)
library(ggrepel)
library(rworldmap)
world_map_without_antarctica <- getMap()[-which(getMap()$ADMIN=='Antarctica'),] #get data from web
world_map_without_antarctica_t <- fortify(world_map_without_antarctica)
Data <- data.frame( "lon"=c(17.816883, 38.544239,20.895352,20.819651,35.392102,99.060241,
43.756911, 10.288485, 16.566191, 14.076159,8.118301,16.596266,
121.544442,-73.077732,14.938152),
"lat"=c(44.1807678, 35.0126143, 42.5793648, 44.2330372, 38.9907297,
39.5015541, 33.0368223, 51.1337227, 45.0162344, 47.6139488,
46.7917377, 62.8114850, 15.7509443, 3.9272139, 46.1254221),
"NAME"=c("Bosnia and Herzegovina", "Syria", "Kosovo","Republic of Serbia",
"Turkey","United States of America","Iraq","Germany","Croatia",
"Austria","Switzerland","Sweden","Philippines","Colombia","Slovenia"),
"Count"=c(65800,32636,15005,9276,6979,6528,6449,
5830,4862,3109,2959,2777,2577,2315,1394))
Data$label <- paste0(Data$NAME,': ',Data$Count)
word_data_merged <- merge(world_map_without_antarctica_t, Data[ , c("NAME","Count")], by.x="id", by.y="NAME", all.x=T)
word_data_merged <- word_data_merged %>% arrange(id, order)
country_shapes <- geom_polygon(data = world_map_without_antarctica_t, aes(x = long, y = lat, group = group),fill = NA)
maptheme <-
theme(panel.grid = element_blank())+
theme(axis.text = element_blank())+
theme(axis.ticks = element_blank())+
theme(axis.title = element_blank())+
theme(legend.position = "bottom")+
theme(panel.grid = element_blank())+
theme(plot.margin = unit(c(0, 0, 0.5, 0), 'cm'))
guide = guide_colorbar(
title="legend_title",
label = TRUE,
draw.ulim = TRUE,
draw.llim = TRUE,
frame.colour = "black",
ticks = TRUE,
nbin = 10,
label.position = "bottom",
barwidth = 13,
barheight = 1.3,
direction = 'horizontal')
ggplot(word_data_merged) +
labs(title = "plot_title", subtitle = "plot_subtitle") +
country_shapes +
scale_fill_gradient(high = "#381802", low = "#fccaa7", guide = guide) +
geom_polygon(aes(long, lat, group = group, fill=Count),alpha=1) +
geom_point(data=Data[Data$label !="",],aes(x = lon, y = lat), shape = 21,fill= "#275083", color = "#275083", size = 1.5,alpha=0.5) +
geom_path(aes(x=long,y=lat, group = group), color="#c7c9c9", size= 0.5, alpha=0.4) +
geom_label_repel(data=Data,aes(x= lon,y= lat,label = label),
size = 5,
show.legend= F,
fontface = "bold",
point.padding = unit(0.2, "lines") ) +
maptheme +
theme(panel.background = element_rect(fill = "#ebf2f7"))
After running the code, the following world map is obtained:
How can I add a legend with free text entry? I would like the map to look like in this picture:
This might help:
a) changing plot.margin,
b) adding geom_text for the annotation (updated with #Tung's suggestion to use check_overlap = TRUE to sharpen up the text), and
c) coord_cartesian(clip = 'off') to allow drawing outside of the plot area
ggplot(word_data_merged) +
labs(title = "plot_title", subtitle = "plot_subtitle") +
country_shapes +
scale_fill_gradient(high = "#381802", low = "#fccaa7", guide = guide) +
geom_polygon(aes(long, lat, group = group, fill=Count),alpha=1) +
geom_point(data=Data[Data$label !="",],aes(x = lon, y = lat), shape = 21,fill= "#275083", color = "#275083", size = 1.5,alpha=0.5) +
geom_path(aes(x=long,y=lat, group = group), color="#c7c9c9", size= 0.5, alpha=0.4) +
geom_label_repel(data=Data,aes(x= lon,y= lat,label = label),
size = 5,
show.legend= F,
fontface = "bold",
point.padding = unit(0.2, "lines") ) +
geom_text(aes(label = "Statistics", x = 180, y = 80),
fontface = "bold",
hjust = -0.5,
size = 5,
check_overlap = TRUE) +
geom_text(aes(label = "Total unique countries = #", x = 180, y = 70),
hjust = -0.35,
size = 3,
check_overlap = TRUE) +
coord_cartesian(clip = 'off')+
maptheme +
theme(panel.background = element_rect(fill = "#ebf2f7"),
plot.margin = unit(c(0, 4, 0.5, 0), 'cm'))
#> Warning: ggrepel: 12 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps
Based on: ggplot2 - annotate outside of plot
Created on 2021-01-16 by the reprex package (v0.3.0)
I am trying to add some annotations to the graph that I create with ggplot2 2. The font size and type seem to be different for the annotations compared to everything else. I have a font size of 20 for the labels and the legend, but even with a size of 12 and specifying the font type (family) as Arial, the annotations look very different than the rest of the graph.
fontsize <- 20 / .pt
plot <- ggplot(material) + theme_void() + scale_color_hue() +
aes(x = strain, y = stress, colour = type, group = type) +
geom_xspline(size = 1, spline_shape = 0.5) + labs(x = "Strain (in/in)",
y = "Stress (ksi)", color = element_blank()) + theme(legend.position = "bottom",
legend.box.background = element_rect(colour = "black", size = 0.5),
axis.title.y = element_text(size=20, colour="black", angle = 90, vjust = -22, hjust = 0.55),
axis.title.x = element_text(size=20, colour="black", vjust = 7, hjust = 0.6),
axis.text.y = element_blank(), axis.text.x = element_blank(),
legend.text=element_text(size=20), legend.box.margin = margin(10,10,10,10),
legend.spacing.x = unit(0, 'cm')) +
scale_y_continuous(trans = "reverse") + scale_x_continuous(trans = "reverse") +
geom_vline(aes(xintercept = 0), size = 0.3) + geom_hline(aes(yintercept = 0), size = 0.3) +
geom_segment(aes(x = 0, y = 0, xend = -0.0009226, yend = 0), size = 1) +
geom_segment(aes(x = -0.0009226, y = 0, xend = -0.0011707, yend = -0.94), size = 1) +
geom_segment(aes(x = 0, y = -4.958, xend = -0.00333, yend = -4.958),
size = 0.6, linetype = "dotted", colour = "black") +
geom_segment(aes(x = -0.00333, y = 0, xend = -0.00333, yend = -4.958),
size = 0.6, linetype = "dotted", colour = "black") +
geom_segment(aes(x = -0.0024, y = 0, xend = -0.0024, yend = -4.958),
size = 0.6, linetype = "dotted", colour = "black") +
geom_segment(aes(x = -3.75E-03, y = 0, xend = -3.75E-03, yend = -4.6011),
size = 0.6, linetype = "dotted", colour = "black") +
geom_segment(aes(x = -0.0046736, y = 0, xend = -0.0046736, yend = -4.6011),
size = 0.6, linetype = "dotted", colour = "black") +
annotate("text", x = 0, y= -0.94, label = "f'", size = fontsize, family = "sans")
The resulted graph is
How can I send all the text in the figure to the same font and size?
EDIT
The issue is that the font size in the theme is usually specified in pt (points) while when specifying size inside of the geoms or annotate it is measured in mm. This means that the size of you axis.title is 20pt, while the size of your annotation is 12mm or approx. 34pt. Therefore, if you want the same font sizes you have to do a conversion for which ggplot2 offers a helper constant .pt.
If you want the annotation to have the same size as e.g. the axis.title you have to set the fontsize in annotate equal to 20 / .pt.
library(ggplot2)
family <- "sans"
fontsize <- 20 / .pt
ggplot(mtcars, aes(hp, mpg)) + theme_void() +
geom_point(aes(color = factor(cyl))) +
annotate("text", x = 0, y= -0.94, label = "f'", size = fontsize, family = "sans") +
geom_text(data = data.frame(hp = 100, mpg = -.94), aes(label = "f'"), size = fontsize) +
theme(legend.position = "bottom",
legend.box.background = element_rect(colour = "black", size = 0.5),
axis.title.y = element_text(size=20, colour="black", angle = 90, vjust = -22, hjust = 0.55),
axis.title.x = element_text(size=20, colour="black", vjust = 7, hjust = 0.6),
axis.text.y = element_blank(), axis.text.x = element_blank(),
legend.text=element_text(size=20), legend.box.margin = margin(10,10,10,10),
legend.spacing.x = unit(0, 'cm'))
I searched for many questions already answered, but none of them can help me.
This is my code:
ggplot(z, aes(x=`Fecha de muestreo`, y = Valor, color = `Nombre de la estación`)) +
geom_line(size = 0.4) +
geom_point(size=0.5) +
scale_x_date(date_labels = "%b", breaks = "1 month", limits = c(as.Date("2019-1-1"), as.Date("2019-12-20"))) +
scale_y_continuous("pH (unidades de pH)", limits = c(3, 11), breaks = c(3:11)) +
geom_hline(aes(yintercept = 6.5, linetype = "ECA cat.3 inf D1 y D2 : 6.5"), colour = "Blue", size = 0.4)+
geom_hline(aes(yintercept = 8.4, linetype = "ECA cat.3 sup. D1 : 8.4"), colour = "Green", size = 0.4)+
geom_hline(aes(yintercept = 8.5, linetype = "ECA cat.3 sup. D2 : 8.5"), colour = "red", size = 0.4)+
scale_linetype_manual(name = NULL, values = c(1, 1, 1), # values = tipo de lineas
guide = guide_legend(override.aes = list(color = c("blue", "Green", "Red")))) +
theme(axis.text=element_text(size=6),
legend.margin = unit(-0.2, "cm"),
axis.title=element_text(size=7,face="bold")) +
theme(legend.text=element_text(size=6),
legend.title = element_blank(),
legend.spacing.x = unit(.2, 'cm')) +
theme(legend.key = element_rect(fill = "white")) +
theme(panel.background = element_rect(fill = "white")) +
theme(panel.grid = element_line(colour= "gray"))
This is the graph:
I need to change the order of the legends: the "ECAS" should be at the bottom.
How can I do it?
This should be possible using the order term of guides(). For example:
library(ggplot2)
a <- ggplot(mtcars, aes(wt, mpg,
color = as.character(cyl),
fill = as.character(gear))) +
geom_point(size = 5, shape = 21, stroke = 2) +
scale_color_discrete(name = "cyl as color") +
scale_fill_discrete(name = "gear as fill")
a
To reverse the order, we can add: (note, ordering seems to be from the bottom up)
a +
guides(color = guide_legend(order = 0),
fill = guide_legend(order = 1))
I have a boxplot with same color boxes. I would like to name the axis with labs(), and add a small symbol in the color of the plot's boxes.
sector <- c("HY", "HY (ETFs)", "IG", "IG (ETFs)", "Loan", "Equities")
YTD_percent <- c(-0.2, 0.5, 0.05, 0.2, -0.1, 0.1)
data1 <- data.frame(sector, YTD_percent)
ggplot(data1, aes(sector, YTD_percent)) +
geom_bar(stat="identity", color="#00669C", fill="#00669C", width=0.7) +
labs(x = NULL, y = NULL, title = "Plot Title") +
coord_flip() +
theme_bw() +
theme(axis.text.y=element_blank(),
axis.ticks.y = element_blank()
)+
guides(fill = guide_legend(keywidth = 1, keyheight = 1)) +
scale_y_continuous(breaks=pretty_breaks(), expand = c(.05, .05), labels=percent) +
geom_text(aes(label = sector , y = YTD_percent), hjust = ifelse(data_ff$YTD_percent >= 0, -0.1, 1.1), size=6)
This is what the axis should look like in the end:
You could include fill in your aesthetics mapping and tune the legend a little bit:
library(ggplot2)
library(scales)
ggplot(data1, aes(sector, YTD_percent, fill = "YTD (in %)")) +
geom_bar(stat="identity", color="#00669C", width=0.7) +
scale_fill_manual(values = c("YTD (in %)" = "#00669C")) +
labs(x = NULL, y = NULL, title = "Plot Title", fill = "") +
coord_flip() +
theme_bw() +
theme(axis.text.y=element_blank(),
axis.ticks.y = element_blank(),
legend.position = "bottom"
)+
guides(fill = guide_legend(keywidth = 1, keyheight = 1)) +
scale_y_continuous(breaks=pretty_breaks(), expand = c(.05, .05), labels=percent) +
geom_text(aes(label = sector , y = YTD_percent), hjust = ifelse(data1$YTD_percent >= 0, -0.1, 1.1), size=6)