In ggplot2, how to choose which geom appears in legend? - r

Some geom obscure the key to other geoms in the legend (notably, boxplot)
How can I choose which geom appears in the legend?
Eg.:
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment)+
geom_boxplot()
Switching the order of the geoms helps
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment,
geom="boxplot")+
geom_point()
But I would like the legend found with:
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment)
Do I need to extract the legend of one plot and paste it to the other using something like gridExtra?

You can suppress the legend for the boxplot by adding show_guide=FALSE to the geom_boxplot() call. You still get the legend from the points.
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment)+
geom_boxplot(show_guide=FALSE)
If you were not already plotting points (that is, just had boxplot, but wanted the legend to be shown with points symbol rather than the boxplot symbol), that is harder, though I think possible.

Related

Annotate is giving error in ggplot2 when using facet

I had previously used annotate() to add letters to facet panels of ggplots. After updating R (to 3.6.1), code that had previously worked with annotate no longer does.
I can solve this by making a separate dataframe to label each facet, but that is cumbersome when I have a decent number of plots to make that vary in how many facets they have. All I want is a letter (e.g., a-f) on each panel for identification in a journal article.
library(ggplot2)
data(diamonds)
ggplot(diamonds, aes(x=carat,y=price)) +geom_point()+ facet_wrap(~cut) + annotate("text",label=letters[1:5],x=4.5,y=15000,size=6,fontface="bold")
ggplot(diamonds, aes(x=carat,y=price)) +geom_point()+ facet_wrap(~cut) + annotate("text",label=letters[1],x=4.5,y=15000,size=6,fontface="bold")
The first ggplot should produce a plot that has the facets labeled with lowercase letters. Instead, I get the error:
Error: Aesthetics must be either length 1 or the same as the data (25): label
The code does work if only one letter is used, as seen in the second ggplot, so annotate will work, but not with multiple values as it previously did.
I usually always use an external data frame for faceted annotations, because it is more traceable to me.
df_labels=unique(diamonds[,"cut"])
df_labels$label=letters[as.numeric(df_labels$cut)] #to preserve factor level ordering
df_labels$x=4.5
df_labels$y=15000
ggplot(diamonds, aes(x=carat,y=price)) +
geom_point()+ facet_wrap(~cut) +
geom_text(data=df_labels,aes(x=x,y=y,label=label))

Easiest way of editing labels in legend when using color and shape in aes

I wonder if there is a function like:
scale_color_hue()
but for shapes.
Editing the labels of a legend when using colors in aes is esay with that function, but I cannot do it when mixing colors and shapes.
For instance, the command:
ggplot(subdata, aes(x=factor(rGPUs), y=Speedup, colour=factor(Factor), shape=factor(Factor), group=Factor, ymin=0)) +
geom_line(size=1) + geom_point(size=4) +
scale_color_hue(labels = c("1X", "1.5X", "2X"))
generates this chart:
I would like to plot it without the second legend. Nevertheless, I would not like to use the functions:
scale_color_manual()
scale_shape_manual()
since they need the labels together with the values, such as it is done in:
Editing legend (text) labels in ggplot
R manually set shape by factor
among many others.

ggplot2 only plotting axes - not the points

I have a CSV file like:
date;foo
2016-07-01;0,54
2016-08-01;0,54
2016-09-01;0,50
2016-10-01;0,49
but then read into R and plotted
foo2 <- read.csv2("here")
ggplot(foo2, aes(x=date, y=foo))
The output is empty. I.e. axes are present but no points are plotted.
A regular plot(foo2$foo) simply plots the points - what could be wrong here?
You need to add a geom to your plot. If you want a line plot add...
ggplot(foo2, aes(x=date, y=foo)) + geom_line()
If you want a scatter plot...
ggplot(foo2, aes(x=date, y=foo)) + geom_point()
You can find more geoms here.

ggplot2 position='dodge' producing bars that are too wide

I'm interested in producing a histogram with position='dodge' and fill=some factor (i.e. side-by-side bars for different subgroups within each bar/group), but ggplot2 gives me something like the first plot here, which has a rightmost bar that's too wide and reserves no space for the empty group, which I would like.
Here's a simple case:
df = data.frame(a=c('o','x','o','o'), b=c('a','b','a','b'))
qplot(a, data=df, fill=b, position='dodge')
From ggplot geom_bar - bars too wide I got this idea, and while it technically produces a bar of the same width, but preserves no space for the empty group:
ggplot(df, aes(x=a, fill=a))+
geom_bar(aes(y=..count../sum(..count..))) +
facet_grid(~b,scales="free",space="free")
How do I achieve what I want? Thanks in advance.
The default options in ggplot produces what I think you describe. The scales="free" and space="free" options does the opposite of what you want, so simply remove these from the code. Also, the default stat for geom_bar is to aggregate by counting, so you don't have to specify your stat explicitly.
ggplot(df, aes(x=a, fill=a)) + geom_bar() + facet_grid(~b)

Trying to keep filled bars in a faceted plot

Not sure what I'm doing wrong here. I have this plot:
ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge")
which produces:
Then I want to facet by a factor, creating two stacked plots w/ dodged, colored bars
ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge") +
facet_grid(~group_label)
However, I lose the factor-based coloring, which I want to keep:
If you move the fill into the geom_bar it should work. As:
ggplot(data.PE5, aes(ybands)) + geom_bar(aes(fill=factor(decide)),position="dodge") + facet_grid(~group_label)
The reason is the way ggplot2 builds plots as a grammar (I think).

Resources