Supressing some labels in legend or putting sampled markers - julia

I am using Julia 1.1.0 and want to do some plotting in it. I am using Plots and pyplot backend for that.
I have two data that I want to plot with the same line style and color, differing them by markers.
Here is an example:
using Plots
pyplot()
nf = 150;
freq = exp10.(range(2, stop=6.4, length=nf)); #logspace
foo1 = (freq*2 .- 3);
foo2 = (freq .- 10);
plot(freq, foo1, label="foo 1", line=(1, :black, :solid), xaxis=:log, yaxis=:log)
plot!(freq, foo2, label="foo 2", line=(1, :black, :solid), marker=(:circle, :black, 3, 0,5))
The problem is that the line with the markers gets convoluted. I did not find an argument to put a marker every n-th point (is there one?)
Then I tried to generate a plot with line and no markers using the full data, then a plot with only markers (no lines) with a sampled data, as follows
plot(freq, foo1, label="foo 1", line=(1, :black, :solid), xaxis=:log, yaxis=:log)
plot!(freq, foo2, line=(1, :black, :solid))
r = 1:10:nf;
plot!(freq[r], foo2[r], line=(false), marker=(:circle, :black, 3, 0,5))
plot!(freq[1:1:2], foo2[1:1:2], label="foo2", line=(1, :black, :solid), marker=(:circle, :black, 3, 0,5))
It has the looks that I want, but now the legends are messed up. How could I suppress labels y2 and y3 from the legend?

I re-read the docs and found that for the attribute label of Series:
The label for a series, which appears in a legend. If empty, no legend
entry is added.
So, making the label an empty string "" solves the problem, while label=false or label=nothing don't.
plot(freq, foo1, label="foo 1", line=(1, :black, :solid), xaxis=:log, yaxis=:log)
plot!(freq, foo2, line=(1, :black, :solid), label="")
r = 1:10:nf;
plot!(freq[r], foo2[r], line=(false), marker=(:circle, :black, 3, 0,5), label="")
plot!(freq[[1,11]], foo2[[1,11]], label="foo 2", line=(1, :black, :solid), marker=(:circle, :black, 3, 0,5))

Related

How to create annotation with annotate and ggarange R

I need to write some text at my shared labels.
I have 4 subplots in my plot and they share the same label axis, so I would like to put some annotation at the Y label (and X label too, but the x label I manage excluding the x labels from all plots except from the last one).
I've tried the annotate function but nothing seems to change.
a <- ggarrange(pet, a63, a64, a65,
labels = c(" Polietileno Tereftalato (PET)", "Amostra 63",
"Amostra 64", "Amostra 65"),
ncol = 1, nrow = 4, label.x=0.18, label.y= 1, align ="v",
vjust=2, hjust=0.2,
legend = "bottom")
a + annotate("text", x = 0, y = 25, label ="Absorbância")
a

Legend labels not appearing

I am attempting to do some line plots using Makie.jl but the legend doesnt appear when I pass a value to the label argument. Any idea why?
using GLMakie: lines, lines!
p,ax, l = lines(
rand(10),
label = "label 1"
)
lines!(
rand(10),
label = "label 2"
)
Try adding this: axislegend(ax)

Plot separate legends for shape and color with multiplot (coefplot) R

