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'))
Related
It was my understanding that the plot order was the same as the occurrence in the code. So if I plotted a line on code line 23 it would be plotted before (and appear behind) a label that was defined from code line 98.
In a workaround to my last question, I am using a line set to 30 px and a transparency of 75 to create a fill. The problem is it is plotting over the top of labels that are defined much later in the code and because of their location should be plotting on top of the line, but they are not.
I have searched everywhere, pine manual vs 4, Kodafy, and here on Stack to find exactly what the output order of drawing, labels, line, and plots are.
If anyone knows the solution to my specific problem or where I can find the information resources to resolve it myself I would be very grateful.
Last Post referred to above: https://stackoverflow.com/questions/69236171/i-want-to-fill-between-two-extended-line-new-pine-script-is-not-having-it-√/69241461#69241461
Thanks, Michael
try adding this to you study() line:
explicit_plot_zorder = true
from the refman:
"explicit_plot_zorder (const bool) Specifies the order in which the indicator's plots, fills, and hlines are rendered. If true, the plots will be drawn based on the order in which they appear in the indicator's code, each newer plot being drawn above the previous ones. This only applies to plot*() functions, fill, and hline. Optional. The default is false."
Hope that can fix your problem
Cheers, and best of luck with your trading and coding
I often use R to run batch jobs that contain PDF outputs with scatter plots produced using a combination of plot() and points(), among other graphical functions. (I don't use ggplot2 much and would like to avoid using it for this question.)
When using the text() function in a plot in order to add text near a plotted symbol, I like to use the pos = 4 option to right-justify text next to say a symbol like pch = 23 (filled diamond). But I've noticed that sometimes the text will get wrapped as part of multiple lines, and other times it stays on a single line. Unfortunately when the lines are wrapped it causes text overlap problems. Why does text() sometimes wrap text on multiple lines?
Is there a way to force R to keep the text added to a plot from text() on a single line?
Here is my code:
text(x=data_frame_w_data_to_plot$x_axis_value,
y=data_frame_w_data_to_plot$y_axis_value, labels=data_frame_w_data_to_plot$text_to_plot, col="black", cex=1, pos=4)
Answering my own question here. Found the text string of "\n" just happened to be in one of the cells, and simply used gsub() to slightly modify this cell. This solved the problem. Learn from my mistake. :)
data_frame_w_data_to_plot$text_to_plot <- gsub("\n", " ", data_frame_w_data_to_plot$text_to_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) ;
I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)
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