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")
Related
I'm trying to make a plot with two y-variables with different shape and color, but I keep failing.
Any suggestions as to where I go wrong?
library (ggplot2)
library (forcats)
ggplot(mtcars, aes(x=disp, y=drat, group=as_factor(vs)))+
geom_point(aes(shape=as_factor(vs), color=as_factor(vs))) +
scale_shape_manual(values=c(21, 24))+
scale_fill_manual(values=c("black", "yellow")) +
theme_bw() +
facet_wrap(vars(cyl))
ggplot(mtcars, aes(x=disp, y=drat, shape=as_factor(vs)))+
geom_point() +
scale_shape_manual(values=c(21, 24))+
scale_fill_manual(values=c("black", "yellow")) +
theme_bw() +
facet_wrap(vars(cyl))
Be careful to use as.factor (not as_factor), and watch out for typos. Here is the corrected code:
library(ggplot2)
ggplot(mtcars, aes(x=disp, y=drat, group=as.factor(vs)))+
geom_point(aes(shape=as.factor(vs), color=as.factor(vs))) +
scale_shape_manual(values=c(21, 24))+
scale_fill_manual(values=c("black", "yellow")) +
theme_bw() +
facet_wrap(vars(cyl))
I used the following script to make the graph below
hypttauplot <- qplot(fuclin_csf, fitHYPTTAU, data=selTAU, geom=c("smooth"),
method="glm", color='black', linetype=BL_HYPT) +
theme_classic() + xlab("Time (years)") + ylab("Tau (pg/ml)") +
scale_x_continuous(expand=c(0,0)) +
ggtitle("A. Hypertension") +
theme(legend.position = "none")
But now my questions is: why are the lines red instead of black? And how can I change them to black?
You have to use I() to set the aesthetics manually in qplot(), e.g. colour=I("black") (setting vs. mapping of aesthetics).
# mapping
qplot(carat, price, data=diamonds, color="black")
# equivalent to ggplot(data=diamonds, aes(carat, price, color="black")) + geom_point()
# setting
qplot(carat, price, data=diamonds, color=I("black"))
# equivalent to ggplot(data=diamonds, aes(carat, price), color="black") + geom_point()
You can use scale_colour_manual to specify an own set of mappings.
I'm working on some flattening of overlapping ranges and would like to visualize the initial data (overlapping) and the resulting set (flattened) the following way:
Initial data:
Resulting set:
Is such possible with R and, for example, ggplot2?
read.table(header=TRUE, sep=",", text="color,start,end
red,12.5,13.8
blue,0.0,5.4
green,2.0,12.0
yellow,3.5,6.7
orange,6.7,10.0", stringsAsFactors=FALSE) -> df
library(ggplot2)
df$color <- factor(df$color, levels=rev(df$color))
ggplot(df) +
geom_segment(aes(x=start, xend=end, y=color, yend=color, color=color), size=10) +
scale_x_continuous(expand=c(0,0)) +
scale_color_identity() +
labs(x=NULL, y=NULL) +
theme_minimal() +
theme(panel.grid=element_blank()) +
theme(axis.text.x=element_blank()) +
theme(plot.margin=margin(30,30,30,30))
There are other posts on SO that show how to get the y labels like you have shown (we can't do all the work for you ;-)
The answer to the second part of the question can be using #hrbrmstr 's great answer for the first part. We can use overplotting to our advantage and simply set the y coordinates for the segments to a fixed value (for example 1, which where "red" is):
p <- ggplot(df) +
geom_segment(aes(x=start, xend=end, color=color),
y=1, yend=1, size=10) +
scale_x_continuous(expand=c(0,0)) + scale_color_identity() +
labs(x=NULL, y=NULL) +
theme_minimal() +theme(panel.grid=element_blank()) +
theme(axis.text.x=element_blank()) +
theme(plot.margin=margin(30,30,30,30))
print(p)
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.
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)