I can't change the fontsize in a direct.label (from the directlabels package) ggplot2 plot. See below for a reproducible example - there's no problem in rotating the labels 45 degrees, making them bold, serif, and 50% transparent (all the other arguments in the list at the end of the code below) - but I can't control the fontsize. (I don't really want them to be 25, this is just for testing....)
Is there something I'm missing, or is this a bug?
library(ggplot2)
library(scales)
library(directlabels)
df <- data.frame(x = rnorm(26), y=rnorm(26), let=letters)
p <- ggplot(df, aes(x, y, color=let)) + geom_point()
direct.label(p,
list("top.points", rot=45, fontsize=25,
fontface="bold", fontfamily="serif", alpha=0.5))
I figured it out, you use cex to change the font size.
df <- data.frame(x = rnorm(26), y=rnorm(26), let=letters)
p <- ggplot(df, aes(x, y, color=let)) + geom_point()
direct.label(p,
list("top.points", rot=45, cex=6,
fontface="bold", fontfamily="serif", alpha=0.5))
That would give you,
It's kind of a different route, but would you consider doing it all in ggplot2?
ggplot(df, aes(x, y, color=let)) +
geom_point() +
geom_text(df, mapping=aes(x, y, label=let, colour=let),
size=5, vjust=-.55, hjust=.55, angle = 45, fontface="bold",
family ="serif", alpha=0.5) + opts(legend.position = "none")
This would give you this, and you can adjust the fontsize using size
Related
Here I have 2-dim numeric array dataset and numeric 1-dim array of labels clustring. Then I plot it with the following code:
s = data.frame(x = dataset[,1], y = dataset[,2])
p = ggplot(s, aes(x, y))
p + geom_point(aes(colour = factor(clustering)))
which displays beautiful picture:
Now I want to remove legend completely, so here I've found possible solution:
# Remove legend for a particular aesthetic (fill)
p + guides(fill=FALSE)
# It can also be done when specifying the scale
p + scale_fill_discrete(guide=FALSE)
# This removes all legends
p + theme(legend.position="none")
but none of such commands wont help. It shows empty plot instead:
So how do I remove the legend from my plot?
Try this:
library(ggplot2)
s = data.frame(x = rnorm(20), y = rnorm(20), clustering = rep(c(1, 2), 10))
p <- ggplot(s, aes(x, y))+
guides(fill=FALSE)+
geom_point(aes(colour = factor(clustering)))+
scale_fill_discrete(guide=FALSE)+
theme(legend.position="none")
p
In your code, you are not saving the plot again after each time you add something to it. You can fix this by changing the lines that add to the plot:
# Remove legend for a particular aesthetic (fill)
p = p + guides(fill=FALSE)
But the way I wrote is is more common R formatting.
Use show.legend = FALSE within geom_point. Here is an example using ggplot2's diamonds dataset.
s <- diamonds
p <- ggplot(data = s, aes(x = depth, y = price))
p + geom_point(aes(colour = factor(cut)), show.legend = FALSE)
Just try this:
p + geom_point(aes(colour = factor(clustering)),show.legend=FALSE)
I'm using ggplot2 with a GAM smooth to look at the relationship between two variables. When plotting I'd like to remove the grey area behind the symbol for the two types of variables. For that I would use theme(legend.key = element_blank()), but that doesn't seem to work when using a smooth.
Can anyone tell me how to remove the grey area behind the two black lines in the legend?
I have a MWE below.
library(ggplot2)
len <- 10000
x <- seq(0, len-1)
df <- as.data.frame(x)
df$y <- 1 - df$x*(1/len)
df$y <- df$y + rnorm(len,sd=0.1)
df$type <- 'method 1'
df$type[df$y>0.5] <- 'method 2'
p <- ggplot(df, aes(x=x, y=y)) + stat_smooth(aes(lty=type), col="black", method = "auto", size=1, se=TRUE)
p <- p + theme_classic()
p <- p + theme(legend.title=element_blank())
p <- p + theme(legend.key = element_blank()) # <--- this doesn't work?
p
Here is a very hacky workaround, based on the notion that if you map things to aestethics in ggplot, they appear in the legend. geom_smooth has a fill aesthetic which allows for different colourings of different groups if one so desires. If it's hard to fix that downstream, sometimes it's easier to keep those unwanted items out of the legend altogether. In your case, the color of the se appeared in the legend. As such, I've created two geom_smooths. One without a line color (but grouped by type) to create the plotted se's, and one with linetype mapped to aes but se set to false.
p <- ggplot(df, aes(x=x, y=y)) +
#first smooth; se only
stat_smooth(aes(group=type),col=NA, method = "auto", size=1, se=TRUE)+
#second smooth: line only
stat_smooth(aes(lty=type),col="black", method = "auto", size=1, se=F) +
theme_classic() +
theme(
legend.title = element_blank(),
legend.key = element_rect(fill = NA, color = NA)) #thank you #alko989
I'm new to ggplot2 and relatively new to R. I can make a picture appear on a plot, and I can make the y axis reverse scale, but I don't know how to do both at once. For example:
library(ggplot2)
y=c(1,2,3)
x=c(0,0,0)
d=data.frame(x=x, y=y)
#following http://stackoverflow.com/questions/9917049/inserting-an-image-to-ggplot2/9917684#9917684
library(png)
library(grid)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)
#these work fine - either reversing scale, or adding custom annotation
ggplot(d, aes(x, y)) + geom_point()
ggplot(d, aes(x, y)) + geom_point() + scale_y_reverse()
ggplot(d, aes(x, y)) + geom_point() + annotation_custom(g, xmin=.23, xmax=.27, ymin=1.8, ymax=2.2)
#these don't...combining both reverse scale and custom annotation
ggplot(d, aes(x, y)) + geom_point() + annotation_custom(g, xmin=.23, xmax=.27, ymin=1.8, ymax=2.2) + scale_y_reverse()
ggplot(d, aes(x, y)) + geom_point() + annotation_custom(g, xmin=.23, xmax=.27, ymin=2.2, ymax=1.8) + scale_y_reverse()
I'm sure I'm missing something pretty basic. Where should I start looking both to get my little graphic to display on a reverse scale plot, and also to understand things better under the hood?
CLARIFICATION IN RESPONSE TO COMMENTS:
The example above is me trying to simplify the problem I'm having. I don't know if it matters, but I'm not merely trying to overlay some data on a static image. I actually want to place an image in a certain spot on a plot, based on the data in the plot. However, I can't seem to do that when the axis scale is reversed. And, as it turns out, I can't even put an image in an absolute position when the scale is reversed either, so that's the code example I posted.
With scale_y_reverse, you need to set the y coordinates inside annotation_custom to their negative.
library(ggplot2)
y=c(1,2,3)
x=c(0,0,0)
d=data.frame(x=x, y=y)
library(png)
library(grid)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)
ggplot(d, aes(x, y)) + geom_point() +
annotation_custom(g, xmin=.20, xmax=.30, ymin=-2.2, ymax=-1.7) +
scale_y_reverse()
Why negative? The y coordinates are the negative of the original. Check out this:
(p = ggplot(d, aes(x=x, y=y)) + geom_point() + scale_y_reverse())
y.axis.limits = ggplot_build(p)$layout$panel_params[[1]][["y.range"]]
y.axis.limits
OR, set the coordinates and size of the grob in relative units inside rasterGrob.
g <- rasterGrob(img, x = .75, y = .5, height = .1, width = .2, interpolate=TRUE)
ggplot(d, aes(x, y)) + geom_point() +
annotation_custom(g) +
scale_y_reverse()
I have a plot with three different lines. I want one of those lines to have points on as well. I also want the two lines without points to be thicker than the one without points. I have managed to get the plot I want, but I the legend isn't keeping up.
library(ggplot2)
y <- c(1:10, 2:11, 3:12)
x <- c(1:10, 1:10, 1:10)
testnames <- c(rep('mod1', 10), rep('mod2', 10), rep('meas', 10))
df <- data.frame(testnames, y, x)
ggplot(data=df, aes(x=x, y=y, colour=testnames)) +
geom_line(aes(size=testnames)) +
scale_size_manual("", values=c(0.5,1,1)) +
geom_point(aes(alpha=testnames), size=5, shape=4) +
scale_alpha_manual("", values=c(1, 0, 0))
I can remove the second (black) legend:
ggplot(data = df, aes(x=x, y=y, colour=testnames)) +
geom_line(aes(size=testnames)) +
scale_size_manual("", values=c(0.5,1,1), guide='none') +
geom_point(aes(alpha=testnames), size=5, shape=4) +
scale_alpha_manual("", values=c(1, 0.05, 0.05), guide='none')
But what I really want is a merge of the two legends - a legend with colours, cross only on the first variable (meas) and the lines of mod1 and mod2 thicker than the first line. I have tried guide and override, but with little luck.
You don't need transparency to hide the shapes for mod1 and mod2. You can omit these points from the plot and legend by setting their shape to NA in scale_shape_manual:
ggplot(data = df, aes(x = x, y = y, colour = testnames, size = testnames)) +
geom_line() +
geom_point(aes(shape = testnames), size = 5) +
scale_size_manual(values=c(0.5, 2, 2)) +
scale_shape_manual(values=c(8, NA, NA))
This gives the following plot:
NOTE: I used some more distinct values in the size-scale and another shape in order to better illustrate the effect.
i'm looking for some help in creating a faceted plot with angeled x-axis tick labels, which is probably best explained by the following example:
require(ggplot2)
df <- data.frame(group=factor(c('sex','sex','race','race')), variable=c('Female','Male','White','African American'), value=1:4)
p <- ggplot(aes(x=variable, y=value), data=df)
p <- p + geom_line()
p <- p + facet_grid(. ~ group, scale="free")
p <- p + opts(axis.text.x=theme_text(angle=45,hjust=1,vjust=1))
ggsave(p, file='faceted.pdf', width=6, height=4)
which produces this figure where the x-ticks on the right are misaligned:
it seems that the problem is introduced when using scale="free" in the facet_grid and is related to the varying tick label length.
any suggestions are greatly appreciated.
Looks like a bug: https://github.com/hadley/ggplot2/issues/221 that has apparently been fixed in ggplot2 0.9 and later.
> help(package='ggplot2')
Information on package ‘ggplot2’
Description:
Package: ggplot2
Type: Package
Title: An implementation of the Grammar of Graphics
Version: 0.8.9
(Too bad for me!)
Bug reproducible with:
qplot(reorder(model, hwy), hwy, data=mpg) +
facet_grid(. ~ manufacturer, scales="free") +
opts(axis.text.x = theme_text(angle=90))
If you really got just a few x-labels - who needs ticks mate? Suppress them with axis.ticks and get rid of horizontal/vertical justification.
require(ggplot2)
df <- data.frame(group=factor(c('sex','sex','race','race')),
variable=c('Female','Male','White','African American'),
value=1:4)
p <- ggplot(df,aes(x=variable, y=value)) + geom_line()
p <- p + facet_grid(. ~ group, scale="free")
p <- p + opts(axis.text.x=theme_text(angle=45),
axis.ticks = theme_blank(),axis.title.y=theme_blank())
ggsave(p, file='no_ticks.png', width=6, height=4)
A bit late (i.e. 9 years after the last answer ;) but still interesting, here is another way to define the angle of axes labels (here: 90° rotation of x-axis labels):
ggplot(df,aes(x=variable, y=value)) + geom_line() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
(where vjust and hjust are vertical and horizontal alignment respectively)