annotation_logticks() and coord_flip() seem incompatible - r

I get an error that seems to be combined by using annotation_logticks() and coord_flip() on the same plot. For instance:
ggplot(mtcars, aes(x=mpg, y=disp)) +
geom_line() +
annotation_logticks(sides="l") +
coord_flip()
gives the error Error in unit(yticks$y, "native") : 'x' and 'units' must have length > 0. traceback() gives results that I don't totally understand, but which seem to have something to do with assigning units.
On the other hand, annotation_logticks() or coord_flip() alone doesn't cause any problem.
ggplot(mtcars, aes(x=mpg, y=disp)) +
geom_line() +
annotation_logticks(sides="l") #+
#coord_flip()
works fine, as does
ggplot(mtcars, aes(x=mpg, y=disp)) +
geom_line() +
#annotation_logticks(sides="l") #+
coord_flip()
I could switch the x and y mappings to avoid coord_flip(), but this is not ideal (I have to rewrite old plots if I want to add annotation_logticks() for instance).

This has now been fixed in ggplot2 version 3.3.4 by this PR.
library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=disp)) +
geom_line() +
annotation_logticks(sides="l") +
coord_flip()
Created on 2021-08-29 by the reprex package (v2.0.0)

Related

Matching legends for ggplot2

I refer to section 12.2.4, exercise 1 in Hadley Wickham's ggplot2 book.
In the exercise, the following code is provided:
fwd <- subset(mpg, drv == "f")
rwd <- subset(mpg, drv == "r")
ggplot(fwd, aes(displ, hwy, colour = class)) + geom_point()
ggplot(rwd, aes(displ, hwy, colour = class)) + geom_point()
Which produces the following output:
The following task is given: Modify the code so that the legend and axes match, without using facetting!
I am able to modify so that the axes match:
ggplot(fwd, aes(displ, hwy, colour = class)) + geom_point() + xlim(c(0, 7)) + ylim(c(0, 45))
ggplot(rwd, aes(displ, hwy, colour = class)) + geom_point() + xlim(c(0, 7)) + ylim(c(0, 45))
yielding:
But, I'm unable to get the legends to match. This question comes before scale_colour_manual() is explained, so I don't think we're supposed to use that.
Any ideas about how to get the legends to match?
Thanks!

How do I add count label to an axis?

I created this bar chart using ggplot. I had to create my own count function and called it 'a,' but now the label for that axis just has an a and nothing else... How do I fix it?
a <- count(df, glass)
gl <- ggplot(df, aes(x=glass, y="a", fill=glass)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
xlab("Glass type") +
ylab("Count") +
coord_flip() +
theme_minimal() +
theme(legend.position = "none")
gl
Here is my graph
The default stat value for geom_bar is “count”, which means that geom_bar() uses stat_count() to count the rows of each x value, or glass in this case. Using stat="identity" will override the default geom_bar() stat and require that you provide the y values in order for aggregation to occur. I don't believe you are trying to do this.
Try the following and see if it is what you were looking for:
gl <- ggplot(df, aes(x=glass, fill=glass)) +
geom_bar() +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
xlab("Glass type") +
ylab("Count") +
coord_flip() +
theme_minimal() +
theme(legend.position = "none")

geom_label_repel text justification and alignment

Is there a possible work-around to left-justify the text label created by geom_label_repel (or geom_text_repel) in the example below where all text are placed with positive nudge_x value and y-only adjusted position in direction parameter? Currently, the default behavior is to center-align the text:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label_repel(aes(label=rownames(mtcars)),
size=3, segment.size=0.25, nudge_x=0.5, direction="y")
I am looking to emulate the left-justification that is possible in geom_label (or geom_text) by setting hjust=0 as seen in example below, while being able to automatically repel labels in the y direction:
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label(aes(label=rownames(mtcars)), size=3, nudge_x=0.2, hjust=0)
Edited: As a hack, would it be possible to build hjust (and vjust) into ggrepel?
In the 4 years since OP posted this question, hjust= seems to have been added to the ggrepel package:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label_repel(
aes(label=rownames(mtcars)), hjust=0,
size=3, segment.size=0.25, nudge_x=0.5, direction="y")

Enlarge ggplot2 legend

I have this plot made in R with ggplot2
which is drawn by the following code:
ggplot(mtcars) +
geom_smooth(fill='grey', alpha=0.3, span=0.1, aes(x=mpg, y=hp, color='AAA',linetype='AAA')) +
geom_smooth(fill='grey', alpha=0.3, span=0.9, aes(x=mpg, y=hp, color='BBB',linetype='BBB')) +
scale_colour_manual(name='test', values=c('AAA'='chocolate', 'BBB'='yellow')) +
scale_linetype_manual(name='test', values=c('AAA'='dashed','BBB'='solid')) +
theme_minimal() +theme(legend.position = "top")
Problem: from the legend, it is not easy to understand that the "AAA" line is dashed, since the box is too small.
How can I enlarge it?
I would love to have something similar to:
Try
# create your legend guide
myguide <- guide_legend(keywidth = unit(3, "cm"))
# now create your graph
ggplot(mtcars) +
geom_smooth(fill='grey', alpha=0.3, span=0.1,
aes(x=mpg, y=hp, color='AAA',linetype='AAA')) +
geom_smooth(fill='grey', alpha=0.3, span=0.9,
aes(x=mpg, y=hp, color='BBB',linetype='BBB')) +
scale_colour_manual(name='test',
values=c('AAA'='chocolate', 'BBB'='yellow'),
guide = myguide) +
scale_linetype_manual(name='test',
values=c('AAA'='dashed','BBB'='solid'),
guide = myguide) +
theme_minimal() + theme(legend.position = "top")
See ?guide_legend and here.
This will give you
You can use keywidth and keyheight to manipulate how much the key "stretches" into both directions. With title.position, direction, etc you can further finetune the legend.
Note that since you have multiple legends that are merged, you need to specify the guide to all merged scales. I simplified this by creating the guide outside as an object first.

Plotting geom_bar and geom_point together?

data=data.frame(x=rep(0:9, each=2))
ggplot(data, aes(x=factor(x))) + geom_bar(alpha=0.5) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=factor(x), y=y), alpha=0.5)
ggplot(data, aes(x=factor(x))) + geom_bar(alpha=0.5) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=factor(x), y=y), alpha=0.5) +
scale_x_discrete(limits=0:10)
Also, do I have to factor given x is integer so it is discrete already?
Wrong order
Wrong x axis label.
ggplot(data, aes(x=x)) + geom_bar(alpha=0.5) + scale_x_discrete(limits=0:10) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=x, y=y), alpha=0.5)
You can force a discrete scale to get what you want. It is odd how when you mix geom_point() and geom_bar() ggplot starts ordering things in unexpected ways.

Resources