I searched about my problem but apparently, there is no topic about this. Any help will be appreciated.
Suppose I have a plot generated by:
library(tidyverse)
df<- data.frame(PCP = c("BOB","FRED","ED","Other"),
closed = c(42,64,45,1812),
total= c(53,81,58,3188),
percentage = c(.7924,.7901,.7758,.5683),
row= c(1, 2, 3,4),
color =c("0099FF","#CCCCCC","#CCCCCC","#660033"),
color_fill = c("99","0","0","98"
))
col <- c(
"99" = "#0099FF",
"98" = "#660033",
"0" = "#CCCCCC"
)
df %>%
arrange(desc(percentage)) %>%
mutate(PCP = PCP,
closed = closed,
total = total,
percentage = percentage,
row = row,
color = color,
color_fill = color_fill) -> df1
ggplot(df1,aes(x=PCP, y = percentage,fill=color_fill, color = color)) +
geom_col() +
coord_flip() +
labs(x ="PCP Name",
y = "Percentage of Gap Closures",
title = "TOP 10 PCPs")+
scale_fill_manual(values = col)+
scale_color_manual(values = col) +
theme(legend.position = "none",
panel.grid = element_blank(),
panel.background = element_blank(),
text = element_text(size=15),
plot.caption = element_text(hjust = 0, face= "italic"),
axis.text.y = element_text(colour = col ))
How can I put 0.6 on the x-axis and FRED on the y-axis inside a colored box?
have a look here by using two annotate (note coord_filp() is updated)
ggplot(df1,aes(x=PCP, y = percentage,fill=color_fill, color = color)) +
geom_col() +
#coord_flip() +
labs(x ="PCP Name",
y = "Percentage of Gap Closures",
title = "TOP 10 PCPs")+
scale_fill_manual(values = col)+
scale_color_manual(values = col) +
theme(legend.position = "none",
panel.grid = element_blank(),
panel.background = element_blank(),
text = element_text(size=15),
plot.caption = element_text(hjust = 0, face= "italic"),
axis.text.y = element_text(colour = col )) +
annotate("rect", xmin = 2.8, xmax = 3.2, ymin = -0.091, ymax = -0.045, alpha=0.5, fill="red") +
annotate("rect", xmin = 0.2, xmax = 0.35, ymin = 0.58, ymax = 0.62, alpha=0.5, fill="red") +
coord_flip(xlim = c(1,4), ylim = c(0, 0.8), clip = "off")
If you insist on a "box", then perhaps use segment instead of rect for the annotate.
Related
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)
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 try to make panel hights in facet_grid() to fit the plot - not be equal for every panel.
I found this: Different y-Axis Labels facet_grid and sizes
Sadly it doesn't work :(
Here is my code, I'm using now
dane <- tibble(
"obszar" = c("Ludzie", "Ludzie", "Ludzie", "Ludzie",
"Myślenie", "Myślenie", "Myślenie",
"Zarządzanie", "Zarządzanie"),
"kompetencja" = c("Współdziałanie", "Komunikowanie i przekonywanie",
"Współdziałanie2", "Komunikowanie i przekonywanie2",
"Planowanie", "Wnioskowanie", "Innowacyjność",
"Budowanie zaangażowanego zespołu", "Inspirowanie i motywowanie"),
"wynik" = c(3,3,4,2,3,6,5,4,2),
"profil" = c(4,4,4,5,4,4,4,5,5)) %>%
mutate(luka = wynik - profil) %>%
arrange(obszar, kompetencja)
ggplot(dane, aes(x=kompetencja, y=wynik)) +
geom_segment(aes(x=kompetencja ,xend=kompetencja, y=1, yend=profil), color=kolor2) +
geom_point(aes(y = profil), pch = 19, size=3, color= kolor2) +
theme_bw() +
coord_flip(ylim=c(1,7)) +
theme(
legend.position = "none",
axis.text.x = element_text(size = 12),
axis.text.y = element_text(hjust = 1, size = 12),
panel.border = element_blank(),
axis.ticks=element_blank(),
axis.line.y = element_line(colour = "grey50"),
panel.grid.major.x = element_line(colour = "grey80", size = 0.2),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank()
) +
scale_y_continuous(breaks=seq(1,7,1)) +
labs(title = NULL,
caption = "źródło: opracowanie własne",
x = NULL,
y = NULL) +
annotate("rect", xmin = -Inf, ymin = 1, xmax = Inf, ymax = 2.5, fill = kolor2, alpha = 0.2) +
annotate("rect", xmin = -Inf, ymin = 5.5, xmax = Inf, ymax = 7.0, fill = kolor2, alpha = 0.2) +
facet_grid(obszar ~ ., scales = "free")
Output gives me 3 panels of hight equal to the highest one. I would like to get panels with heights adequate to the number of segments in the plot.
result of my code
I have the following code which produces the plot below:
ggplot(data = factor_output, aes(y=F.Difference, x=reorder(Action.Title, F.Difference), fill=Efficacy.Median)) +
coord_flip() +
scale_fill_gradient(low = "red", high = "green") +
geom_bar(stat = "identity") +
geom_point(aes(y=Factor1, colour="DarkRed"), size=3) +
geom_point(aes(y=Factor2, colour="DarkBlue"), size=3) +
scale_color_manual(labels = c("Factor 1", "Factor 2"), values = c("DarkRed", "DarkBlue")) +
labs(colour="Coefficient Value") +
annotate("rect", ymin = c(0.3, -0.3, -0.3), ymax = c(Inf, -Inf, 0.3),
xmin = -Inf, xmax = Inf,
alpha = 0.1, fill = c("green", "green", "orange")) +
theme(axis.title.x = element_text(colour = "DarkGreen", size = 18),
axis.title.y = element_text(colour = "DarkGreen", size = 18),
axis.text.x = element_text(size = 15),
legend.title= element_text(size = 15),
legend.text= element_text(size = 12),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.background = element_rect(fill="transparent"),
plot.title = element_text(colour = "DarkRed", size = 22, hjust = 0.5))
I know from other threads on stackoverflow that I need to start with empty ggplot() and then add annotate. I tried to do that and state the data and aes for each plot, but I got a lot of error messages.
Could you please let me know how I can amend the code above to send the three background areas to the back.
I used to get an error message, which reads: 'Error: Discrete value supplied to continuous scale.', when I tried to put annotate before the other plots. However, I managed to solve the problem by using scale_x_discrete() to specify to ggplot2 that I am using a discrete scale for the x axis.
ggplot(data = factor_output) +
scale_x_discrete() +
coord_flip() +
annotate(geom = "rect", ymin = c(0.3, -0.3, -0.3), ymax = c(Inf, -Inf, 0.3),
xmin = -Inf, xmax = Inf,
alpha = 0.4, fill = c("LightGreen", "LightGreen", "Orange")) +
scale_fill_gradient(low = "red", high = "green") +
scale_y_continuous(breaks=c(-0.75, -0.5, -0.3, 0, 0.3, 0.5, 0.75)) +
geom_bar(aes(y = F.Difference, x = reorder(Action.Title, F.Difference), fill=Efficacy.Median), stat = "identity") +
geom_point(aes(x = reorder(Action.Title, F.Difference), y = Factor1, colour="DarkRed"), size = 3) +
geom_point(aes(x = reorder(Action.Title, F.Difference), y = Factor2, colour="DarkBlue"), size = 3) +
scale_color_manual(labels = c("Factor 1", "Factor 2"), values = c("DarkRed", "DarkBlue")) +
labs(colour="Coefficient Value") +
ggtitle("XXX") +
xlab("XXX") +
ylab("XXX") +
theme_bw() +
theme(axis.title.x = element_text(colour = "DarkGreen", size = 18),
axis.title.y = element_text(colour = "DarkGreen", size = 18),
axis.text.x = element_text(size = 15),
legend.title = element_text(size = 15),
legend.text = element_text(size = 12),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.background = element_rect(fill = "transparent"),
plot.title = element_text(colour = "DarkRed", size = 22, hjust = 0.5))
Ggplot adds layers on top of each other, so if I understand your question correctly, you can solve this by putting +annotate() before +geom_point() and +geom_bar(), like this:
ggplot(data = factor_output, aes(y=F.Difference, x=reorder(Action.Title, F.Difference), fill=Efficacy.Median)) +
coord_flip() +
scale_fill_gradient(low = "red", high = "green") +
annotate("rect", ymin = c(0.3, -0.3, -0.3), ymax = c(Inf, -Inf, 0.3),
xmin = -Inf, xmax = Inf,
alpha = 0.1, fill = c("green", "green", "orange")) +
geom_bar(stat = "identity") +
geom_point(aes(y=Factor1, colour="DarkRed"), size=3) +
geom_point(aes(y=Factor2, colour="DarkBlue"), size=3) +
scale_color_manual(labels = c("Factor 1", "Factor 2"), values = c("DarkRed", "DarkBlue")) +
labs(colour="Coefficient Value") +
theme(axis.title.x = element_text(colour = "DarkGreen", size = 18),
axis.title.y = element_text(colour = "DarkGreen", size = 18),
axis.text.x = element_text(size = 15),
legend.title= element_text(size = 15),
legend.text= element_text(size = 12),
legend.position = c(0, 1),
legend.justification = c(0, 1),
legend.background = element_rect(fill="transparent"),
plot.title = element_text(colour = "DarkRed", size = 22, hjust = 0.5))
How to modify the "New infected", "Mortality" and "TDR level" text characteristics? I want to make it bigger and in bold. How to modify the "Scen1", "Scen2" and "Scen3" text?
I already looked into the R documentation but didn't find an answer in the particular case where barplot are used with "dodge".
Here is the code:
myd<- data.frame( var1 = rep(c("Newly infected","Mortality","TDR level"),each=3),
samp = rep(c("Scen1","Scen2","Scen3"),3),
V3 = c(3.5,2,NA,8,2,NA,4,5,NA)/1.5, V2 = c(3.5,2,NA,8,3,NA,4,4.3,NA), V1 = c(1.5,0.2,5,5,3,0.2,4,-5,2) )
# rshaping data to long form for ggplot2
library(reshape2)
meltd<- melt(myd, id.vars=1:2)
#meltd<- meltd[-which(is.na(meltd$value)),]
ggplot(meltd, aes(x = var1, y = value, fill = variable, label = paste0(round(value * 100, 1), "%"))) +
geom_bar(stat = "identity", position = position_dodge(width = 0.6), width = 0.5) +
facet_grid(samp ~ ., switch = "y", scales = "free_y", space = "free") +
coord_flip() +
scale_fill_manual("legend",values = c("V3" = "orange", "V2" = "red", "V1" = "blue", "Baseline" = "black")) +
geom_text(aes(y = value + 0.4 * sign(value)), position = position_dodge(width = 0.6)) +
theme_bw() +
theme(
legend.position = "none",
strip.placement = "outside",
#axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_text(colour="black"),
strip.text.y = element_text(size = 12, colour = "black"))+
ylab("Relative change (in %)")
Here is the plot:
You're pretty much there, I just added face, and size arguments:
axis.text.y = element_text(colour="black", face='bold',size=12),
strip.text.y = element_text(size = 12, colour = "black", face='bold'))+
ylab("Relative change (in %)")
Or to change the size of the value labels add size to geom_text:
geom_text(aes(y = value + 0.4 * sign(value)),
position = position_dodge(width = 0.6), size=6)
Full code:
ggplot(meltd, aes(x = var1, y = value, fill = variable, label = paste0(round(value * 100, 1), "%"))) +
geom_bar(stat = "identity", position = position_dodge(width = 0.6), width = 0.5) +
facet_grid(samp ~ ., switch = "y", scales = "free_y", space = "free") +
coord_flip() +
scale_fill_manual("legend",values = c("V3" = "orange", "V2" = "red", "V1" = "blue", "Baseline" = "black")) +
geom_text(aes(y = value + 0.4 * sign(value)), position = position_dodge(width = 0.6), size=6)+
theme_bw() +
theme(
legend.position = "none",
strip.placement = "outside",
#axis.title.x = element_blank(),
axis.title.y = element_blank(),
# added face and size arguments
axis.text.y = element_text(colour="black", face='bold',size=12),
strip.text.y = element_text(size = 12, colour = "black", face='bold'))+
ylab("Relative change (in %)")