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)
Related
I am trying to label the series of concentric circles below with the labels from C in the data frame
I am aware that I could use something like geom_text_repel but I cannot seem to get it to work.
In addition, I cannot seem to get rid of the tick marks on the upper left.
df <- data.frame(C=c(rep("The macro-environment",4),rep("The industry",4),rep("Competitors",4),rep("The organisation",4)))
ggplot(df, aes(factor(1), fill = C)) +
geom_bar(width = 1, colour = NA, show.legend = FALSE, alpha = .8) +
coord_polar() +
labs(
x = "",
y = ""
) +
scale_fill_manual(values = c("#289045", "#beddc7", "#d4dfe9", "#286291")) +
theme(axis.ticks.x = element_blank(),
axis.ticks.y = element_blank()) +
theme_minimal()
A second option would be to add your labels as curved labels using the geomtextpath package:
library(ggplot2)
library(geomtextpath)
ggplot(df, aes(factor(1), fill = C)) +
geom_bar(width = 1, colour = NA, show.legend = FALSE, alpha = .8) +
geom_textpath(aes(x = .5, label = C, group = C),
stat = "count", position = position_stack(vjust = .5),
vjust = 1
) +
coord_polar() +
labs(
x = "",
y = ""
) +
scale_fill_manual(values = c("#289045", "#beddc7", "#d4dfe9", "#286291")) +
theme_void()
You could do:
ggplot(df, aes(factor(1), fill = C)) +
geom_bar(width = 1, colour = NA, show.legend = FALSE, alpha = .8) +
geom_text(stat = 'count', aes(label = C), size = 6,
position = position_stack(vjust = 0.5),
vjust = c(0.5, 0.5, 0.5, 2)) +
coord_polar(start = pi) +
labs(x = NULL, y = NULL ) +
scale_fill_manual(values = c("#289045", "#beddc7", "#d4dfe9", "#286291")) +
theme_void()
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')
I have a graph like attached and want to change the 'colour' legend to be a symbol of a square or circle not a line.
ggplot(sharkanovadata, aes(x=mean, y=sd), colour="Sharks")+
geom_point(alpha=0.2)+
geom_point(data=birdanovadata, colour= "#D22B2B", alpha=0.1)+
geom_abline(data=sharkanovadata, mapping=aes(colour="Sharks", slope=0.15, intercept=3.33, linetype= "Genetic"), size=1.2)+
geom_abline(data=sharkanovadata, mapping= aes( colour="Sharks", slope= 0.22, intercept=4.12, linetype= "Imputed"), size=1.2) +
geom_abline(data=birdanovadata, mapping=aes(colour= "Birds", slope=0.17, intercept=0.31, linetype= "Genetic"), size=1.2)+
geom_abline(data=birdanovadata, mapping=aes(colour= "Birds", slope=0.30, intercept=0.53, linetype="Imputed"), size=1.2)+
scale_colour_manual(values= c("Sharks" = "black", "Birds" = "#B03A2E"))+
xlab("Mean ED") +
ylab("Standard deviation of ED") +
ggtitle("Imputed species have a greater linear relationship of standard devation and mean", (size=30))+
theme(panel.background = element_rect(fill = "white"))+
theme(panel.grid.major = element_line(size=0.4, colour= "grey"))+
scale_linetype_manual(values= c("Genetic"= "longdash", "Imputed" = "solid"))+
theme(legend.key.size = unit(1, 'cm')) +
theme(plot.title = element_text(face = "bold"))
As the legend symbols aka key glyphs are determined by the geoms one option would be to switch the key_glyph for one of your geom_ablines. to point which will add a point to each legend. Afterwards we can tweak the the color and linetype legends via the override.aes argument of guide_legend where I opted for a square shape.
As you provided no data I use some fake random data to mimic your real data:
set.seed(123)
birdanovadata <- sharkanovadata <- data.frame(
mean = runif(1000, 0, 150),
sd = runif(1000, 0, 30)
)
library(ggplot2)
ggplot(sharkanovadata, aes(x = mean, y = sd), colour = "Sharks") +
geom_point(alpha = 0.2) +
geom_point(data = birdanovadata, colour = "#D22B2B", alpha = 0.1) +
geom_abline(data = sharkanovadata, mapping = aes(colour = "Sharks", slope = 0.15, intercept = 3.33, linetype = "Genetic"), size = 1.2, key_glyph = "point") +
geom_abline(data = sharkanovadata, mapping = aes(colour = "Sharks", slope = 0.22, intercept = 4.12, linetype = "Imputed"), size = 1.2) +
geom_abline(data = birdanovadata, mapping = aes(colour = "Birds", slope = 0.17, intercept = 0.31, linetype = "Genetic"), size = 1.2) +
geom_abline(data = birdanovadata, mapping = aes(colour = "Birds", slope = 0.30, intercept = 0.53, linetype = "Imputed"), size = 1.2) +
scale_colour_manual(values = c("Sharks" = "black", "Birds" = "#B03A2E")) +
xlab("Mean ED") +
ylab("Standard deviation of ED") +
ggtitle("Imputed species have a greater linear relationship of standard devation and mean", (size <- 30)) +
theme(panel.background = element_rect(fill = "white")) +
theme(panel.grid.major = element_line(size = 0.4, colour = "grey")) +
scale_linetype_manual(values = c("Genetic" = "longdash", "Imputed" = "solid")) +
theme(legend.key.size = unit(1, "cm")) +
theme(plot.title = element_text(face = "bold")) +
guides(color = guide_legend(override.aes = list(linetype = "blank", size = 8, shape = 15)),
linetype = guide_legend(override.aes = list(shape = NA)))
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 want to add a text to my forest plot in R that has strip in it but the text is repeated on every strip . how can I add only the text to one strip or just on the plot? My code is as below:
My data is like:
Group Mean LowerLimit UpperLimit
M 1.172827 1.083498 1.268857
H 5.142589 4.333141 6.148088
h<-"XXXX"
p = ggplot(data=df4,
aes(x = Group,y = Mean, ymin = LowerLimit, ymax = UpperLimit),
+
ggtitle(PlotTitle)+
geom_point(aes(fill=Group, color=Group), shape=22, size=3)+
geom_pointrange(aes(col=Group), fatten = 3)+
geom_hline(aes(),yintercept =1, linetype="longdash")+
geom_text(aes(-1.5, 0.8, vjust =-0.5, hjust=-0.8, size=10),label=h,
check_overlap = T)+
geom_errorbar(aes(ymin=LowerLimit,
ymax=UpperLimit,col=Group),width=0.4,cex=1)+
facet_wrap(~Group,strip.position="left",nrow=2, scales= "free_y") +
theme(plot.title=element_text(aes(5, 5), hjust=0.5, size=14,face="bold"),
legend.position='none',
strip.text.y = element_text(size=10, hjust=0.5,vjust =1,lineheight=0.1, angle=270,face="bold"),
panel.background = element_blank(),
strip.background = element_rect(fill="green"),
plot.margin = margin(3.5,0.1,3.5, 0.5, "cm"))+
coord_flip()
p
In your parameters for geom_text try changing label=h to label = ''
library(ggplot2)
df4 <- data.frame(Group = c("M", "H"),
Mean = c(1.172827, 5.142589),
LowerLimit = c(1.083498, 4.333141),
UpperLimit = c(1.268857, 6.148088))
PlotTitle = "Insert plot title here"
p = ggplot(data=df4,
aes(x = Group,y = Mean, ymin = LowerLimit, ymax = UpperLimit)) +
ggtitle(PlotTitle) +
geom_point(aes(fill=Group, color=Group), shape=22, size=3) +
geom_pointrange(aes(col=Group), fatten = 3) +
geom_hline(aes(),yintercept =1, linetype="longdash") +
geom_text(aes(-1.5, 0.8, vjust =-0.5, hjust=-0.8, size=10),label='',
check_overlap = T) +
geom_errorbar(aes(ymin=LowerLimit,
ymax=UpperLimit,col=Group),width=0.4,cex=1) +
facet_wrap(~Group,strip.position="left",nrow=2, scales= "free_y") +
theme(plot.title=element_text(aes(5, 5), hjust=0.5, size=14,face="bold"),
legend.position='none',
strip.text.y = element_text(size=10, hjust=0.5,vjust =1,lineheight=0.1, angle=270,face="bold"),
panel.background = element_blank(),
strip.background = element_rect(fill="green"),
plot.margin = margin(3.5,0.1,3.5, 0.5, "cm")) +
coord_flip()
p
which yields this image: