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?
Related
The Y axis labels are offset
The Y axis labels from 100 to the top are aligned to the right, while from 90 to the bottom are aligned to the left. I've looked at many paramaters and I couldn't find one to be causing this. Also, I haven't found anyone else with this same issue.
Here's my code:
test <- ggplot(data,
aes(x=Month, y=Value, color=Name, group=Name, fill=Name))+
geom_line(size=3)+
geom_point(size=5)+
scale_color_manual(values=c("#FBE785","#0F5F00","#FFC300","#1BFFAA"))+
ylab ("")+
xlab ("")+
labs(caption = paste("Fonte: Fred e IBGE.")) +
scale_x_date(date_labels = "%b/%y", breaks = "6 month", expand=c(0,0))+
coord_cartesian(clip = "off")+
theme_minimal() +
guides(fill=guide_legend())+
theme(panel.background = element_rect(fill= "#122929",color = "#122929"),
plot.background = element_rect(fill = "#122929"),
panel.grid.major = element_line(color = "#4D4B55", size =0.1),
panel.grid.minor = element_line(color= "#4D4B55", size =0.1),
panel.grid = element_blank(),
axis.text.y = element_text(vjust = 1, hjust=-1),
axis.text.x = element_text(vjust = -1, hjust=0),
legend.title = element_blank(),
legend.position = "bottom",
legend.key.width = unit(1.5, "cm"),
plot.caption = element_text(family = "Abel",vjust = -1, hjust = 0,colour="#4D4B55", size= 30),
text = element_text(family = "Abel", color = "#4D4B55",size = 35),
plot.margin = margin(1,1,1.5,1.2, "cm"))
ggsave("./test.png", width = 21, height = 15, dpi = 300)
PS: Not sharing the data itself because I guess that's not where the problem is.
Thanks!
Your text is misaligned because of your axis.text.y argument. Change hjust to 1 and it will be properly aligned. I have provided a minimal reproducible example below.
library(tidyverse)
Month <- rep(x = month.abb, times = 10)
Value <- sample(x = 10:120, size = 120, replace = TRUE)
Name <- sample(x = LETTERS[1:4], size = 120, replace = TRUE)
data <- data.frame(Month, Value, Name)
ggplot(data, aes(x = Month, y = Value, color = Name, group = Name, fill = Name)) +
geom_line(size = 1) + geom_point(size = 2) +
theme(axis.text.y = element_text(vjust = 1, hjust = 1)) # <- problem here
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)
PLEASE HELP, something seems not to be working here,I previously ploted this figure and it was working fine, however, now it seems something is off.
The categories are no longer orderly and some of them appear twice...
...
ggplot(data = data_c, aes(x = reorder(Sektor, -Preis), y = Preis)) +
geom_bar(stat = "identity", width = data_c$Menge/26) +
geom_line(data = data_c, aes(group = 1, x = Sektor,
y = Preis, colour = "40 € pro MWh")) +
geom_line(data = data_d, aes(group = 1, x = sektoren2,
y = preise2, colour = "60 € pro MWh")) +
geom_label(aes(label = preise2a), nudge_y = 30, size = 4) +
geom_label(aes(label = twh2), nudge_y = 6, size = 4) +
geom_text(aes(label = euros), vjust = 1.5, colour = "white", size = 2) +
scale_y_continuous(labels = dollar_format(suffix = " €", prefix = "")) +
labs(title = "Merrit Order Curve Wasserstoff",
subtitle = "Mengen in TWh und Preise in Euro pro MWh",
x = "Sektoren",
y = "Preise in Euro pro MWh") +
theme_bw()
´´´
I'm new to Julia and I'm trying to make plot using PyPlot back-end with multiple Y-Axes, more than two.
Using:
twinx()
I'm able to plot two y-axes, and plot multiple line on each y-axis. But as soon as I try to use more than two y-axes all the y-axes on the right are superposed. Is there a way to add a margin between the axes?
Below a simple example:
using LaTeXStrings
using Plots
pyplot()
# Sets the default font family for the plots.
plot_font = "CMU Typewriter Text"
a = [1; 2; 3]
b = [3; 5; 2]
c = [4; 7; 1]
d = [7; 2; 12]
e = [3; 9; 1]
plSz = (1600, 900)
lbl = "line 1"
title = "Test"
xlabel = "xlabel"
ylabelx = "ylabel 1"
gfc0 = Plots.PyPlot.figure(title, figsize = (10, 10))
gfc0 = plot(
a,
b,
label = lbl,
size = plSz,
marker = :xcross,
legend = :topleft,
fontfamily = plot_font,
linewidth = 2,
grid = true,
minorgrid = true,
minorgridls = :dot,
gridls = :dash,
gridlw = 1,
minorgridlw = 0.75,
background_color = RGB(7/255,54/255,66/255),
right_margin = 25Plots.mm
)
gfc0 = title!(title)
gfc0 = xlabel!(xlabel)
gfc0 = ylabel!(ylabelx)
gfc0 = xaxis!(minorticks=10)
gfc0 = yaxis!(minorticks=10)
lbl = "line 2"
ylabelx = "ylabel 2"
gfc1 = twinx(gfc0)
plot!(
gfc1,
a,
c,
label = lbl,
marker = :cross,
legend = :topright,
linewidth = 2,
ylabel = ylabelx,
linestyle = :dash
)
lbl = "line 3"
ylabelx = "ylabel 3"
gfc2 = twinx(gfc1)
plot!(
gfc2,
a,
d,
label = lbl,
legend = :top,
marker = :circle,
linewidth = 2,
ylabel = ylabelx,
linestyle = :dot
)
lbl = "line 4"
plot!(
gfc2,
a,
d,
label = lbl,
linewidth = 2,
marker = :circle,
linestyle = :dot
)
display(gfc0)
The result of this code looks like this:
Plot with 3 y-axes and the two on the right are superposed
When I try to assign colours using colour = "" or fill = """, the graph changes its colour always to the same colour (some kind of weird orange tone). The specific code I used is this:
Plot <- ggplot(data, aes(ymin = 0)) + geom_rect(aes(xmin = left, xmax = right, ymax = a, colour = "#FDFEFE")).
Has anyone had this problem before? It doesn`t seem to matter whether I use the colour names or the HTML codes, the result stays the same.
Thank you!
I just wanted to add some explanation because this also tripped me up when I started using ggplot.
Some toy data:
data <- tibble(a = 1:10, left = 1:10 * 20, right = 1:10 * 20 + 10)
As explained by #stefan, you need to set the hard-coded color outside of the aestetics:
ggplot(data, aes(ymin = 0)) +
geom_rect(aes(xmin = left, xmax = right, ymax = a), fill = "#FDFEFE")
The aestetics are meant to link the plot to your data table. When you write this:
ggplot(data, aes(ymin = 0)) +
geom_rect(aes(xmin = left, xmax = right, ymax = a, fill = "#FDFEFE"))
It is like having the following:
data <- tibble(a = 1:10, left = 1:10 * 20, right = 1:10 * 20 + 10, col = "#FDFEFE")
ggplot(data, aes(ymin = 0)) +
geom_rect(aes(xmin = left, xmax = right, ymax = a, fill = col))
Except that ggplot understands "#FDFEFE" as a categorical value, not as a color. Having "#FDFEFE" or "banana" is the same to ggplot:
ggplot(data, aes(ymin = 0)) +
geom_rect(aes(xmin = left, xmax = right, ymax = a, fill = "banana"))
or
data <- tibble(a = 1:10, left = 1:10 * 20, right = 1:10 * 20 + 10, col = "banana")
ggplot(data, aes(ymin = 0)) +
geom_rect(aes(xmin = left, xmax = right, ymax = a, fill = col))
assign default colours to the categorical data.
As an extra, if you want to assign specific colors to different entries in the table, it is best to use a scale_*_manual layer:
data <- tibble(a = 1:10, left = 1:10 * 20, right = 1:10 * 20 + 10,
col = sample(c("banana", "orange", "coconut"), 10, replace = T))
ggplot(data, aes(ymin = 0)) +
geom_rect(aes(xmin = left, xmax = right, ymax = a, fill = col)) +
scale_fill_manual(values = c("banana" = "yellow2", "orange" = "orange2", "coconut" = "burlywood4"))
If you wanted to hard-code the colors in the table, you would have to use this column outside to the aestetics:
data <- tibble(a = 1:10, left = 1:10 * 20, right = 1:10 * 20 + 10,
col = sample(c("yellow2", "orange2", "burlywood4"), 10, replace = T))
ggplot(data, aes(ymin = 0)) +
geom_rect(aes(xmin = left, xmax = right, ymax = a), fill = data$col)
But it is best to use meaningful categorical values and assign the colors in the scale layer. This is what the grammar of graphics is all about!