I would like to plot geom_text() in a facet_wrap with scale = free.
I tried to use geom_blank() or, set each height on each graph, but it was not successful.
Would you possibly tell me how to plot geom_text() in the right bottom in each figure.
z_cor <- fit01_varsize2 %>%
filter(!variable1 == "intercept") %>%
group_by(variable1) %>%
# mutate(height = max(value_with) + .3 * sd(value_with)) %>%
ggplot(aes(x = value_without, y = value_with))+
geom_point(aes(color = value), shape = 1)+
# geom_blank(aes(x = 1, y = 1)) +
geom_text(
data = data.frame(variable1 = c("Agricultural_land", "Artificial_land", "Precipitation", "Protected_area",
"RiverLake", "Seashore", "Temperature", "Volcanic_area", "Wasteland"),
label = c("TRUE:FALSE = 694:316", "TRUE:FALSE = 698:312", "TRUE:FALSE = 733:277", "TRUE:FALSE = 864:146",
"TRUE:FALSE = 721:289", "TRUE:FALSE = 739:271", "TRUE:FALSE = 657:353", "TRUE:FALSE = 748:262", "TRUE:FALSE = 707:303")),
aes(x = 0.1, y = 0.1, label = label))+
geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
scale_color_manual(values = c("TRUE" = "salmon", "FALSE" = "steelblue"))+
# geom_smooth(method = "lm",colour= "deepskyblue3")+
# ggpubr::stat_cor(method="pearson", label.y.npc="top", label.x.npc = "center")+
facet_wrap(.~variable1, scales = "free")+
theme(strip.text.x = element_text(size = 20),
axis.title=element_text(size=16),
axis.line = element_line(colour="grey40"),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
legend.position = "bottom",
panel.background = element_rect(fill = "transparent",
colour = "transparent",
size = 0.5, linetype = "solid"),
plot.background = element_rect(fill = "transparent",
colour = "transparent"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
[![enter image description here][1]][1]
By setting the aes(x, y) parameters to positive or negative Inf inside geom_text, we can have text labels on the lower right bottom of each facet. The extra hjust and vjust adjust the position of the label so that they would be in the panel.
Here I use the diamonds dataset as an example, and the data for geom_text is called diamonds_label.
library(ggplot2)
diamonds_label <- data.frame(clarity = unique(diamonds$clarity), label = LETTERS[1:8])
ggplot(diamonds, aes(x, y)) +
geom_point() +
facet_wrap(.~clarity, scale = "free") +
geom_text(data = diamonds_label, aes(Inf, -Inf, label = label),
col = "red",
hjust = 1,
vjust = -1)
Created on 2022-05-10 by the reprex package (v2.0.1)
Related
I have a 100% stacked bar chart that displays 3 types of variable. I've set a example db so I could create a graph more easily.
I've already adjust the chart with the colors and information I need. But I'm not being able to independently position the labels. Here's the current code and output.
Code:
(empilhado<-ggplot(dfm, aes(y = Year, x = abs(value), fill = variable)) +
scale_x_continuous(sec.axis = sec_axis(trans = ~.*1, name="Trab."), expand=expansion(mult=c(0,0.05)))+
geom_col(data = rotulo, aes(y = Year, x=abs(trabalho), fill=NULL), width = .7, colour="black", lwd=0.1, position = "fill", orientation = "y") +
geom_label(data = rotulo, aes(y= Year, x = abs(trabalho), fill=NULL, label=paste(format(round(trabalho, digits=0), nsmall=0, decimal.mark=",", big.mark="."),
format(round(aprovado, digits=0), nsmall=0, decimal.mark=",", big.mark="."))
), color="black", size=4, position = position_fill(vjust=1.06)) +
geom_col(width = .7, colour="black", lwd=0.1, position = "fill", orientation = "y") +
geom_text(aes(label=format(round(value, digits=0), nsmall=0, decimal.mark=",", big.mark=".")),
size=4, color="white", position = position_fill(vjust=0.5)) +
theme(panel.grid.major = element_line(colour = "gray90",size=0.75), panel.grid.minor = element_line(colour = "gray90",size=0.75),
legend.position="top", axis.text.x = element_blank(), axis.ticks.x = element_blank(),
axis.title.x = element_blank(), panel.background = element_blank())+
scale_fill_manual(values = c("#000000","tomato","blue"))
Output:
How is it now? Position_fill(vjust=0.5), so all the labels are centered inside its respective bar.
What I want? To be able to set the position of the 'Marionete' label on the left(like a vjust=0 would do), keep the 'Pedido' label as is (in the center of the 'Pedido' stacked bar) and place the 'Fatura' label on the right (like a vjust=1 would do).
Thanks in advance!
One option to achieve your desired result would be to compute/set the positions for each label and the horizontal alignment manually instead of making use of position="fill":
Making use of some random mock data:
library(ggplot2)
library(dplyr)
dfm <- dfm %>%
group_by(Year) %>%
arrange(desc(variable)) %>%
mutate(
pct = value / sum(value),
x_label = case_when(
variable == "Marionete" ~ 0,
variable == "Pedido" ~ .5 * (cumsum(pct) + lag(cumsum(pct))),
TRUE ~ 1
),
hjust = case_when(
variable == "Marionete" ~ 0,
variable == "Pedido" ~ .5,
TRUE ~ 1
)
)
ggplot(dfm, aes(y = Year, x = abs(value), fill = variable)) +
scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 1, name = "Trab."), expand = expansion(mult = c(0, 0.05))) +
geom_col(width = .7, colour = "black", lwd = 0.1, position = "fill", orientation = "y") +
geom_text(aes(x = x_label, label = format(round(value, digits = 0), nsmall = 0, decimal.mark = ",", big.mark = "."), hjust = hjust),
size = 4, color = "white"
) +
theme(
panel.grid.major = element_line(colour = "gray90", size = 0.75), panel.grid.minor = element_line(colour = "gray90", size = 0.75),
legend.position = "top", axis.text.x = element_blank(), axis.ticks.x = element_blank(),
axis.title.x = element_blank(), panel.background = element_blank()
) +
scale_fill_manual(values = c("#000000", "tomato", "blue"))
DATA
set.seed(123)
dfm <- data.frame(
Year = rep(c(2006:2016), each = 3),
value = sample(1:100, 3 * 11, replace = TRUE),
variable = c("Fatura", "Pedido", "Marionete")
)
dfm$variable <- factor(dfm$variable, levels = c("Fatura", "Pedido", "Marionete"))
dfm$Year <- factor(dfm$Year)
I am trying to set perfectly black ticks and labels in ggplot2 but they are coming with a very slight grey tint. Here is an example:
library(tidyverse)
mpg %>% ggplot(aes(x = cty, y = cyl)) + geom_point() + theme_classic() +
theme(line = element_line(size = 4,color = "black",lineend = "square"),
text = element_text(color = "black", face = "bold",size = 24))
Plots this:
The color difference in the ticks is subtle but noticeable.
Try this approach with modifications on theme():
library(tidyverse)
mpg %>% ggplot(aes(x = cty, y = cyl)) + geom_point() + theme_classic() +
theme(axis.text = element_text(color = "black", face = "bold",size = 24),
line = element_line(size = 4,color = "black",lineend = "square"),
text = element_text(color = "black", face = "bold",size = 24),
axis.ticks = element_line(size = 4,color = "black",lineend = "square"))
Output:
I am having some troubles with the following piece of code:
ggplot(data = sb11.20194, aes(y = PROMLECTURACRITICA, x = año)) +
geom_boxplot(fill = "#3AAA35", color = "#3AAA35",outlier.color = "#95C11F",
outlier.size = 5) +
ylab("Puntajes promedio de Lectura Crítica") +
stat_boxplot(geom = "errorbar", colour = "#006633",
width = 0.6) +
stat_summary(geom = "crossbar", width=1.5, fatten=0,
color="white",
fun.data = function(x){ return(c(y=median(x),
ymin=median(x),
ymax=median(x))) }) +
theme(
panel.background = element_rect(fill = "white", colour = rgb(198,
198,
198,
maxColorValue = 255),
size = 1, linetype = "solid"),
panel.grid.minor = element_line(size = 0.1, linetype = 'dashed',
colour = rgb(198,198,198,
maxColorValue = 255)),
axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.text.y = element_text(family = "Montserrat"),
axis.title.y = element_text(family = "Montserrat")
) + geom_text(data = num, aes(label = num, y = num),
color = "#575756", hjust = -8,
family = "Montserrat")
which gives the following plot:
I would like to align the labels. Does anyone know how I might do this?
You didn't provide a sample data set, so I made some on my own. You can use two arguments in geom_text: nudge_x and hjust. You can use nudge_x in the way you're currently using hjust in your code. Then, we can use hjust to align the labels.
library(tidyverse)
set.seed(123)
# generate sample data and calculate quantiles
dat <- tibble(x = rnorm(1000))
dat_summary <- tibble(quants = quantile(dat$x))
ggplot(dat, aes(x = 1, y = x))+
geom_boxplot() +
geom_text(data = dat_summary, aes(x = 1.5, y = quants,
label = round(quants, 2)),
hjust = 'outward', nudge_x = 0.1)
This question already has answers here:
Remove 'a' from legend when using aesthetics and geom_text
(6 answers)
Closed 3 years ago.
How can I change the filling of each key in the legend to match the labels?
If I do it in geom_label_repel using show.legend = TRUE it doesn't look very good and it puts a letter "a" in place of dots.
Yellow is for injured players, blue for owned players, green for free players and red for hobbits players.
Here's the code used for the plot:
ggplot(fim, aes(Price, Average,
label = Player,
colour = color,
fill = color,
#alpha = ih,
group = Position
)) +
geom_smooth(method = "lm", se = FALSE, color = "lightblue", show.legend = FALSE) +
geom_point(aes(fill = factor(color))) + #
geom_label_repel(size = 3.25,
family = "Bahnschrift",
#fontface = 'bold',
label.size = NA,
segment.alpha = 0.5,
alpha = 0.9,
show.legend = FALSE,
#label.padding = unit(.22, 'lines'),
#hjust = 0,
#vjust = 0,
#label.r = 0,
box.padding = unit(0.20, "lines"),
point.padding = unit(0.20, "lines"),
#force = 4
) +
#nudge_y = 0.005,
#nudge_x = 0) +
scale_x_continuous(labels=function(y) format(y, big.mark = ".", scientific = FALSE)) +
ggtitle("Price and Average Points in LaLiga Fantasy",
paste("Top", nrow(fim), pos, "by market value with at least", minapps, "appearances, excluding Messi & Benzema")) +
labs(y="Average Points (Sofascore Rating System)",
x = "Price (Market Value in Euros)",
caption = "Sources: Biwenger, Jornada Perfecta plot by Weldata") +
scale_color_manual(values = c("Hobbits" = WT,
"Free" = WT,
"Injured" = BK,
"Owned" = WT)) +
scale_fill_manual(values = c("Hobbits" = cl,
"Free" = MF,
"Injured" = GK,
"Owned" = DF)) +
scale_alpha(0.1) +
dark_theme_gray() +
theme(plot.title = element_text(family = "Bahnschrift",
face = "bold",
size = 18,
colour = YL),
plot.background = element_rect(fill = BK),
panel.background = element_blank(),
panel.grid.major = element_line(color = "grey30", size = 0.2),
panel.grid.minor = element_line(color = "grey30", size = 0.2),
legend.title = element_blank(),
#legend.background = element_blank(),
axis.ticks = element_line(colour = "grey30"),
axis.title = element_text(family = "Bahnschrift", size = 14, colour = WT),
axis.text = element_text(size = 12, colour = "grey80", face = 'bold'),
legend.position = c(0.9, 0.2), #legend.position = "none",
plot.tag = element_text(),
plot.caption = element_text(color = YL, face = "italic")
)
You can use show.legend=F in geom_label_repel
library(ggrepel)
set.seed(42)
dat <- subset(mtcars, wt > 2.75 & wt < 3.45)
dat$car <- rownames(dat)
# your problem:
p <- ggplot(dat, aes(wt, mpg, label = car)) +
geom_point(aes(color=car))+
geom_label_repel(aes( fill=car)) +
labs(title = "geom_text_repel()")
p
#Answer:
p <- ggplot(dat, aes(wt, mpg, label = car)) +
geom_point(aes(color=car))+
geom_label_repel(aes( fill=car), show.legend = F) +
labs(title = "geom_text_repel()")
p
I created a vector of ordered names and tried to replace each panel title with the ordered one (e.g., Jessie with 1. Jessie, Marion with 2.Marion, etc.). But, I am getting NAs for each panel title instead. Any hints what is going wrong.
With the NAs
With the labeller commented out
list.top.35.names.ordered <- data.frame( cbind(order = c(1:35),list.top.35.names)) %>%
unite( name.new, c("order" ,"list.top.35.names"), sep = ".")
list.top.35.names.ordered <- list.top.35.names.ordered$name.new[1:35]
str(list.top.35.names.ordered)
chr [1:35] "1.Jessie" "2.Marion" "3.Jackie" "4.Alva" "5.Trinidad" "6.Ollie" ...
data.babyname.all %>%
ggplot( mapping = aes(x = year, y = perc, fill = sex)) +
geom_density(stat = "identity", position = "stack" , show.legend = F ) +
facet_wrap(~name, ncol= 7, nrow =5, labeller= as_labeller(list.top.35.names.ordered)) +
scale_fill_manual(values = c('#E1AEA1','#9ABACF')) +
geom_point(data = most.unisex.year.and.value, mapping = aes(x = year, y = perc),
size = 3,
fill = "white",
color = "black",
shape = 21) +
scale_y_continuous(breaks = c(0,.50,1), labels= c("0%", "50%","%100")) +
scale_x_continuous(breaks = c(1940, 1960, 1980,2000), labels= c('1940', "'60","'80",'2000')) +
geom_text(mapping = aes(x =x , y = y , label = label), check_overlap = F, na.rm = T, position = position_dodge(width=.9), size=3) +
theme_minimal() + #set theme
theme(
text = element_text(size = 10),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
plot.background = element_blank(),
axis.ticks.x = element_line(color = "black"),
axis.ticks.length =unit(.2,'cm'),
strip.text = element_text(size = 10, margin = margin(l=10, b = .1)))