Remove grid lines in dotplot without modifying underlying trellis parameters - r

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

Related

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

How to fix width of bar in plotly R

Plotly is one of the most useful plotting packages in R, but I am facing a very basic issue with it.
When I am drawing a barplot , it looks ok in a small screen, but as soon as I resize the window , it covers the whole window, making it ugly.
As my requirement is dynamic , number of bars on the plot changes, but I want to keep the bar width look decent.
But when it gets resized
the code which I am using to plot the barplot is simple and I hope need not be explained . The width option doesnt make any impact on the graph.
Am I missing anything?
ds <- as.data.frame(matrix(c('some_name',2300),nrow = 1,ncol=2))
colnames(ds) <- c('name','value')
plot_ly(ds,x=name,y=value,type='bar') %>% layout(width = 0.1)
There is no such option called bar size in ploty, but you can use bargap option in the layout.
Yes, you need to set the autosize=F. So correctly it should be :
plot_ly(ds,x=name,y=value,type='bar') %>% layout(width = 100, autosize=F)
Another option is to set width and height in the plotlyOutput (keeping the autosize=T):
plotlyOutput("plot", height="700px", width="200px")))
I could not find the answer either, but I found an alternative solution, which is to use dot plot, instead of bar chart.
See: https://plot.ly/r/dot-plots/

not enough horizontal space to display frequencies

I am attempting to use the aggr() function of the VIM package to plot missing data patterns. My plots don't show the frequencies/proportions of missing data patterns to the outside of the right-side axis. Should look like this. I'm getting an error that there is "not enough horizontal space to display frequencies".
library(VIM)
aggr(sleep, prop = T, numbers = T)
I don't do much base R plotting. I think this has something to do with margins. I reviewed this informative tutorial on margins, but I did not find my way to a solution.
I had two problems: one localized and one related to aggr().
1) This function helped me to reset par("pin"). Resetting made the toy example work.
resetPar <- function() {
dev.new()
op <- par(no.readonly = TRUE)
dev.off()
op
}
par(resetPar())
2) My actual use case was still failing with the same horizontal space error. I realized I needed to set the cex.numbers parameter of aggr() to something less than 1.

R: how to change lattice (levelplot) color theme?

The default theme on my installation is something which maps the values to pink and cyan. How to change it for example to a gray scale theme?
You can use
library(lattice)
lattice.options(default.theme = standard.theme(color = FALSE))
which turns on black-and-white suitable for printing. I've also played with things like
sb <- trellis.par.get("strip.background")
sb[["col"]][1] <- "lightgray"
trellis.par.set("strip.background", sb)
which changes just the header background.
And I thought people only asked ggplot2 questions here :) Nice to see some lattice for a change.
Thanks for the answers guys! It also helped me to find more information on the subject. I learned that I can control the scales of gray using for example the following:
levelplot(my_var, col.regions = gray(0:100/100))
which gives me 100 shades of gray from black (0) to white (1).
I'm using the function to plot gray scale images (photos) which I've pre-processed to a double matrix. I don't know if it's the best possible approach, but so far it works and I believe it gives me more options for graphing than the basic displaying options in the EBImage and rimage libraries. I don't know how I'd alter the palette to match displaying color images, but I'm glad I didn't have to do that so far...
There are many ways to accomplish your request. The simplest is:
trellis.device(color = FALSE)
another is
ltheme <- canonical.theme(color = FALSE) ## in-built B&W theme
ltheme$strip.background$col <- "transparent" ## change strip bg
lattice.options(default.theme = ltheme) ## set as default
See this mail archive item: Re: [R] specify lattice black-and-white theme for more info.
You may also want to consider using panel.levelplot.raster, as it reduces the figure size sizably, as I recently found out. Combining this tip with changing trellis settings in general, here are a following examples:
trellis.par.set(regions=list(col=topo.colors(100)))
levelplot(volcano, panel = panel.levelplot.raster)
levelplot(volcano, panel = panel.levelplot.raster,
par.settings=list(regions=list(col=topo.colors(100))))
The second method is deceptive because the trellis settings are in fact being changed globally. I didn't know about the col.regions argument though - that's pretty nice as it seems to change the color theme locally.
The only one that worked for me is:
>trellis.par.set(canonical.theme(color = FALSE))

Resources