I am trying to plot a graph using multiplot, I function that I do not know really well. I want to set different colours with respect to the time span and different shapes with respect to the type of regression used. I tried to use the ggplot settings: I set the levels for colour and shape inside the multiplot function and then I add scale_shape_manual to set colours and shapes, but It does not seem to work. I will really appreciate your help.
Here the code I am running:
Model <- factor(c("Without controls","With controls 1","With controls 2",
"Without controls","With controls 1","With controls 2",
"Without controls","With controls 1","With controls 2",
"Without controls","With controls 1","With controls 2",
"Without controls","With controls 1","With controls 2",
"Without controls","With controls 1","With controls 2",
"Without controls","With controls 1","With controls 2",
"Without controls","With controls 1","With controls 2"),
levels = c("Without controls","With controls 1","With controls 2"))
Time_span <- factor(c("Day+0","Day+0","Day+0",
"Day+3","Day+3","Day+3",
"Day+7","Day+7","Day+7",
"Day+14","Day+14","Day+14",
"Day+21","Day+21","Day+21",
"Day+30","Day+30","Day+30",
"Day+60","Day+60","Day+60",
"Day+90","Day+90","Day+90"),
levels = c("Day+0","Day+3","Day+7","Day+14","Day+21","Day+30","Day+60","Day+90"))
coefplot::multiplot(string0,string01,string02,
string1,string11,string12,
string2,string21,string22,
string3,string31,string32,
string4,string41,string42,
string5,string51,string52,
string6,string61,string62,
string7,string71,string72,
intercept = FALSE, #if display the intercept
numberAngle=0, #The angle for the value labels, 0 is horizontal
coefficients = "wvs_risk", #coeff to show
title= NULL,
ylab = NULL,
xlab = "Stringency",
zeroColor = "black", #The color of the line indicating 0
zeroLWD = 1, #The thickness of the 0 line
zeroType = 1, #The type of 0 line, 0 will mean no line
color = Time_span,
shape = Model) +
coord_flip()+
theme_light() +
theme(legend.position = "left") +
scale_colour_manual(values = c("#e49205","#a15439","#e11e47","#e72eb6","#2b63e1",
"#32cdaf","#74bb81","#93e424"),
labels = c("Day+0","Day+3","Day+7","Day+14","Day+21","Day+30","Day+60","Day+90"),
name = "Time span") +
scale_shape_manual(values = c(0,16,25),
labels = c("Without controls","With controls 1","With controls 2"),
name = "Model") +
guides(colour = guide_legend(order = 2), shape = guide_legend(order = 1)) +
scale_y_discrete(labels=c("Risk taking"))
I get the following error message: "Scale for 'colour' is already present. Adding another scale for
'colour', which will replace the existing scale.
Scale for 'shape' is already present. Adding another scale for
'shape', which will replace the existing scale.
Error: Insufficient values in manual scale. 24 needed but only 8 provided."

How to replace a set of x axis labels with another one

I am plotting roughly 300 variables, R automatically sets x-axis labels and has them as numbers, I don't want this. But if I use the xaxt = 'n' feature and then 'axis', it won't work because for axis I'll need to define x labels for 300 variables to....
Is there any way to fix this?
To give a better example of my issue, my code looks something like this:
plot(data, xaxt = 'n')
x = c("Time 1", "Time 2", "Time 3", "Time 4", "Time 5", "Time 6")
axis(side = 1, at = 1:6, labels = x[1:6])
But this is not working, I think it has to do with the fact that I have 300 data points, not 6, but I just want it to equally space out these 6
at should contain 6 numbers between 1 and 300 at which you want the labels to be placed, not just 1 through 6, because this will squash them all at the beginning/left side.
dat=rnorm(300)
plot(dat, xaxt="n")
x=c("Time 1", "Time 2", "Time 3", "Time 4", "Time 5", "Time 6")
axis(1, at=floor(seq(1,300,length=6)), labels=x)

Set condition for axis.text.x in general theme() in R

I am working on creating a unified theme for a large project including many different types of plots. I have set an overall theme where the angle for axis.text.x.bottom is 0 (horizontal), however some of my x-axis labels are quite long. I would like to set the angle of axis.text.x.bottom equal to 0 for labels of length less than 3 and equal to 90 (read bottom to top) for labels of length greater than 3, without having to specify the theme for each individual plot.
*** this is a highly simplified example
library(ggplot2)
report_theme(axis.text.x.bottom = element_text(angle = 0))
testdata1 = matrix(NA, nrow = 10, ncol = 2)
testdata1[,1] = 1:10
testdata1[,2] = 1:10
testdata1 = as.data.frame(testdata1)
g1 = ggplot(testdata1, aes(x = testdata1[,1], y = testdata1[,2])) + geom_point()
g1 + report_theme
testdata2 = testdata1
testdata2[,1] = c("number 1", "number 2", "number 3", "number 4", "number 5", "number 6", "number 7", "number 8", "number 9", "number 10")
g2 = ggplot(testdata, aes(x = testdata2[,1], y = testdata2[,2])) + geom_point()
g2 + report_theme
The plot for testdata1 looks fine since the x-axis labels are numbers, however the x-axis labels for testdata2 are long strings, and hence can't easily be read horizontally because they overlap.
I know how to change the angle for each plot individually, but it requires knowing the name of the data frame being used to plot, and I would like to be able to do it within the overall theme report_theme. How can I do this when a different data frame is being used for each plot?

Resources