I have a very simple problem that I am struggling with, namely changing the legends of plot using geom_smooth in ggplot2.
Here is my code:
p1<- mtcars$group <- factor(mtcars$vs)
ggplot(mtcars, aes(x=mpg, y=disp, group=group)) +
geom_smooth(method=lm, se=FALSE,fullrange=TRUE, show.legend=TRUE,aes(linetype=group), colour="black")
p1
result of p1
What I would like to do, is change the labels: i.e.: from "group" to "legend" and from "0" to "Experiment" and "1" to "Control". I tried to do this by addeding the labs argument and using scale_fill_discrete:
p2<- ggplot(mtcars, aes(x=mpg, y=disp, group=group)) +
geom_smooth(method=lm, se=FALSE,fullrange=TRUE, show.legend=TRUE,aes(linetype=group), colour="black")+
labs(linetype="Legend")+
scale_fill_discrete(labels=c("Experiment", "Control"))
p2
The result changes the legend title, (p2) but still does not change the labels. Any ideas?
EDIT:
This solves the problem, thanks for the quick replies:
ggplot(mtcars, aes(x=mpg, y=disp, group=group)) +
geom_smooth(method=lm, se=FALSE,fullrange=TRUE, show.legend=TRUE,aes(linetype=group), colour="black")+
labs(linetype="Legend")+
scale_linetype_discrete(labels=c("Experiment", "Control"))
My mistake was using scale_fill_discrete instead of scale_linetype_discrete.
Do you get the error if you run the code like this?
ggplot(mtcars, aes(x=mpg, y=disp, group=group)) +
geom_smooth(method=lm, se=FALSE,fullrange=TRUE, show.legend=TRUE,aes(linetype=group), colour="black")+
labs(linetype="Legend")+
scale_linetype_discrete(labels=c("Experiment", "Control"))
I get this plot:
Related
I have attached multiple plots to one page using grid.arrange.
Is there a way to label each plot with "(a)","(b)" etc...
I have tried using geom_text but it does not seem compatible with my plots....
.... as you can see, geom_text has some strange interaction with my legend symbols.
I will show an example using the mtcars data of what I am trying to achieve. THe alternative to geom_text I have found is "annotate" which does not interact with my legend symbols. However, it is not easy to label only one facet....
q1=ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(a)",size=8,family="serif")
q2=ggplot(mtcars, aes(x=mpg, y=wt,)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(b)",size=8,family="serif")
geom_text(x=15, y=5,size=8, label="(b)")
gt1 <- ggplotGrob(q1)
gt2 <- ggplotGrob(q2)
grid.arrange(gt1,gt2, ncol=1)
Therefore, my question is, is there a way to label plots arranged using grid.arrange, so that the first facet in each plot is labelled with either a, or b or c etc...?
You can use ggarrange from ggpubr package and set labels for each plot using the argument labels:
library(ggplot2)
library(ggpubr)
q1=ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(a)",size=8,family="serif")
q2=ggplot(mtcars, aes(x=mpg, y=wt,)) +
geom_line() +
geom_point()+
facet_grid(~cyl)+
annotate(geom="text", x=15, y=12, label="(b)",size=8,family="serif")
ggarrange(q1,q2, ncol = 1, labels = c("a)","b)"))
Is it what you are looking for ?
If you set inherit.aes=FALSE, you can prevent it from interring:
ggplot(mtcars, aes(x=mpg, y=wt,col=factor(cyl))) +
geom_line() +
geom_point()+
geom_text(inherit.aes=FALSE,aes(x=15,y=12,label="(a)"),
size=8,family="serif")+
facet_grid(~cyl)
If you want to only label the first facet (hope I got you correct), I think the easiest way to specify a data frame, e.g if we want only something in the first,
#place it in the first
lvl_data = data.frame(
x=15,y=12,label="(a)",
cyl=levels(factor(mtcars$cyl))[1]
)
ggplot(mtcars, aes(x=mpg, y=wt,col=factor(cyl))) +
geom_line() +
geom_point()+
geom_text(data=lvl_data,inherit.aes=FALSE,
aes(x=x,y=y,label=label),size=8,family="serif")+
facet_grid(~cyl)
I want to have a horizontal bar chart, but without legend. When I run the script below without coord_flip(), no legend shows. But when I run it with the coord_flip() argument, the legend appaers. I think this might be a bug. Does anyone know a different code with which I can skip the legend?
df <- aggregate(formula=mpg~cyl, data=mtcars, FUN=sum)
fig <- ggplot(df, aes(x=cyl, y=mpg, fill=cyl, label=mpg)) +
geom_col() +
coord_flip() +
theme(legend.position="none") +
theme_bw()
fig
I am trying to replicate the example here (sthda.com) using the following code:
# Change point shapes and colors manually
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
The example on that web page says that code should produce the following result:
But when I run it in R, I get the following error:
"Error: Continuous value supplied to discrete scale"
Does anyone know what could be wrong with this code? Or why I am getting a different result than the example?
If someone could run the sample code and tell me if they get the same error I would be very grateful.
Yeah, I was able to fix it by converting the color and shape aesthetics to factors:
ggplot(mtcars, aes(x=wt, y=mpg, color=as.factor(cyl), shape=as.factor(cyl))) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
as.factor makes it work
ggplot(mtcars, aes(x=wt, y=mpg, color=as.factor(cyl), shape=as.factor(cyl))) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
until now I can't find an appropriate answer, here is my short question about ggplot2 in R:
data(mtcars)
ggplot(data=mtcars, aes(x=mpg, y=wt, fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue"))+
geom_point(size=2, pch=21)+
facet_grid(.~cyl)
This is all fine, now I want all data points (regardless what number cyl has) in every facet (e.g. with a smooth grey below the points)?
Thanks, Michael
Here is a slight simplification that makes life a bit easier. The only thing you need to do is to remove the faceting variable from the data provided to the background geom_point() layer.
library(tidyverse)
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
geom_point(data=select(mtcars,-cyl), colour="grey") +
geom_point(size=2, pch=21, aes(fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue")) +
facet_wrap(~cyl)
Using the link given by #beetroot, I was able to do something like this :
g1 <- ggplot(data=mtcars, aes(x=mpg, y=wt)) +
geom_point(data=mtcars[, c("mpg", "wt")], aes(x=mpg, y=wt), colour="grey") +
geom_point(size=2, pch=21, aes(fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue")) +
facet_wrap(~cyl)
This produces the plot :
Hope this helps you.
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")