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))
Related
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))
I just discovered the newish ggplotly function that makes ggplot2 graphs into interactive plotly visualizations. This is great. But I also ran into an odd effect, possibly a bug.
If I use the log= argument to change the axis scales, the axis labels disappear. log="x" will cause the x axis label to disappear, log="y" will cause the y axis label to disappear, and log="xy" will cause both to disappear.
The same thing happens if I use scale_x_log10() and scale_y_log10() functions instead of the log argument.
Is there a workaround for this?
Example (y axis label is visible, x axis label disappears):
qplot(wt, mpg, data=mtcars, colour=factor(cyl), log="x")
ggplotly()
or
qplot(wt, mpg, data=mtcars, colour=factor(cyl)) + scale_x_log10()
ggplotly()
You can get axis names if you supply them as arguments to scale_... function.
qplot(wt, mpg, data=mtcars, colour=factor(cyl)) + scale_x_log10("wt")
ggplotly()
or maybe this?
ggplot(data = mtcars, aes(x = log10(wt), y=mpg, colour = factor(cyl))) +
geom_point() +
scale_x_continuous("wt, log10-scaling")
ggplotly()
Maybe this:
gg <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() + coord_trans(y="log10")
ggplotly(gg) %>% layout(yaxis = list(type="log", autorange=TRUE))
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'm having a similar problem as described in here under "2- After having the two legends...", but instead of increasing the point size (which eventually also enlarges the legend itself), I would like fill each box in the legend with the corresponding color. Like in a bar plot's legend. Data & code examples here.
Looking through several other questions here, the ggplot docu, etc., I tried variations of code-snippets I found, but couldn't figure out a solution. The legend always retained the point symbols.
Therefore: If possible, how to tweak or replace the legend of a point/scatter/bubble plot so that it looks like the legend of a bar plot? Or, more generally, how to replace the legend of a given geom in ggplot2 with that of a different one? Thank you for any hints!
Edit: Example with mtcars data
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(colour = factor(cyl), size = qsec))
p
Adding what I gathered from other SO-answers...
p <- p + guides(colour = guide_legend(override.aes = list(fill = unique(mtcars$cyl))))
p
...keeps the points, instead of expanding the color to fill the legend box, no matter arguments and datasources I try for guides() and list().
On the other hand:
ggplot(mtcars, aes(wt, mpg)) + geom_bar(aes(fill = factor(cyl)), stat="identity")
...draws nicely color-filled boxes to the legend. That's what I'm trying to do for a bubble plot.
You won't be able to get a fill-type legend per se, but you can easily emulate it:
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(colour = factor(cyl), size = qsec)) +
guides(col = guide_legend(override.aes = list(shape = 15, size = 10)))
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)