R legend with different icons - r

For my graph I use smartlegend. I have a plot composed of lines and boxplots. Is there a way to use different icons in smartlegend, i.e. I'd like to have the "normal" color filled boxes for the boxplots and some simple horizontal lines for the other data.
Thanks for your help!

Why not use the standard legend function? The following will produce a legend with 2 lines and 2 symbols
legend ("top",
col=c(1:4),
lty=c(1,1,0,0),
pch=c(0,0,1,2),
legend=c("1","2","3","4")
)

Related

How to remove the legend from a plot in R?

I'd appreciate help on how to remove a legend from a plot. Say we have this:
I want to remove the "PM2.5" and the corresponding red line at the bottom of the plot. I wish it to be blank, the reason for this is because I'm using the print and split functions to create a custom plot showing 3 different monitoring sites for air pollution analysis like this:
I do not wish to have 3 legends, just one at the bottom.
To turn off the legend of openair's timePlot, set "key" to FALSE.

Is there a way to plot multiple plot types (i.e., bar chart and adjacent strip chart) on the same plot?

I'd like to display a bar plot with an adjacent series of strip charts, both with different y-axes, on the same overall plot. Can someone please suggest a way to to do this if possible?
The goal is to use a bar plot to show fold-change for qPCR, and then in the same figure show the individual dCt values for control and treatment groups that were used to generate the fold-change. The bar plot and strip chart need different y-axes.
Thank you for your help.

Mixed geom_line & geom_point plot: remove marker from color scale

I often have to use plots mixing lines and points (ggplot2), with the colors of the line representing one variable (here, "Dose"), and the shape of the points another one (here, "Treatment). Figure 1 shows what I typically get:
Figure 1: what I get
I like having different legends for the two variables, but would like to remove the round markers from the color scale, to only show the colors (see legend mockup below, made with Gimp). Doing so would allow me to have a clean legend, with colors and shapes clearly segregated.
Figure 2 (mockup): what I would like
Would anyone know if there is a way to do that? Any help would be much appreciated.
Note: the plots above show means and error bars, but I have the same problem with any plot mixing geom_line and geom_point, even simple ones.
Thanks in advance !

How do I Create a Faceted Bar Graph with Different Discrete X Axis in R ggplot

I have a bar graph which looks like the following:
Problem: If I facet it by the same variable as the color, the x-axis has space for plotting all the bars even though I don't need them.
My Solution: I used multiplot function from the Rmisc to separately make bar graphs for each partner but then a lot of individual customization is needed to make the graphs go cohesively together.
Question: Is there another way that I can use to get closer to plot 2 without the extra spaces for the variables that don't apply.
I'm using ggplot to plot.
use:
+ facet_wrap(~variable_to_facet_by,
scales = 'free')
as part of your ggplot code and that should get you what you want.

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)

Resources