Change geom_text label angle in plotly - r

I have the following chart I have built using ggplot + ggplotly.
I am trying to add labels to the red (median) and blue (percentile 90%) vertical lines without luck.
Please advise how should I fix it.
The code I have used:
p1 <- ggplot(users_d_total %>% filter(isSame, D_rank == 2), aes(x = D, fill = as.factor(train_user_id))) +
geom_density(alpha = .3) +
labs(title = paste0("Without Normalization Analysis [K = 2]")) +
scale_fill_discrete(name = "Users") +
scale_x_continuous(breaks = by_two) +
geom_vline(aes(xintercept = median(D)), col = 'red', linetype = 1, size = 1) +
geom_text(aes(x = median(D), y = 1, label = "Median"), hjust = 1, angle = 90, colour= "red") +
geom_vline(aes(xintercept = quantile(D, probs = .9)), col = 'blue', linetype = 1, size = 1) +
geom_text(aes(x = quantile(D, probs = .9), y = 1, label = "90th Percentile"), hjust = 1, angle = 90, colour = "blue") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(p1)
I want the text to be vertical but using the answer from How to add legend for vertical lines in ggplot? didn't help me.

Related

geom_bar + coord_polar labels with lines

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()

Trying to add legend using geom_abline

I have a data frame, saved as df, with two columns of points that I would like to plot. In addition, I would like to plot two lines on the plot and would like to have a legend for these lines. Here is my code:
ggplot(df, aes(x = x, y = y)) +
geom_point(color = "black", shape = 16, alpha = 1) +
scale_x_continuous(name = "x", limits = c(-5, 5)) +
scale_y_continuous(name = "y", limits = c(-5, 5)) +
geom_abline(intercept = 0, slope = 4/3, linetype = "dashed",
color = "gray40", size = 1, aes(colour = "XNULL")) +
geom_abline(intercept = 0, slope = 0, linetype = "dotted",
color = "gray40", size = 1, aes(colour = "YNULL")) +
scale_color_manual(name = "", values = c("XNULL" = "red", "YNULL" = "blue")) +
theme(panel.background = element_rect(fill = "white"),
panel.border = element_rect(colour = "black", fill = NA, size = 1),
legend.position = "bottom")
However, when I run this, no legend comes up (I would like to have the legend on the bottom). Any suggestions as to what I am doing wrong? I am new at using ggplot2, and none of the solutions I looked up on other forums helped.
You can try creating another data.frame to contain information about your ablines:
df = data.frame(x=runif(10),y=runif(10))
df2 = data.frame(intercept=0,slope=c(4/3,0),type=c("XNULL","YNULL"))
Then we can call geom_abline specifying the aes so that we can use
ggplot(df, aes(x = x, y = y)) +
geom_point(color = "black", shape = 16, alpha = 1) +
scale_x_continuous(name = "x", limits = c(-5, 5)) +
scale_y_continuous(name = "y", limits = c(-5, 5)) +
geom_abline(data=df2,aes(intercept=intercept,slope=slope,
linetype=type,col=type),size = 1) +
scale_color_manual(name = "", values = c("XNULL" = "red", "YNULL" = "blue")) +
scale_linetype_manual(name = "", values = c("XNULL" = "dashed", "YNULL" = "dotted")) +
theme(panel.background = element_rect(fill = "white"),
panel.border = element_rect(colour = "black", fill = NA, size = 1),
legend.position = "bottom")
From the documentation:
These geoms act slightly differently from other geoms. You can supply
the parameters in two ways: either as arguments to the layer function,
or via aesthetics. If you use arguments, e.g. geom_abline(intercept =
0, slope = 1), then behind the scenes the geom makes a new data frame
containing just the data you've supplied.
Apparently, you have to specify intercept and slope in aes, so that it works.
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
coord_cartesian(xlim = c(0,10), ylim = c(0,10)) +
geom_abline(aes(intercept = 0, slope = 0, color = "X"), linetype = "dotted") +
geom_abline(aes(intercept = 0, slope = 4/3, color = "Y"),linetype = "dashed") +
scale_color_manual(values = c(X = 'grey', Y = 'black'))
Created on 2020-02-12 by the reprex package (v0.3.0)

Adding labels to this ggridge plot

I can't see, to figure out how to add labels to this plot:
ggplot(input_cleaned, aes(x =DAYS_TO_FA, y = fct_rev(DATE_TEXT), group = fct_rev(DATE_TEXT))) +
geom_density_ridges2(stat="binline", bins = 75, scale = 0.95, draw_baseline = FALSE) +
labs(title = 'Monthly Plots of Time to First Nose Pickin', y='Month Tracked', x = 'Days to First Pickin Action') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,130,5)) +
geom_segment(aes(x=50, xend = 50, y=1,yend=5),
linetype = "dashed", size = 1.5,
color = "black") +
geom_segment(aes(x=75, xend = 75, y=5,yend=30),
linetype = "dashed", size = 1.5,
color = "black")
which produces:
I have tried this:
ggplot(input_cleaned, aes(x =DAYS_TO_FA, y = fct_rev(DATE_TEXT), group = fct_rev(DATE_TEXT))) +
geom_density_ridges2(stat="binline", bins = 75, scale = 0.95, draw_baseline = FALSE) +
geom_text(stat = "bin",
aes(y = fct_rev(input_cleaned$DATE_TEXT) + 0.95*(..count../max(..count..)), label = ifelse(..count..>0, ..count.., "")),
vjust = 1.4, size = 3, color = "white", binwidth = 1) +
labs(title = 'Monthly Plots of Time to First Nose Pickin', y='Month Tracked', x = 'Days to First Pickin Action') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,130,5)) +
geom_segment(aes(x=50, xend = 50, y=1,yend=5),
linetype = "dashed", size = 1.5,
color = "black") +
geom_segment(aes(x=75, xend = 75, y=5,yend=30),
linetype = "dashed", size = 1.5,
color = "black")
based on the example found here:
Visualization of Groups of Poisson random samples using ggridges
but I can't get it to work. Nothing changes.
I know it may not be a good idea for this graph, but I am interested in seeing how it looks and more or less learning how to apply it.

