Legend labels not appearing - makie.jl

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)

Related

how to fix legend in pie chart rstudio

pie(table(games_list$Genre),
main = "Rating Pie Chart",
col=brewer.pal(length(games_list$Genre),'Spectral'))
legend("topright",
legend=row.names(games_list$Genre),
fill = brewer.pal(length(games_list$Rating), 'Spectral'))
Error in legend("topright", legend = row.names(games_list$Genre), fill = brewer.pal(length(games_list$Rating), :
'legend' is of length 0
Check that row.names(games_list$Genre) returns something and is what the legend parameter expects.
Because you did not provide sample data, I am using the iris dataset in the example below:
pie(table(iris$Species),
main = "Rating Pie Chart",
col=brewer.pal(
length(unique(iris$Species)), # we need only unique values
'Spectral'
)
)
legend("topright",
legend=unique(iris$Species),
fill = brewer.pal(
length(unique(iris$Species)),
'Spectral'
)
)
The code above produces:
As an aside, if you have more than 2 or 3 types/classes you want to show, better use barcharts. Here is a good write up by Stephen Few on this topic "Save the Pies for Dessert"

y axis labeling in R and how to change x-axis to specific increments in R

I would like to create a plot of this data, with x-axis increments of 500000 and with sampleIDs on the y-axis. The following code works to create the plot, but the y-axis labels don't work, and I am unsure how to code the x-axis ticks. Also, I had to add headings manually to the data file (and then obviously add header = TRUE when I assigned d) to get the code to work. I shouldn't have had to put the column titles in though should I since I use setNames?
d = read.delim("n_reads_per_sample.tsv", header = TRUE, sep = "\t")
xticks <- ( ? increments of 500000 to xmax ? )
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim = c(0, at = xticks, 1 max(d$n_reads)),
labels = dimnames(d[[1]])
,
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
In case the link doesn't work, this is what the file looks like.
x.sample n_reads
LT-145 3193621
LT-323 786578
LT-458 485543
LT-500 3689123
LT-95 3308764
LT-367 765972
LT-205 2090226
LT-245 10238727
I can't get at your full data right now, so I am just using your sample in the question.
Not sure what you mean that the y-axis labels don't work. They seem OK to me. You can get the x-axis labels that you want by suppressing the x-axis produced by dotchart and then making your own axis using the axis function. That requires a little fancy footwork with par. Also, unless you stretch out your graphics window, there will not be enough room to print all of the axis labels. I reduced the font size and stretched the window to get the graph below.
UpperLimit <- ceiling(max(d$n_reads)/500000)*500000
xticks <- seq(0,UpperLimit, 500000)
par(xaxt = "n")
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim=c(0, UpperLimit),
labels = dimnames(d[[1]]),
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
par(xaxt = "s")
axis(1, at=xticks, cex.axis=0.7)

how do you write some text on a plot using the plot() function

I have a plot and I'd like to write some text on the plot is this possible?
how can I write: "this is my text on the plot" somewhere on the actual plot?
frame1<-data.frame(x = rnorm(4), y=rnorm(4))
plot(frame1$x,frame1$y,main="plot 1")
You only need to use text():
frame1<-data.frame(x = rnorm(4), y=rnorm(4))
plot(frame1$x,frame1$y,main="plot 1")
text(x = 1, y= 0.5, labels ="this is my text on the plot")
x, y : is the position in your graph.
For more info, see the text() vignette:

Space between legend items

I'm using R and the lattice package to plot the chart below.
Notice the two items in the legend, that are shown with no spacing between them. If I show the legend above (or below) the chart, it's shown with some space between them, but not if I show in on the right. Is there any way I can separate the two items a bit?
The code used to produce this figure is also reproduced below.
barchart(val1 ~ val2, groups=group, tasks,
auto.key=list(
columns=1,
space="right",
text=c("Data 1","Data 2")
),
main="Title",
xlab="Tasks", ylab="Duration",
par.settings=list(superpose.polygon=list(col=c("firebrick","dodgerblue2")))
)
I think padding.text ist what you are looking for. I dont know, if that works with auto.key.
attach(mtcars)
gear.f<-factor(gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"))
cyl.f <-factor(cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
densityplot(~mpg|cyl.f,
main="Density Plot by Number of Cylinders",
xlab="Miles per Gallon"
,par.settings = list(superpose.line = list(col=c(1,2,3)))
,key = list(text = list(c("A", "B", "C")),lines = list(col=c("black", "red", "green")),
columns=1,space="top",padding.text=4
)
)

Splitting axis labels with expressions

I have a plot with a long label containing an expression and I want to split it on two lines. Adding a "\n" inside the expression the result is not as expected.
ylabel <- expression("A very long label with text and \n
expression"*(alpha+beta) [ij]*" A very long label with text and expression")
curve(x^3 - 3*x, -2, 2, xlab=xlabel)
Any help would be appreciated. Thanks
Here is another solution, relying on atop as did #AndresT in his edit.
Note that we cannot use control character like \n in an expression, which explains why using something like expression(paste("...\n", alpha[i], "....")) would not produce the desired output.
xlabel <- expression(atop("A very long label with text and expression",
paste((alpha+beta)[ij], " A very long label ...")))
curve(x^3 - 3*x, -2, 2, sub=xlabel, xlab="")
Note that I used sub instead of xlab to avoid collision with x tick marks.
plot(1:10, ann = FALSE)
mtext(side = 2, text = "Y Axis", line = 3)
mtext(side = 2, text = "and something extra", line = 2)
For ggplot2:
set.seed(124)
data1 = data.frame(x=1:10,y=rnorm(10,1))
colnames(data1) = c("X", "Y")
theme = theme_set(theme_bw())
# atop is the function to use in order to get two lines
xlab = expression(atop(paste("x Axis"),
"More text"))
ylab = expression(atop(paste("y Axis"),
"Two lines"))
ggplot(data1, aes(x = X, y = Y))+
geom_point(shape = 1, color ="black") +
xlab(xlab) +
ylab(ylab)
#And adjust the margins with opts.
example line in ggplot2:
ylab(expression(atop(paste(italic("Saxifraga tridactylites")),
"individuals per plot")))+

Resources