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)
Related
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))
require(ggplot2)
ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(vs+gear ~ cyl+am)
I would like to add the name of the 4 variables used for facet_grid on this graph. I suppose the best way to do so would be to add the name of the variables in the corners with a small arrow pointing to the row or column. I was thinking to use annotation_custom and textGrob for this purpose but failed to get anything printed on the graph.
Something like this?
ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(vs+gear ~ cyl+am,labeller = labeller(.rows = label_both, .cols = label_both))
You can also use syntax like so:
labeller = label_bquote("Gear"==.(gear))
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 am trying to display the xvar median as a dotted line & show it in the legend. Here's my code:
require(ggplot2)
require(scales)
medians_mtcars <- data.frame("wt.median"=median(mtcars$wt))
# legend shows but linetype is wrong (solid)
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point()
p <- p + geom_vline(aes(xintercept=wt.median, linetype="dotted"),
data=medians_mtcars, show_guide=TRUE)
p
I also tried:
# linetype is correct but legend does not show
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point()
p <- p + geom_vline(aes(xintercept=wt.median),
data=medians_mtcars, show_guide=TRUE, linetype="dotted")
p
Would have liked to post the plot images, but haven't crossed the reputation threshold yet.
There were 2 other posts on this forum that comes close to this topic but does not offer a solution to this problem:
Add vline to existing plot and have it appear in ggplot2 legend?
;
Incorrect linetype in legend, ggplot2 in R
I am using ggplot2 version 1.0.0
What am I doing wrong ?
Thanks in advance
If you need to show linetype in legend and also change it then inside aes() you can just write name for that linetype (as you have only one line) and then change linetype with scale_linetype_manual().
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_vline(aes(xintercept=wt.median, linetype="media"),
data=medians_mtcars, show_guide=TRUE)+
scale_linetype_manual(values="dotted")
If you really want to type linetype in aes() and also get correct legend then you should use scale_linetype_identity() with argument guide="legend".
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_vline(aes(xintercept=wt.median, linetype="dotted"),
data=medians_mtcars,show_guide=TRUE)+
scale_linetype_identity(guide="legend")