If you look at my image, i want to add some text vertically to label my "-1 to 1" facet grids in between the legend and the graph. What would i use to do this? Annotate seems to be giving me some trouble.
You can add the annotation with labs(tag=...) and position it (and the legend) with theme elements. Here is an example:
library(tidyverse)
library(grid)
ggplot(mtcars, aes(disp, mpg, color=factor(am))) + geom_point() +
facet_grid(vs ~ cyl) +
labs(tag='My text') +
#annotation_custom(grob=textGrob(label='My text', rot=-90)) +
theme(legend.box.margin=margin(l=20),
plot.tag=element_text(angle=-90),
plot.tag.position=c(.85, 0.5))
Related
I'm trying to generate a "satisfactory" legend using ggplot2, but what frustrates me is the position of ticks (they are inside the legend key). Here is some code to show what I mean.
library(ggplot2)
ggplot(mtcars, aes(x = cyl, y = gear, fill = mpg)) +
geom_tile() +
coord_equal() +
theme(legend.position = 'top')
What I get is below. The ticks are inside the legend key box.
Is there a way to put the ticks outside of the legend key box? Just like this legend (it look like they did this with ggplot2):
Any suggestion would be appreciated!!!
I am attempting to add labels (capital letters) to each plot in the following facet_grid:
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p + facet_grid(drv ~ cyl)
This outputs:
What I would like to have is this:
The major issues I am having is 1) My Y axis is scaled freely, so inputting specific coordinates for each isn't working. 2) I am not sure what keywords I should be searching here, I am sure there is a way to do this in facet_grid but I am unable to find it.
How about this? Fixing the position of label as the upper-left corner of each plot panel:
p + facet_grid(drv ~ cyl)+ annotate('text', label = LETTERS[1:12], x=min(mpg$displ), y=max(mpg$cty))
You can replace label =c('aaa','bb','fff'....), anything you like, but has to be the same number of your facet plots.
You can also fine-tune the position of the label proportional to both axis by using:
x=mean(mpg$displ)*0.6, y=max(mpg$cty)*0.97
I want to plot data using geom_label to plot some text inside rectangles. I can't find how to add transparency (alpha) to my text. In fact, transparency seems to apply only to the filling color:
ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars), alpha=cyl))+ geom_label(fill="blue")
ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars), alpha=cyl)) + geom_text()
When I use geom_text instead, the alpha parameters works as expected:
Do you know a way to make the text inside the label also transparent when using geom_label?
Thanks,
By using this brutal trick you can make it work. Just move label inside geom_text,
and make an empty long string for geom_label, this is hardly a "reproducible" solution though.
library(ggplot2)
ggplot(mtcars, aes(wt, mpg, alpha=cyl)) +
geom_label(label=" ", fill="blue") +
geom_text(aes(label = rownames(mtcars)))
Edit by #agenis: we could first compute the length of blank spaces for each label so we adapt the box to the text
ggplot(mtcars %>% mutate(blank_label = strrep(" ", nchar(rownames(.))*2)), aes(wt, mpg, alpha=cyl)) +
geom_label(aes(label=blank_label), fill="blue") +
geom_text(aes(label = rownames(mtcars)))
(the *2 is because I coulnd't get a font with fixed-width characters")
This is not a real answer, but solves a similar problem that I was having: my labels were hiding datapoints, but transparency of the labels background wasn't doing it for me, so I finally found a workaround: using the function scales::alpha to change the color aesthetic:
ggplot(mtcars) +
aes(wt, mpg, label = rownames(mtcars)) +
geom_point() +
geom_label(alpha = .5, color = alpha('black', .5))
I learned from a StackOverflow post how to insert a legend for a horizontal (or vertical) line, which is:
ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()+
geom_hline(aes(lty="foo",yintercept=20),show_guide=TRUE)+
scale_linetype_manual(name="",values=2)
That gives this plot:
However, when I specify the color of the horizontal, it will also show a separate legend for the color, as if they were different lines. See:
ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point() +
geom_hline(aes(lty="foo",yintercept=20, color = "red"),show_guide=TRUE)+
scale_linetype_manual(name="",values=2)
Finally, my question is: How can I make the legend for the color of the geom_hline disappear?
Simply following hrbrmstr's directions to get the question closed (code below). #hrbrmstr, feel free to copy/paste my response if you like.
library(ggplot2)
ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point() +
geom_hline(aes(lty="foo",yintercept=20), color = "red" ,show_guide=TRUE)+
scale_linetype_manual(name="",values=2)
I want to create a scatter plot with regression line, while using size aesthetics for one of attribute. I realized that the legend now have overlaid symbol for fitted line and I want to remove that, keeping only the legend for size. How can I do that?
> library(ggplot2)
> ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point()
This much gives this picture, which is good:
Now having smooth line on top, and then this blue "line" is what i want to get rid of, or at least make all thin like the one in the plot is.
> ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point() + stat_smooth()
Thanks!
use legend=FALSE option
ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point() + stat_smooth(legend = FALSE)
The most recent documentation {ggplot2} version 2.2.1 uses legend.show= NA
ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point() + stat_smooth(show.legend = F)