Adjusting event.labels in addEventLines in the new xts (old xtsExtra) - r

A very basic question, I'm afraid, but I'm struggling to understand what controls I have over event.labels in the new xts(old xtsExtra). I would like to:
resize the text of these labels
adjust the offset (distance) between the text and the lines themselves
ideally have the option to have the text inside a legend at the side of the chart
Some basic code:
library(quantmod)
library(xts)
getSymbols('F',src='yahoo')
F.Close <- F[,4]
zoo.F <- as.zoo(F.Close)
xF <- as.xts(zoo.F)
plot.xts(xF)
addEventLines(event.dates="2013-01-05",event.labels = "Motor Show",
date.format="%Y-%m-%d",main="",on=1,lty=1,lwd=3,col="red")
Also if anyone can point me in the direction of some primers on this functionality I'd be grateful. Many thanks in advance.

Some of the arguments to control the labels were hardcoded in the addEventLines() function which is why you weren't able to resize the text or adjust the offset of the labels. We removed the hardcoded values for offset, pos, and srt and now pass ... through to the call to text in addEventLines() which draws the labels.
See http://joshuaulrich.github.io/xts/plotting_basics.html for examples of adding event lines as well as adding a legend to address your third point.

Related

Labelling X and Y axis in Dymola plot

I have drawn a plot using "plotArrays" function in Dymola. I would like to label X-axis in meters(Ideally, I need it to be flexible so that I can later change it to millimeters, micrometers, etc..). I want to label Y-axis similarly in volts which I have done already using
plotArrays(x_axis,phie,legend=names,units=fill("V",size(phie,2)));
and I can read the values in volts and also change to mV etc.. However, its not 'visible' in Y-axis as you can see in the plot below. So, How can I label the axes separately?
Thanks a lot!
I don't know any more efficient possibility than this:
createPlot(id=1, erase=false, grid=true, leftTitleType=2, leftTitle="myLabelY", bottomTitleType=2, bottomTitle="myLabelX")
with:
id being the number shown in the original plot
erase=false to ensure that the content is not modified
grid=true (re-)enabling the grid
*TitleType=2 saying that there is a custom title
*Title being the string to put there
This will result in the plot being resized to the default size. You can use plotSetup() to get the current setup, including position, which you can then pass (manually) to the createPlot() command to result in the original size again.
Not very elegant, but I don't know any other possibility...

Add information to an existing legend in an Octave plot

I have a figure in Ocatve 4.4.1, with a single plotted dataset and its corresponding legend.
I want to add another plot to that figure (in the same primary XY axes, but I doubt this is relevant here), and extend the legend with this plot.
I managed to accomplish the first (adding the second plot), using
fig = figure(1) ;
hold on ;
plot(...) ;
with the same figure as before.
But I could not extend the legend.
I guess one way of doing this is getting the text of the legend, and setting it again extending the text with what I want.
That may lose some formatting, but it would be ok for a starter.
How can I do this?
You can add the legend text in the plot command:
plot(...,'DisplayName','legend text here')
The legend should update automatically. If it doesn't, you can turn it off and then back on again (isn't that always the solution to computer problems?):
legend off
legend show
With the answer provided by Cris I put together a simple and versatile solution.
I had the style used for plotting (e.g., '-r') as a string named style.
Then, as shown in official documentation, I could choose via a variable leg_param whether to extend the legend for each added plot, with
if ( !strcmp(leg_param, '') )
style = [ style ";" leg_param ";" ] ;
endif
ploth = plot(xtab1, ytab1, style) ;

Plotly Multi Column Horizontal Legend

