How can I fix my grouping and legend in R? - r

I'm really new to R and i'm trying to group the x axis together instead it being separate like it it and also move the legend.
Graph and Code http://127.0.0.1:41763/graphics/plot_zoom_png?width=1200&height=455
Ran<-table(data$class, data$feeling)
Raw<-barplot(Ran, main="Class Feeling",xlab="Feeling", col=c("darkblue","red"), legend = rownames(Ran), beside=TRUE)
I would help with moving the legend and have the x-axis grouped as one. on the group its two separate, like Great and Great, where i just want one great on the group with the data together by the different times

You can specify legend position and others by using args.legend(). Unfortunately I can't tell you exactly what to do without knowing what data set would looks. However, I think this page might help you.

Related

How to move the legend to outside the plotting area in Plots.jl (GR)?

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)

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

mosaic chart coloring, scaling and order

I am very new to R and just did my first chart:
View Plot
Could you help me understand how I can change the order of the x axis and apply coloring to the different pieces of the mosaic chart?
I would also like to reduce the spacing between the pieces (linebreaks for text?), remove the black borders and avoid the heading to overlap.
Many thanks for any help! I would really appreciate it.
you just need to specify the levels of your factor in the order you want.
To know more visit

R - Adding series to multiple plots

I have the following plot:
plot.ts(returns)
I have another dataframe ma_sd which contains the rolling SD from moving averages of the above returns. The df is structured exactly like returns. Is there a simple way to add each line to the corresponding plots?
lines(1:N, ma_sd) seemed intuitive, but it does not work.
Thanks
The only way I can see you doing this is to plot them separately. This code is a bit clunky but will allow you full flexibility to be able to specify labels and axis ranges. You can build on this.
par(mfrow=c(3,1),oma=c(5,4,4,2),mar=c(0,0,0,0))
time<-as.data.frame(matrix(c(1:length(returns[,1])),length(returns[,1]),3))
plot(time[,1],returns[,1],type='l',xaxt='n')
points(time[,1],ma_sd[,1],type='l',col='red')
plot(time[,2],returns[,2],type='l',xaxt='n')
points(time[,2],ma_sd[,2],type='l',col='red')
plot(time[,3],returns[,3],type='l')
points(time[,3],ma_sd[,3],type='l',col='red')

Adding individual X and Y axis labels when using facet_wrap()

I am attempting to plot lots of graphs on the fly and I chanced upon the facet_wrap functionality. It produced the desired results until I realised that it was not assigning individual axes headings. There was just a single X and Y axis heading for a whole set of graphs. What I'm looking for is a way to assign individual axes headings for each graph.
Is this possible using the facet_wrap functionality at all?
Looking forward to any suggestions and advice.
EDIT:
(removed previous, incorrect, answer)
It is my understanding that if the axes of your plots are not the same (i.e. require different labels), the way to go would be with multiple separate plots (on the same page), and not with facet_wrap.

Resources