ggplot2: Highlight area depending on dnorm function

I want to highlight the area between a vertical line and a normal distributed function. I know how it works with discrete values, but the stat_function confuses me. The code looks something like this:
library(ggplot2)
n1 <- 5
ggplot(data.frame(x = c(-2, 2)), aes(x)) +
stat_function(fun = dnorm, args = list(sd = 1/sqrt(n1))) +
geom_vline(xintercept = 0.5, linetype = "dashed", color = "red", size = 1) +
geom_vline(xintercept = -0.5, linetype = "dashed", color = "red", size = 1) +
ylim(c(0, 1.5)) +
theme_light() +
geom_rect(aes(xmin = 0.5, xmax = Inf, ymax = Inf, ymin = 0), fill = "grey", alpha = .3)
I know I need to change ymax to the values of x > 0.5. The question is how?
EDIT:
I looked into the question which is supposed to be the same as mine. When I rewrite the code the way they did, the highlighting works but it doesn't give me a proper normal distribution anymore, as you can see here:
library(dplyr)
set.seed(123)
range <- seq(from = -2, to = 2, by = .01)
norm <- rnorm(range, sd = 1 / sqrt(n1))
df <- data_frame(x = density(norm)$x, y = density(norm)$y)
ggplot(data_frame(values = norm)) +
stat_density(aes(x = values), geom = "line") +
geom_vline(xintercept = 0.5, linetype = "dashed", color = "red", size = 1) +
geom_vline(xintercept = -0.5, linetype = "dashed", color = "red", size = 1) +
ylim(c(0, 1.5)) +
theme_light() +
geom_ribbon(data = filter(df, x > 0.5),
aes(x = x, ymax = y), ymin = 0, fill = "red", alpha = .5)
When I stick with stat_function and use geom_ribbon with subsetting as proposed in the very same question, it highlights buggy, as you can see here:
ggplot(data_frame(x = c(-2, 2)), aes(x)) +
stat_function(fun = dnorm, args = list(sd = 1/sqrt(n1))) +
geom_vline(xintercept = 0.5, linetype = "dashed", color = "red", size = 1) +
geom_vline(xintercept = -0.5, linetype = "dashed", color = "red", size = 1) +
ylim(c(0, 1.5)) +
theme_light() +
geom_ribbon(data = filter(df, x > 0.5),
aes(x = x, ymax = y), ymin = 0, fill = "red", alpha = .5)
Not satisfying yet.
Here is an approach:
library(ggplot2)
n1 <- 5
ggplot(data.frame(x = c(-2, 2)), aes(x)) +
stat_function(fun = dnorm, geom = "area", fill = "grey", alpha = 0.3, args = list(sd = 1/sqrt(n1)), xlim = c(-0.5,0.5)) +
stat_function(fun = dnorm, args = list(sd = 1/sqrt(n1))) +
geom_vline(xintercept = 0.5, linetype = "dashed", color = "red", size = 1) +
geom_vline(xintercept = -0.5, linetype = "dashed", color = "red", size = 1) +
ylim(c(0, 1.5)) +
theme_light()
in stat_function one can define different geom, just pick the ones that suits your needs.

Transparent masking in ggplot2

I'm interested in ways to only include panel grid lines right near the ribbon--I can do this manually, in a trivial example
library(ggplot2)
d1 <- data.frame(x = seq(0, 1, length.out = 200))
d1$y1 <- -3*(d1$x-.5)^2 + 1
d1$y2 <- -3*(d1$x-.5)^2 + 2
ggplot(d1) +
geom_ribbon(aes(x, ymin = y1, ymax = y2),
alpha = .25) +
geom_ribbon(aes(x, ymax = y1),
ymin = .25,
fill = "white") +
geom_ribbon(aes(x, ymin = y2),
ymax = 2,
fill = "white") +
scale_y_continuous(limits = c(.25, 2.0),
expand = c(0, 0))+
scale_x_continuous(limits = c(0, 1),
expand = c(0, 0))+
theme_bw() +
theme(panel.grid = element_line(linetype = 1, color = "black"))
is there some less hacky way to have a transparent mask for these gridlines, so they only appear underneath a ribbon?
If gridlines the same color as the background are acceptable, you can remove the actual gridlines, then use geom_hline() and geom_vline() to make your own "gridlines" that will show on ribbons but be invisible against the background
d1$y3 <- d1$x + 0.3
d1$y4 <- d1$x + 0.4
ggplot(d1) +
geom_ribbon(aes(x, ymin = y1, ymax = y2), alpha = 0.25) +
geom_ribbon(aes(x, ymin = y3, ymax = y4), alpha = 0.25, fill = "blue") +
# use geom_vline and geom_hline to plot "gridlines" on top of ribbons
geom_hline(yintercept = seq(0, 2, by = 0.25), colour = "white") +
geom_vline(xintercept = seq(0, 1, by = 0.25), colour = "white") +
scale_y_continuous(limits = c(.25, 2.0), expand = c(0, 0)) +
scale_x_continuous(limits = c(0, 1), expand = c(0, 0)) +
theme_bw() +
theme(panel.grid.minor = element_blank(), # remove actual gridlines
panel.grid.major = element_blank())
produces this:
This is still a workaround, and will only make gridlines that match the background color, but it is easy to use with a variety of plots, such as the situation you mentioned with multiple ribbons (I've added a second ribbon to demonstrate that this will work)

Resources