Change Linetype of geom_hline Without Changing the Legend Title - ggplot2

I am pretty new to R so please excuse me if I got this wrong.
I am trying to change a dashed linetype of a geom_hline in ggplot2 to a solid one.
Any help is much appreciated!
Usually this is easily done with changing linetype = "dashed" to linetype = "solid". But since I have to use linetype for the text in my legend I cannot do this. Also adding linetype = "solid" after color results in
Error in [[<-.data.frame (tmp, i, value = c("blue", "blue")) :
Replacement has 2 lines, Data has 1`
This is the code that I am trying to adjust:
geom_hline(aes(yintercept = 5, linetype = "Title 1"), colour = "blue") +
geom_hline(aes(yintercept = 2, linetype = "Title 2"), colour = "blue") +
scale_linetype_manual(name = "", values = c(2, 2),
guide = guide_legend(override.aes = list(color = c("blue", "blue"))))

Related

Automatically assigning p-value position in ggplot loop

I am running an mapply loop on a huge set of data to graph 13 parameters for 19 groups. This is working great except the p-value position. Due to the data varying for each plot I cannot assign position using label.y = 125 for example, in some plots it is in the middle of the bar/error bar. However, I can't assign it higher without having it way to high on other graphs. Is there a way to adjust to the data and error bars?
This is my graphing function and like I said the graph is great, except p-value position. Specifically, the stat compare means anova line.
ANOVA_plotter <- function(Variable, treatment, Grouping, df){
Inputdf <- df %>%
filter(Media == treatment, Group == Grouping) %>%
ggplot(aes_(x = ~ID, y = as.name(Variable))) +
geom_bar(aes(fill = ANOVA_Status), stat = "summary", fun = "mean", width = 0.9) +
stat_summary(geom = "errorbar", fun.data = "mean_sdl", fun.args = list(mult = 1), size = 1) +
labs(title = paste(Variable, "in", treatment, "in Group", Grouping, sep = " ")) +
theme(legend.position = "none",axis.title.x=element_blank(), axis.text = element_text(face="bold", size = 18 ), axis.text.x = element_text(angle = 45, hjust = 1)) +
stat_summary(geom = "errorbar", fun.data = "mean_sdl", fun.args = list(mult = 1), width = 0.2) +
stat_compare_means(method = "anova", label.y = 125) +
stat_compare_means(label = "p.signif", method = "t.test", paired = FALSE, ref.group = "Control")
}
I get graphs that look like this
(https://i.stack.imgur.com/hV9Ad.jpg)
But I can't assign it to label.y = 200 because of plots like this
(https://i.stack.imgur.com/uStez.jpg)

Tradingview Pine script -- Line 15: Syntax error at input ')'

I tried everything I know but it still gives me this error. Help please. Thank you in advance.
(Stack is not letting me post this with the following error with the message "It looks like your post is mostly code; please add some more details." So I'm repeating this message several times -- please ignore. "It looks like your post is mostly code; please add some more details.")
//#version=5
indicator(title='Hull MA Multi-Time Frame', shorttitle='HMA MTF', overlay=false)
PDS = input.int(7, title = "Periods (PDS)", minval=2)
SRC = input(close, title='Source')
HMA1_res = input.timeframe('15', title = "HMA1")
HMA2_res = input.timeframe('30', title = "HMA2")
HMA3_res = input.timeframe('60', title = "HMA3")
HMA4_res = input.timeframe('240', title = "HMA4")
f_HMA(_PDS, _SRC) =>
_xHMA = ta.wma(2 * ta.wma(_SRC, _PDS / 2) - ta.wma(_SRC, _PDS), math.round(math.sqrt(_PDS))))
_xHMA_is_up = _HMA >= _xHMA[1]
[_xHMA, _xHMA_is_up]
[xHMA1, _xHMA1_is_up] = request.security(syminfo.tickerid, HMA1_res, f_HMA(PDS, SRC))
HMA1_color = xHMA1_is_up ? color.green : color.red
HMA1_text = f_res_to_string(HMA1_res) + "=" + str.tostring(xHMA1, "#.00")
plot(xHMA1, color = HMA1_color, linewidth=1)
var label HMA1_label = label.new(x = na, y = na, color = #00000000, size = size.normal, style = label.style_label_left)
label.set_xy(HMA1_label, x = bar_index, y = xHMA1)
label.set_textcolor(HMA1_label, HMA1_color)
label.set_text(HMA1_label, HMA1_text)
[xHMA2, _xHMA2_is_up] = request.security(syminfo.tickerid, HMA2_res, f_HMA(PDS, SRC))
HMA2_color = xHMA2_is_up ? color.green : color.red
HMA2_text = f_res_to_string(HMA2_res) + "=" + str.tostring(xHMA2, "#.00")
plot(xHMA2, color = HMA2_color, linewidth=1)
var label HMA2_label = label.new(x = na, y = na, color = #00000000, size = size.normal, style = label.style_label_left)
label.set_xy(HMA2_label, x = bar_index, y = xHMA2)
label.set_textcolor(HMA2_label, HMA2_color)
label.set_text(HMA2_label, HMA2_text)
[xHMA3, _xHMA3_is_up] = request.security(syminfo.tickerid, HMA3_res, f_HMA(PDS, SRC))
HMA3_color = xHMA3_is_up ? color.green : color.red
HMA3_text = f_res_to_string(HMA3_res) + "=" + str.tostring(xHMA3, "#.00")
plot(xHMA3, color = HMA3_color, linewidth=1)
var label HMA3_label = label.new(x = na, y = na, color = #00000000, size = size.normal, style = label.style_label_left)
label.set_xy(HMA3_label, x = bar_index, y = xHMA3)
label.set_textcolor(HMA3_label, HMA3_color)
label.set_text(HMA3_label, HMA3_text)
[xHMA4, _xHMA4_is_up] = request.security(syminfo.tickerid, HMA4_res, f_HMA(PDS, SRC))
HMA4_color = xHMA4_is_up ? color.green : color.red
HMA4_text = f_res_to_string(HMA4_res) + "=" + str.tostring(xHMA4, "#.00")
plot(xHMA4, color = HMA4_color, linewidth=1)
var label HMA4_label = label.new(x = na, y = na, color = #00000000, size = size.normal, style = label.style_label_left)
label.set_xy(HMA4_label, x = bar_index, y = xHMA4)
label.set_textcolor(HMA4_label, HMA4_color)
label.set_text(HMA4_label, HMA4_text)
if you put your cursor to the right of the () it will highlight the other end if theres one to close it try removing the extra one
Picture
Code =D
I was in a good mood you were pretty close.. pretty sure its what you were trying to get.. only had to add a x to the _(x)HMA >= in the function
and remove the f_string function thing because the timeframes are already strings
and the extra )
Looks interesting whats the idea behind this if you dont mind me asking?

Add linetype to geom_segment legend

Aim: add the linetypes of the segments to the legend, as well as the colour.
Problem: only the colour is showing.
Data:
m = as.data.frame(matrix(c(1:10), ncol = 2, nrow = 10))
Plot:
ggplot(m, aes(v1,v2)) + geom_segment(aes(x = 0, xend = 9.75, y = 10, yend = 10, colour = "PEL"), linetype = "dotted") + geom_segment(aes(x = 0, xend = 9.75, y = 5, yend = 5, colour = "AL1"), linetype = "longdash") + geom_segment(aes(x = 0, xend = 9.75, y = 2, yend = 2, colour = "ISQG"), linetype = "solid") + scale_colour_manual("legend", values = c("PEL" = "black", "AL1" = "blue", "ISQG" = "purple"), guide = guide_legend(override.aes = list(alpha = 1))) + theme(legend.position = "bottom")
I've tried adding scale_linetype_manual(values = c("PEL" = "dotted", "AL1" = "longdash", "ISQG" = "solid") but nothing changes.
This answer is similar, Legend linetype in ggplot but I couldn't figure out how to make it work with geom_segment
Thank you in advance
The most ggplot-esque way of doing this, is to include a linetype variable as part of mapping in the aes() functions. You must then ensure that both the linetype and colour scales have the same titles, breaks, limits, labels etc.
Alternatively, you can also include the linetype in the override.aes part of guide_legend().
library(ggplot2)
ggplot() +
geom_segment(
aes(x = 0, xend = 9.75, y = 10, yend = 10, colour = "PEL", linetype ="PEL"),
) +
geom_segment(
aes(x = 0, xend = 9.75, y = 5, yend = 5, colour = "AL1", linetype ="AL1"),
) +
geom_segment(
aes(x = 0, xend = 9.75, y = 2, yend = 2, colour = "ISQG", linetype = "ISQG"),
) +
scale_colour_manual(
"legend",
values = c("PEL" = "black", "AL1" = "blue", "ISQG" = "purple"),
) +
scale_linetype_manual(
"legend",
values = c("PEL" = "dotted", "AL1" = "longdash", "ISQG" = "solid"),
) +
theme(legend.position = "bottom")
Created on 2022-05-19 by the reprex package (v2.0.1)

How to label line plot in a combined bar & line plot in ggplot r?

I want to combine a bar and line plot and label line plot.
This is what I got: plot
this is my code:
df %>%
ggplot(aes(reorder(NAME, pval),y = pval)) +
geom_col(aes(x = NAME, y = pval), size = 1, color = "royalblue", fill = "white") +
geom_line(aes(x = NAME, y = 10*Ratio), size = 1.5, color="#c4271b", group = 1) + geom_text(aes(label = Ratio))+coord_flip()
I want to label line plot, but the bar plot gets the labels?
My second question:
How to rearrange the y-axis from the largest -log(pvalue) to lowest one?
Any help will be really appreciated!
try set the x and y aes in geom_text() with the same in geom_line()
geom_text(aes(x = NAME, y = 10*Ratio, label = Ratio))

The second description of the x-axis in ggplot2?

I am wondering if there is a way to add the second description of x-axis in ggplot2 as follows: Here "the second description" refers "Sample A / Sample B / two arrows" colored in red (shown in the figure).
Please click for the figure!
Of course, I can just put the "second description" using PowerPoint as I did, but I just wonder if it is possible to add it using ggplot2.
Here is the code for the background plot.
library(ggplot2)
library(ggridges)
x <- data.frame(v1=rnorm(100, mean = -2, sd = 0.022),
v2=rnorm(100, mean = -1, sd = 0.022),
v3=rnorm(100, mean = 0, sd = 0.022),
v4=rnorm(100, mean = 1, sd = 0.022),
v5=rnorm(100, mean = 2, sd = 0.022),
v6=rnorm(100, mean = 3, sd = 0.022),
v7=rnorm(100, mean = 4, sd = 0.022))
colnames(x) <- c("A",
"B",
"C",
"D",
"E",
"F",
"G")
head(x)
# Manipulate the data
library(reshape2)
data <- melt(x)
head(data)
# Generating plot
colors <- rainbow(7)
ggplot(data, aes(x = value, y = variable)) +
geom_density_ridges(aes(fill = variable), alpha=0.6, bandwidth=0.1) +
scale_fill_manual(values = colors)+
theme(axis.title = element_text(size = 12),
axis.text = element_text(size = 10),
legend.text = element_text(size = 12),
plot.title = element_text(size = 17, face = "bold",
margin = margin(b=10), hjust = 0.5),
panel.spacing = unit(0.1, "lines"),
legend.position="none") +
geom_vline(xintercept = 0, linetype="dotted") +
geom_vline(xintercept = 2, linetype="dotted",
color = "red", size=1.2) +
xlab("") +
ylab("Groups") +
labs(title = 'Density plot of each group')
Thank you in advance!
I'm not 100% sure this is what you mean, but you can add text on the x-axis using the following in labs:
labs(x="← Sample A Sample B →")
I got the arrows from unicode here: http://xahlee.info/comp/unicode_arrows.html
There are bigger arrows in the link if needed.
EDIT:
Here's your code adapted with the new labels in red font:
ggplot(data, aes(x = value, y = variable)) +
geom_density_ridges(aes(fill = variable), alpha=0.6, bandwidth=0.1) +
scale_fill_manual(values = colors)+
theme(axis.title = element_text(size = 12),
axis.text = element_text(size = 10),
legend.text = element_text(size = 12),
plot.title = element_text(size = 17, face = "bold",
margin = margin(b=10), hjust = 0.5),
panel.spacing = unit(0.1, "lines"),
legend.position="none") +
geom_vline(xintercept = 0, linetype="dotted") +
geom_vline(xintercept = 2, linetype="dotted",
color = "red", size=1.2) +
xlab("🢀 Sample A Sample B 🢂") +
theme(axis.title.x = element_text(size=40,colour = "red")) +
ylab("Groups") +
labs(title = 'Density plot of each group')
You can also push the labels further apart by adding extra spaces. Bring them closer together with fewer spaces.