I am trying to create horizontal bar chart in in R using the plotly package. Due to the length of the legend items I would like for them to show horizontally at the top or bottom of the visual in 2 columns. Is it possible to dictate the number of columns for the legend?
I've been able to place the legend below the x axis successfully using Layout(legend = list(orientation='h')) however regardless of where I put the legend (using the x and y arguments) it is always just one long list. I've seen a github project for creating a multi column legend in js but not r.
Thanks,
This is not possible in a normal way. I think it has its own logic that determines how many place there it is and how many columns it will display then.
So I guess if you make your plot width smaller you could reach the goal that it will just display 2 column.
Also you can try to play around with the margin attribute (https://plot.ly/r/reference/#layout-margin) by setting r and l to 10 e.g.
An other idea could be to make the font-size in legend (https://plot.ly/r/reference/#layout-legend-font-size) bigger, so that it just uses two columns. Hope it helps.
I read the same github page and I thought that it is not possible, but seems to be! I only checked in Python, but I hope this will help in your endeavors in R as well as everyone in Python looking for information. Sadly, there is not a lot of information on Plotly here compared to other packages.
This solved my problem
Setting orientation='h' is not enough. You also have to put the legend items in different legendgroups, if you want them in different columns. Here is an example with legend labels:
fig = go.Figure([
go.Scatter(x=best_neurons_df['Test Size'],
y=best_neurons_df['Training Accuracy Max'],
# You can write anything as the group name, as long as it's different.
legendgroup="group2",
name='Training',
mode='markers',
go.Scatter(x=best_neurons_df['Test Size'],
y=best_neurons_df['Validation Accuracy Max'],
# You can write anything as the group name, as long as it's different.
legendgroup="group1",
layout=dict(title='Best Model Dependency on Validation Split',
xaxis=dict(title='Validation Set proportion'),
yaxis=dict(title='Accuracy'),
margin=dict(b=100, t=100, l=0, r=0),
legend=dict(x=1, y=1.01,xanchor='right', yanchor='bottom',
title='',
orientation='h', # Remember this as well.
bordercolor='black',
borderwidth=1
))
Example image

Remove grid lines in dotplot without modifying underlying trellis parameters

I want to remove the light gray grid lines from a Lattice dotplot. After searching R help pages, Sarkar's book, and the web, the one answer I've found is this post, which explains that you can set the grid line width to zero for all dotplots using this magic:
## turn off grid lines
d1 <- trellis.par.get("dot.line")
d1$lwd <- 0 ## hack -- set line width to 0
trellis.par.set("dot.line",d1)
Example: Try dotplot(VADeaths[,"Rural Female"]) before and after doing the preceding.
This solution works, but I would have thought that there would be a way to control the grid lines from inside the dotplot function, perhaps using a panel function. Is there a way to do that? (An authoritative "No" could count as a correct answer.)
Setting col.line = "transparent" inside panel.dotplot should solve this issue. See also ?panel.dotplot.
dotplot(VADeaths[, "Rural Female"], panel = function(...) {
panel.dotplot(..., col.line = "transparent")
})

How to put legend marker on the right side of the word in R plot [duplicate]

The legend that R creates when you call legend() has the symbols (or line types etc) on the left and the labels on the right. I'd like it the other way round, i.e. labels on the left (right-aligned) and the symbols on the right.
I know that I can use adj to adjust the position of the labels, but with this they are not aligned properly anymore. If I set adj=2 for example, the labels are to the left of the symbols, but the end of the text is not aligned with the symbols.
Any pointers on how to do this using either the standard legend() function or a package would be appreciated.
If you set trace = TRUE and then save the output, you can draw the legend and then add the labels with a call to text() using the coordinates given by trace, setting pos = 2 for right alignment. Here's an example:
set.seed(1)
plot(1:10,runif(min=0,max=10,10),type='l',ylim=c(0,10),xlim=c(0,10),col=1)
lines(1:10,runif(min=0,max=10,10),col=2,lty=2)
lines(1:10,runif(min=0,max=10,10),col=3,lty=2)
a <- legend(1,10,lty=1:3,col=1:3,legend=c("","",""),bty="n",trace=TRUE)
text(a$text$x-1,a$text$y,c("line 1","line 2","line 3"),pos=2)

Resources