Add information to an existing legend in an Octave plot - 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) ;

Related

Formatting line breaks when using superscripts in tmap legend

I am trying to create a map with the tmap showing the population density. Therefore, I want the legend title of the map to be "Population density in n\ [people per km^2]". This map is the closest I have gotten to my desired final result.
Now, as you see, I manage to insert a line break but somehow the last "]" jumps all to the right and the title is cut half off in the first line. I think the latter is a problem I can solve by myself, but I don't find a solution to get rid off the former problem.
To create this map, I used the following code
tm_polygons("Pop_dens",title=expression(paste("Population density \n[people per km"^"2","]")),breaks=c(0,50,100,500,1000,3000,5000,25000),border.alpha = 0)+
tm_shape(border_plateau)+
tm_polygons(alpha=0)+
tm_scale_bar(breaks=c(0,10,20,50,100),position = c(0.56,0.04))+
tm_compass(position = c(0.4,0.04))+
tm_legend(legend.position=c(0.71,0.11))
How can I remove the space (ergo make it left-binding)?
I am familiar with making new lines in the legend using \n and pasting expressions using expression(paste(..)), but combining both doesn't seem to work.
One option would be to use:
title=expression(atop("Population density","(people per km"^2*")"))
Although the space between lines may be excessively large
I faced the same problem and solved it by using HTML tags with the package ‘htmltools’:
title=paste('Line 1', br('Line 2'))

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...

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")
})

R Make barchart with unicode symbol instead of bars

I'd like to plot a (kind of) barchart, but rather than using bars, I'd like to use a unicode symbol. Is this possible?
I know how to use a unicode character as a symbol (points); for example:
plot(1,1,pch=-0x0001F3E2, cex=5, col="gray30")
but how can make the symbol substitute the bars?
EDIT:
As suggested by Eric, I'll try to write a minimal reproducible example...
Given variable x:
x <- c(2,3,4,2,1)
barplot(x)
I want to produce a barplot using custom symbols rather than bars. Something like:
plot(1:5,rep(0,5),pch=-0x0001F3E2, cex=x, col="gray30")
The problem is that I am not able to vertical align sysmbols as I can do with text:
plot(1:5,rep(0,5),type="n", ylim=c(0,1),bty="n")
text(1:5,0,"A",cex=c(2,5,3,3,1),adj=c(0.6,0))
So, a sort of solution to my problem, although not ideal, is to use
some custom system font with package extrafont.
For example, using font "Destiny's Little Houses" (http://www.dafont.com/destiny-little-hous.font)
I am able to produce some nice infographics:
plot(1:5,rep(0,5),type="n",ylim=c(0,1),bty="n")
text(1:5,0.1,"A",cex=c(2,5,3,3,1),adj=c(0.6,0),family="Destiny's Little Houses")
So, rather than having bars I have an image whose size changes according to some variable (using "cex").
This is not ideal, first of all because I'd like to be able to import any image (ex. using the png package).
So, is it possible to:
import an image file and use it as the symbol in plot (rather than one of the available sysmbols using pch)?
Vertical align symbols so that I get a kind of custom barchart?
Thanks
António
Here's a somewhat hackish start (might not be helpful as I don't know what your data looks like).
counts <- table(mtcars$gear)
foo <- barplot(counts, col = c("white", "white", "white"), border=c("white"), axes=F, xaxt="n")
points(foo, counts-.5, pch = -0x0001F3E2, cex=2, pos=3)
Remove , axes=F, xaxt="n" to get the labels and axis back.

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

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.

Resources