Related
I am trying to change the legend items of this plot.
My code is:
library(ggplot2)
library(HDInterval)
library(ggridges)
df <- data.frame(
density = c(rgamma(400, 2, 10), rgamma(400, 2.25, 9), rgamma(400, 5, 7)),
source = rep(c("source_1", "source_2", "source_3"),
each = 400))
ggplot(df, aes(x = density, color = source, linetype = source,
fill = after_stat(ifelse(quantile == 2, NA, color)))) +
geom_density_ridges_gradient(aes(y = 0), quantile_lines = TRUE, size=1.2,
quantile_fun = hdi, # vline_linetype = 0, scale = 1) +
labs(y = "Density", x = "Contribution") +
scale_linetype_cyclical(name = "Source", values = c("solid", "dotted", "longdash"),
labels = c("source1", "source2", "source3"),
guide = "legend") +
scale_fill_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
labels = c("source1", "source2", "source3"),
guide = "none", na.value = "transparent") +
scale_color_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
labels = c("source1", "source2", "source3"),
guide = "none") +
ylim(0, 8) + xlim(0, 1) +
theme(#legend.position=c(.85,.75),
legend.text = element_text(size=16), # item legend text font size
legend.title=element_text(size=18), # title font size
legend.key.height= unit(1, 'cm'),# box height
legend.key.width= unit(1, 'cm')) + # box width
guides(color = guide_legend(override.aes = list(fill = "white")))+
theme(axis.text.y = element_text(size = 12, vjust = 0.5),
axis.text.x = element_text(size = 12, vjust = 0.5),
axis.title.x = element_text(size = 14),
axis.title.y = element_text(size = 14))
I would like to show lines in the legend, instead of the boxes.
I would appreciate your help.
I have tried to understand ggridges deeply, however it is a bit different or rigid for some things.
Thanks in advance.
ggplot(df, aes(x = density, color = source, linetype = source,
fill = after_stat(ifelse(quantile == 2, NA, color)))) +
geom_density_ridges_gradient(aes(y = 0), size=1.2,
quantile_lines = TRUE, quantile_fun = hdi,
key_glyph = "path") +
...
https://ggplot2.tidyverse.org/reference/draw_key.html
I have a dataframe like this
id <- c(5738180,51845,167774,517814,1344920)
amount <- c(3.76765976,0.85195407,1.96821355,0.01464609,0.57378284)
outlier <- c("TRUE","FALSE","FALSE","FALSE","FALSE")
df.sample <- data.frame(id,amount,outlier)
I am trying to plot the points and add an id label to any point that is above the limit. In this case (id=5738180)
I am plotting like this
library(tidyverse)
library(ggplot2)
library(ggrepel) # help avoid overlapping text labels
library(gridExtra) # adds custom table inside ggplot
library(scales) # breaks and labels for axes and legends
library(ggthemes) # Adding desired ggplot themes
df.sample %>%
ggplot(aes(x = as.numeric(row.names(df.sample)),
y = amount, label = as.character(id))) +
geom_point(alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0),
aes(colour = (amount < 3)), size = 2) +
geom_hline(aes(yintercept = 3, linetype = "Limit"),
color = 'black', size = 1) +
geom_text(aes(y = 3,x = amount[4],
label = paste("Limit = ", round(3, 3)),
hjust = 0, vjust = 1.5)) +
geom_text_repel(data = subset(df.sample, outlier == 'TRUE'),
nudge_y = 0.75,
size = 4,
box.padding = 1.5,
point.padding = 0.5,
force = 100,
segment.size = 0.2,
segment.color = "grey50",
direction = "x") +
geom_label_repel(data = subset(df.sample, outlier == 'TRUE'),
nudge_y = 0.75,
size = 4,
box.padding = 0.5,
point.padding = 0.5,
force = 100,
segment.size = 0.2,
segment.color = "grey50",
direction = "x")
labs(title = "Outlier Detection",
y = "amount",
x = "") +
theme_few() +
theme(legend.position = "none",
axis.text = element_text(size = 10, face = "bold"),
axis.title = element_text(size = 10, face = "bold"),
plot.title = element_text(colour = "blue", hjust = 0.5,
size = 15, face = "bold"),
strip.text = element_text(size = 10, face = "bold")) +
scale_colour_manual(values = c("TRUE" = "green","FALSE" = "red"))
I am running into an error "Error: Aesthetics must be either length 1 or the same as the data (1): x"
Can someone point me in the right direction?
The issue is with geom_text_repel() and geom_label_repel(). You subset the data, which now only includes 1 row, but the aes() are inheriting from the original data which have 5 rows, hence the error. To fix this, subset the data outside of the ggplot() call, and change the aesthetics for it. You are also missing a + after geom_label_repel() and the result below modifies the nudge_y to nudge_x and removes the geom_text_repel().
outliers <- subset(df.sample, outlier == TRUE)
ggplot(data = df.sample,
aes(x = as.numeric(row.names(df.sample)),
y = amount,
label = as.character(id))) +
geom_point(alpha = 0.6,
position = position_jitter(w = 0.05, h = 0.0),
aes(colour = (amount < 3)),
size = 2) +
geom_hline(aes(yintercept = 3,
linetype = "Limit"),
color = 'black',
size = 1) +
geom_text(aes(y = 3,x = amount[4],
label = paste("Limit = ",
round(3, 3)),
hjust = 0,
vjust = 1.5)) +
geom_label_repel(data = outliers,
aes(x = as.numeric(rownames(outliers)),
y = amount,
label = amount),
nudge_x = 0.75,
size = 4,
box.padding = 0.5,
point.padding = 0.5,
force = 100,
segment.size = 0.2,
segment.color = "grey50",
direction = "x",
inherit.aes = F) +
labs(title = "Outlier Detection",
y = "amount",
x = "") +
theme_few() +
theme(legend.position = "none",
axis.text = element_text(size = 10, face = "bold"),
axis.title = element_text(size = 10, face = "bold"),
plot.title = element_text(colour = "blue", hjust = 0.5,
size = 15, face = "bold"),
strip.text = element_text(size = 10, face = "bold")) +
scale_colour_manual(values = c("TRUE" = "green","FALSE" = "red"))
Need to display the x-axis levels in neatly way without affecting the actual point numbers in the final output. As currently, I am getting x-axis in closely spaced which looks not good while I am showing in powerpoint
library("readxl")
my_data <-read_excel("central_high.xlsx") # Input file
str(my_data)
my_data = as.data.frame(my_data)
str(my_data)
my_data$var1 = NULL
f20 = as.data.frame(table(my_data$Year20))
f20$Var1 = as.Date(f20$Var1, "%Y-%m-%d")
f20$Var1 = format(f20$Var1, format="%m-%d")
f20$Cumulative_F20 = cumsum(f20$Freq) # cumulative calculation
f20
newcol_20 = c( my_data$Year19,
my_data$Year18, my_data$Year17,
my_data$Year16, my_data$Year15,
my_data$Year14, my_data$Year13,
my_data$Year12, my_data$Year11,
my_data$Year10, my_data$Year9,
my_data$Year8, my_data$Year7,
my_data$Year6, my_data$Year5,
my_data$Year4, my_data$Year3,
my_data$Year2, my_data$Year1)
str(newcol_20)
newdata_20 = data.frame(newcol_20)
str(newdata_20)
newdata_20$newcol_20 = as.Date(as.character(newdata_20$newcol_20), "%Y-%m-%d")
newdata_20$newcol_20 = format(newdata_20$newcol_20, format="%m-%d")
str(newdata_20)
newtable_20 = table(newdata_20$newcol_20)
newtable_20
newdf_20 = as.data.frame(newtable_20)
#newdf_20$Cumulative_20 = cumsum(newdf_20$Freq)/19 # cumulative calculation
newdf_20$Freq = newdf_20$Freq/19
newdf_20
newcol_05 = c( my_data$Year19,
my_data$Year18, my_data$Year17,
my_data$Year16)
str(newcol_05)
newdata_05 = data.frame(newcol_05)
str(newdata_05)
newdata_05$newcol_05 = as.Date(as.character(newdata_05$newcol_05), "%Y-%m-%d")
newdata_05$newcol_05 = format(newdata_05$newcol_05, format="%m-%d")
str(newdata_05)
newtable_05 = table(newdata_05$newcol_05)
newtable_05
newdf_05 = as.data.frame(newtable_05)
newdf_05$Cumulative_05 = cumsum(newdf_05$Freq)/4 # cumulative calculation
newdf_05$Freq = newdf_05$Freq/4
newdf_05
library(ggplot2)
library(ggpubr)
ggplot() +
geom_line(data = newdf_20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#111111"), size = 1.6) +
geom_line(data = newdf_05, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#999999"), size = 1.6) +
geom_line(data = f20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#CC79A7"), size = 1.6) +
geom_vline(xintercept = "03-25", color="gray", size=1)+
geom_vline(xintercept = "04-21", color="gray", size=1)+
labs(y = "Cumulative_Frequency", colour= "#000000", size = 16 )+
font("ylab", size = 15, color = "black", face = "bold.italic")+
font("legend.text",size = 10, face = "bold")+
font("legend.title",size = 15, face = "bold")+
theme(axis.line.x = element_line(size = 0.5, colour = "black"), # theme modification
axis.line.y = element_line(size = 0.5, colour = "black"),
#axis.text.x=element_blank(),axis.ticks.x=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.text.x = element_text(colour = "#000000", size = 7,
angle = 90, face ="bold" ),
axis.text.y = element_text(colour = "#000000", size = 12,
angle = 90, face ="bold" ))
Please modify the code and I also added the final output what I am getting need a little bit of modification in the code to get x-axis neatly
One option would be dodging the labels in x-axis:
library(ggplot2)
library(ggpubr)
ggplot() +
geom_line(data = newdf_20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#111111"), size = 1.6) +
geom_line(data = newdf_05, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#999999"), size = 1.6) +
geom_line(data = f20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#CC79A7"), size = 1.6) +
geom_vline(xintercept = "03-25", color="gray", size=1)+
geom_vline(xintercept = "04-21", color="gray", size=1)+
scale_x_discrete(guide = guide_axis(n.dodge=2))+
labs(y = "Cumulative_Frequency", colour= "#000000", size = 16 )+
font("ylab", size = 15, color = "black", face = "bold.italic")+
font("legend.text",size = 10, face = "bold")+
font("legend.title",size = 15, face = "bold")+
theme(axis.line.x = element_line(size = 0.5, colour = "black"), # theme modification
axis.line.y = element_line(size = 0.5, colour = "black"),
#axis.text.x=element_blank(),axis.ticks.x=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.text.x = element_text(colour = "#000000", size = 7,
angle = 90, face ="bold" ),
axis.text.y = element_text(colour = "#000000", size = 12,
angle = 90, face ="bold" ))
Output:
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 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)