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)
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)
I'm trying to follow the code suggested in this post, but to no avail.
Specifically, I would like the legend associated with the red vertical line to have a white background.
Some toy data:
df1 <- data.frame(correlation = c(rnorm(1000, 0, 0.6), rnorm(1000, 0, 0.1)), type = c(rep("Real", 1000), rep("Permuted", 1000)))
corSig <- 0.24542
My code:
ggplot(df1, aes(correlation, fill = type)) +
geom_density(alpha = .5) +
geom_vline(aes(xintercept = signif(corSig, 2), linetype = 'FDR = .05'), colour = 'red') +
theme(plot.title = element_text(hjust = .5),
plot.subtitle = element_text(hjust = .5),
legend.title = element_blank(),
legend.position = c(.8, .8),
panel.background = element_blank()) +
guides(linetype = guide_legend(override.aes = list(fill = "#000000"))) +
ggtitle("Gene Expression Correlation", subtitle = paste(nrow(datExpr), "genes,", ncol(datExpr), "bulk sections")) +
xlab("Correlation") +
ylab("Density")
There doesn't seem to be a consistent solution for this issue (for me, at least) but I've got a solution here:
library(tidyverse)
df1 <- data.frame(correlation = c(rnorm(1000, 0, 0.6), rnorm(1000, 0, 0.1)), type = c(rep("Real", 1000), rep("Permuted", 1000)))
corSig <- 0.24542
ggplot(df1, aes(correlation, fill = type)) +
geom_density(alpha = .5) +
geom_vline(aes(xintercept = signif(corSig, 2), linetype = 'FDR = .05'), colour = 'red') +
theme(
plot.title = element_text(hjust = .5),
plot.subtitle = element_text(hjust = .5),
legend.title = element_blank(),
legend.position = c(.8, .8),
panel.background = element_blank(),
legend.key = element_rect(colour = "transparent", fill = "transparent")) +
ggtitle("Gene Expression Correlation", subtitle = paste(nrow(df1), "genes,", ncol(df1), "bulk sections")) +
xlab("Correlation") +
ylab("Density")
Fixed Gene Expression Correlation Plot
In short, I removed ...guides(linetype = guide_legend(override.aes = list(fill = "#000000")))... and added ...legend.key = element_rect(colour = "transparent", fill = "transparent"))...
Hopefully that helps!
I have the following data set.
The 'Date' column (from 11/29/2017 to 1/16/2018) is in the correct order in my dataset. However, when I do a barplot using ggplot2, the dates on the x-axis are being reordered randomly. I have tried reorder() method and it doesn't work. I have attached the code and data I have right now. Your help is greatly appreciated. Thank you in advance!!
nest_men_women <- melt(mydata[, c('Date', 'Women', 'Men')], id.vars = 1)
view(nest_men_women)
ggplot(nest_men_women, aes(Date, value)) +
geom_bar(aes(fill = variable, position = position_dodge(width = 1),
stat = "identity", width = 0.8)) +
scale_fill_manual(values = c("Women"="lightpink", "Men" = "steelblue3"))+
scale_y_continuous(breaks = c(20,40,60,80,100))+
theme_light()+
theme(axis.text.x = element_text(angle = 90, hjust = 1),
legend.background = element_blank(), legend.justification = c(1,1),
axis.line.x.bottom = element_line(colour = "lightgrey", color = "lightgrey", linetype = "solid"),
axis.line.y.left = element_line(colour = "lightgrey", color = "lightgrey", linetype = "solid"),
axis.line.x.top = element_line(colour = "lightgrey", color = "lightgrey", linetype = "solid"),
axis.line.y.right = element_line(colour = "lightgrey", color = "lightgrey", linetype = "solid"))+
geom_text(data = nest_men_women,
aes(Date, label = value),
position = position_dodge(width = 0.8), colour="black",
check_overlap = TRUE, vjust = -1, hjust = 1, size = 3.8,
inherit.aes = TRUE)
I can create quality control charts with the qicharts2 package.
library(tidyverse)
library(qicharts2)
(plot1 <- qic(age,
data = tail(cabg, 100),
chart = 'i',
ylab = 'Years',
xlab = 'Patient #'
)
)
p1 <- plot1$data
Then I can customize the charts.
(plot2 <- ggplot(p1, aes(x, y)) +
geom_ribbon(ymin = p1$lcl, ymax = p1$ucl, fill = "black", alpha = 0.05) +
geom_line(color = "black", size = 1) +
geom_line(aes(x, cl)) +
geom_point(color = "black" , fill = "black", size = 2) +
geom_point(data = p1 %>% filter(sigma.signal == TRUE), color = "red", size = 2) +
ggtitle(label = NULL) +
labs(x = NULL, y = NULL) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_cartesian(ylim = c(0, 100)) +
theme_bw() +
theme(
text = element_text(size = 18),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6),
axis.text.y = NULL,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text.x = element_text(size = 14, color = "black", angle = 0))
)
Using the part argument, in my qichart, causes it to split at the specified part point(s).
(plot3 <- qic(age,
data = tail(cabg, 100),
chart = 'i',
part = c(70, 85),
ylab = 'Years',
xlab = 'Patient #'
)
)
p3 <- plot3$data
What do I need to add to my customized ggplot2 syntax, below, to get it to part in the same manner? What I've got does everything, EXCEPT, it doesn't part like in the syntax directly above.
(plot4 <- ggplot(p3, aes(x, y)) +
geom_ribbon(ymin = p3$lcl, ymax = p3$ucl, fill = "black", alpha = 0.05) +
geom_line(color = "black", size = 1) +
geom_line(aes(x, cl)) +
geom_point(color = "black" , fill = "black", size = 2) +
geom_point(data = p3 %>% filter(sigma.signal == TRUE), color = "red", size = 2) +
ggtitle(label = NULL) +
labs(x = NULL, y = NULL) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_cartesian(ylim = c(0, 100)) +
theme_bw() +
theme(
text = element_text(size = 18),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6),
axis.text.y = NULL,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text.x = element_text(size = 14, color = "black", angle = 0))
)
Is the following plot what you are looking for?
If so, what I used is group= in the aesthetics of geom_ribbon and geom_line
(plot4 <- ggplot(p3, aes(x, y)) +
geom_ribbon(aes(group=cut(p3$x,c(0,70,85,max(p3$x)))),ymin = p3$lcl, ymax = p3$ucl, fill = "black", alpha = 0.05) +
geom_line(color = "black", size = 1, aes(group=cut(p3$x,c(0,70,85,max(p3$x))))) +
geom_line(aes(x, cl, group=cut(p3$x,c(0,70,85,max(p3$x))))) +
geom_point(color = "black" , fill = "black", size = 2) +
geom_point(data = p3 %>% filter(sigma.signal == TRUE), color = "red", size = 2) +
ggtitle(label = NULL) +
labs(x = NULL, y = NULL) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_cartesian(ylim = c(0, 100)) +
theme_bw() +
theme(
text = element_text(size = 18),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6),
axis.text.y = NULL,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text.x = element_text(size = 14, color = "black", angle = 0)))
I'm creating an overlapping bar graph with the code below:
library(ggplot2)
library(reshape)
library(scales)
x = data.frame('DB' = c('Table4annot', 'UCSC_old', 'UCSC_new'), 'all_elements' = c(595632, 605914, 711073), 'unique_loci' = c(264978, 265979, 274936), 'start_codons' = c(10661, 10714, 22815))
melted = melt(x, id="DB")
p = ggplot(melted, aes(x = DB, y = value, fill = variable))
p = p + geom_bar(stat = "identity", position = "identity", alpha = 0.7, color = 'black', size = 0.4)
p = p + geom_label(aes(label = comma(value), fill = variable), vjust = -0.2, size = 3, show.legend = FALSE, label.padding = unit(0.2, 'lines'))
p = p + scale_x_discrete(limits = c('Table4annot', 'UCSC_old', 'UCSC_new'), labels=c("Table4annot", "UCSC our archive", "UCSC current"))
p = p + scale_y_continuous(labels = comma, expand = c(0, 0), limits = c(0, 750000), breaks = seq(0, 700000, 100000))
p = p + theme(panel.border = element_rect(colour = 'black', fill = NA),
panel.background = element_rect(fill = "white"),
panel.grid.major.y = element_line(colour = "grey40", size = 0.3),
panel.grid.minor.y = element_line(colour = "grey40", size = 0.1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
axis.title = element_blank(),
axis.ticks.x = element_blank(),
legend.title = element_blank(),
legend.position = 'top')
p = p + scale_fill_manual(labels = c('All annotated elements', 'Unique loci with annotation', 'Number of annotated start codons'), values = c(rgb(86, 180, 233, maxColorValue = 255, alpha = 1), rgb(240, 228, 66, maxColorValue = 255, alpha = 1), rgb(0, 0, 0, maxColorValue = 255, alpha = 1)))
p
Since I'm using geom_label(), I get these nice boxed labels filled with the color of the respective bar. But when using scale_fill_manual(), which I do because I didn't find a better way to customise the colours, the boxed labels get a white background. Anybody an idea why?
Any help would be greatly appreciated! Thanks :)
My sessionInfo() is a bit long, but if it's necessary I can add it later. Don't think it matters in this scenario.