Adding legend to ggplot when lines were added manually - r

I have a plot in ggplot with 4 separate lines that I have added with a separate geom_line() argument. I would like to add legend but scale_colour_manual doesn't work in this case. What is the proper way to add legends when I added the variables separately?
Here's my code:
ggplot(proba[108:140,], aes(c,four)) +
geom_line(linetype=1, size=0.3) +
scale_x_continuous(breaks=seq(110,140,5)) +
theme_bw() +
theme(axis.line = element_line(colour = "black", size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(axis.text.x = element_text(angle = 0, hjust = +0.5, size=6,color="black")) +
theme(axis.text.y = element_text(angle = 0, hjust = -100, size=6, color="black")) +
theme(axis.ticks=element_line(colour="black",size=0.25)) +
xlab("\nTime-steps") +
ylab("Proportion correct\n") +
theme(axis.text=element_text(size=8),axis.title=element_text(size=8)) +
geom_line(aes(c,three), size=0.2, linetype=2) +
geom_line(aes(c,one),linetype=3, size=0.8, colour="darkgrey") +
geom_line(aes(c,two), linetype=1, size=0.8, colour="darkgrey")

Just set the color name in aes to whatever the line's name on the legend should be.
I don't have your data, but here's an example using iris a line with random y values:
library(ggplot2)
line.data <- data.frame(x=seq(0, 10, length.out=10), y=runif(10, 0, 10))
qplot(Sepal.Length, Petal.Length, color=Species, data=iris) +
geom_line(aes(x, y, color="My Line"), data=line.data)
The key thing to note is that you're creating an aesthetic mapping, but instead of mapping color to a column in a data frame, you're mapping it to a string you specify. ggplot will assign a color to that value, just as with values that come from a data frame. You could have produced the same plot as above by adding a Species column to the data frame:
line.data$Species <- "My Line"
qplot(Sepal.Length, Petal.Length, color=Species, data=iris) +
geom_line(aes(x, y), data=line.data)
Either way, if you don't like the color ggplot2 assigns, then you can specify your own using scale_color_manual:
qplot(Sepal.Length, Petal.Length, color=Species, data=iris) +
geom_line(aes(x, y, color="My Line"), data=line.data) +
scale_color_manual(values=c("setosa"="blue4", "versicolor"="red4",
"virginica"="purple4", "My Line"="gray"))
Another alternative is to just directly label the lines, or to make the purpose of the lines obvious from the context. Really, the best option depends on your specific circumstances.

Related

ggplot legend without title leaves space for title

So I have a ggplot that doesn't require a legend because it actually has a title and thus doesn't need a legend that would simply repeat the title.
Imagine something like this:
ggplot(iris)+
geom_point(aes(x=Sepal.Length, y=Sepal.Width, color=Species))+
theme(legend.box.background=element_rect(fill="white", color="black"))+
labs(color="")+
ggtitle("Sepals ~ Species")+
xlab("Length")+
ylab("Width")
(ignore the fact that the legend in my reprex only has two lines drawn for the box)
Do you notice the graphical problem? Apparently ggplot "thinks" there is a legend title and leaves some space, so I though using element_blank for the legend title might work.
ggplot(iris)+
geom_point(aes(x=Sepal.Length, y=Sepal.Width, color=Species))+
labs(color=element_blank())+
theme(legend.box.background=element_rect(fill=NA, color="black"),
legend.margin=margin(t=0,r=0,b=0,l=0))+
ggtitle("Sepals ~ Species")+
xlab("Length")+
ylab("Width")
While this improves the situation by making the box smaller at the top, it does not fix the problem because the top space is still smaller. As I have manually set the legend margins to 0 this can't be the issue.
any ideas?
You can set theme(legend.title = element_blank()) . This also means you don't need to set an empty string for the label.
To show this, let's make that box outline a little thicker, and use the "empty string" method:
ggplot(iris) +
geom_point(aes(Sepal.Length, Sepal.Width, color = Species)) +
ggtitle("Sepals ~ Species") +
labs(x = "Length", y = "Width", color = "") +
theme(legend.box.background = element_rect(color ="black", size = 2))
We can see that there is an obvious space where the title should be.
But now let's try it with the element_blank() method:
ggplot(iris) +
geom_point(aes(Sepal.Length, Sepal.Width, color = Species)) +
ggtitle("Sepals ~ Species") +
labs(x = "Length", y = "Width") +
theme(legend.box.background = element_rect(color ="black", size = 2),
legend.title = element_blank())
As Tjebo points out, the other option is to use NULL instead of an empty string, which does the same thing as theme(legend.title = element_blank())
ggplot(iris) +
geom_point(aes(Sepal.Length, Sepal.Width, color = Species)) +
ggtitle("Sepals ~ Species") +
labs(x = "Length", y = "Width", color = NULL) +
theme(legend.box.background = element_rect(color ="black", size = 2))
You additionally need to change legend.spacing. Very related: Reduce padding in ggplot2 legend
By the way, margin() has as defaults all = 0, so you don't need to type them out... ;)
library(ggplot2)
ggplot(iris)+
geom_point(aes(x=Sepal.Length, y=Sepal.Width, color=Species))+
labs(color=NULL) +
theme(legend.box.background=element_rect(fill="white", color="black"),
legend.margin=margin(),
legend.spacing.y = unit(0, "mm"))
Created on 2022-05-31 by the reprex package (v2.0.1)

Can't add legend into my graph on ggplot, Why is this the case?

I want to add a legend it shows black for the 7 day moving average and blue for the bars(daily cases). so it looks something similar to the NHS graph , but the legend does not work when i add it into my code?
ggplot(LimerickNew1, aes(x=TimeStamp, y=DailyCCase,Shape=MA7)) +
geom_bar(stat="identity",fill="steelblue") +
geom_line(aes(y=MA7),size=1.5) +
labs( x="", y="Total cases", title="Total Covid Cases for County Limerick 01-03-20 to 01-
06-`20" )+`
theme_bw()+
theme(legend.background = element_rect(fill = "darkgray"),
legend.key = element_rect(fill = "lightblue", color = NA),
legend.key.size = unit(1.5, "cm"),
legend.key.width = unit(0.5,"cm"),
axis.text.x = element_text(face="bold", color="#008000",size=12, angle=0),
axis.text.y = element_text(face="bold", color="#008000",size=12, angle=0),
axis.title.x = element_text(face="bold", size=12, color="black"),
plot.title = element_text( face = "bold", colour = "black", size = 20,, hjust = 0.5))
Without a reproducible example, it's hard to give a more specific answer for your question, but here's a method that expands upon the comment from #teunbrand and should guide you toward showing a legend.
TL;DR - The reason you're not seeing a legend is because you do not have any layers or geoms drawn that have one of the aesthetics mapped to aes(). All aesthetics like color, size, and fill are specified for each geom outside of an aes() function, which means there's no reason for ggplot2 to draw a legend. To get a legend to be drawn, you need to specify the aesthetic inside aes().
Example
Here's an illustrative example that is similar to OP's question.
library(ggplot2)
set.seed(8675309)
df <- data.frame(
x = 1:100,
y = rnorm(100, 10))
df$z <- log(cumsum(df$y))
ggplot(df, aes(x=x)) +
geom_col(aes(y=y), fill='blue', alpha=0.3) +
geom_line(aes(y=z), size=2) +
ylim(0,25) + theme_bw()
Showing a Legend
To get a legend to show, you have to specify one or more aesthetics inside aes(). The fun fact here is that you don't have to specify a column in the dataset when doing this. If you specify a single value, it will apply to the whole dataset, and basically just show a legend key for the entire set of observations. This is what we want.
ggplot(df, aes(x=x)) +
geom_col(aes(y=y, fill="the columns"), alpha=0.3) +
geom_line(aes(y=z, size="the line"), color="black") +
ylim(0,25) + theme_bw()
Formatting
The names come from the values specified in aes(), and the color change is due to default mapping of color values. To get things to look "right", you want to use some theme() elements to:
keep only one name
squish legends together
position on the chart, rather than on the side
The end result is here:
ggplot(df, aes(x=x)) +
geom_col(aes(y=y, fill="the columns"), alpha=0.3) +
geom_line(aes(y=z, size="the line"), color="black") +
ylim(0,25) +
scale_fill_manual(values="blue") + # recolor the columns
guides(
fill=guide_legend(title="My Legend", order=1),
size=guide_legend(title=NULL, order=2)
) +
theme_bw() +
# legend placement and spacing
theme(
legend.title = element_text(margin=margin(b=10)),
legend.spacing.y = unit(-3,'pt'),
legend.position = c(0.8, 0.8)
)

In ggplot2 how can I scale the legend when using two graph types?

I'm using ggplot2 with both + geom_line() + geom_point(). I have the colors/shapes worked out, but I can't scale the legend appropriately. If I do nothing it's tiny, and if I enlarge it, the color blocks the shape.
For example:
You can see that the shapes and colors are both in the legend, but the shapes are being drawn over by the colors. I would like to have shapes of the appropriate color drawn in the legend, but can't figure out how to do it.
My plot is being drown as follows:
ggplot(data=melted, aes(x=gene, y=value, colour=variable, shape=variable, group = variable, stroke=3, reorder(gene, value)))
+ theme_solarized()
+ scale_colour_solarized("blue")
+ geom_line()
+ geom_point()
+ theme(axis.text.x = element_text(angle = 90, hjust = 1), plot.title = element_text(size=16, face="bold"), legend.title=element_blank(), legend.text=element_text(size=20))
+ ggtitle('Signiture Profiles')
+ labs(x="Gene", y=expression(paste("Expression"), title="Expression"))
+ scale_colour_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c("#ff420e","#89da59","#89da59","#89da59","#376467","#376467","#376467","#00293c","#00293c","#00293c"))
+ scale_shape_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c(0,1,2,3,1,2,3,1,2,3))
+ guides(colour = guide_legend(override.aes = list(size=12)))
Here is some example data as requested:Example Data
Thanks in advance for any help you can provide.
You could perhaps rethink how you are differentiating your variables.
You could do something like the following. Note the changes in the first line, where I have separated the component parts of variable rather than setting colours and shapes via your scale statements. (I haven't got your theme, so I left that out).
ggplot(data=melted, aes(x=gene,
y=value,
colour=gsub("_.*","",variable),
shape=gsub(".*_","",variable),
group = variable,
stroke=3,
reorder(gene, value))) +
geom_line() +
geom_point() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
plot.title = element_text(size=16, face="bold"),
legend.title=element_blank(),
legend.text=element_text(size=20)) +
ggtitle('Signiture Profiles') +
labs(x="Gene", y=expression(paste("Expression"), title="Expression")) +
guides(shape = guide_legend(override.aes = list(size=5)),
colour = guide_legend(override.aes = list(size=5)))

Add secondary axis ggplot - line chart with two variables - one data frame

I used ggplot to make a like graph with two variables, but I need to add a secondary y-axis and assign it to one of the variables ("volt").
I also would like to specify the range of the secondary y-axis (upper and lower limit), as well as the breaks - as I did for the y-main-axis.
My two variables are "Sr" and "volt".
I don't want to use different dataframes and then merge the graphs.
Do any of you know how to do it?
Oh, I must add that I am an absolute beginner!
Thanks,
Pedro
ggplot(data = k, aes(x = Dist)) +
geom_line(aes(y = Sr), colour="blue") +
geom_line(aes(y = volt), colour = "grey") +
xlab(bquote('Distance-um')) +
ylab(bquote('Sr87Sr86')) +
geom_point(aes(y = Sr), colour="black", size=2) +
geom_point(aes(y = volt), colour="grey", size=2) +
theme(axis.title.x = element_text(colour="black",size=10,face="bold"),
axis.title.y = element_text(colour="black",size=10,face="bold"),
axis.text.x = element_text(colour="black",size=8, face="plain"),
axis.text.y = element_text(colour="black",size=8, face="plain")) +
theme(panel.background = element_rect(fill = "white")) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_rect(colour = "black", fill="transparent")) +
theme(plot.title = element_text(lineheight=.8, size=10, face="bold")) +
geom_hline(aes(yintercept=0.7061), colour="black", linetype="dotted") +
geom_hline(aes(yintercept=0.7078), colour="black", linetype="dotted") +
geom_hline(aes(yintercept=0.70467), colour="black", linetype="dotted") +
scale_x_continuous(limits=c(-0.01, 1000), breaks=c(0, 250, 500, 750, 1000))+
scale_y_continuous(limits=c(0.7039, 0.7101), breaks=c(0.7040, 0.7050,
0.7060, 0.7070, 0.7080, 0.7090)) +
theme(plot.margin = unit(c(.25,.25,.0,.0), "cm"))
First, I would like to mention that two axis is not the best idea.
Having said that, if you still want two axis, you have to scale one of your variables (volt in this case).
Dist<-seq(1,10)
Sr<-c(0.704, 0.705, 0.706, 0.707, 0.708, 0.704, 0.705, 0.706, 0.707, 0.708)
volt<-c(3,5,10,8,12,4,11,3,14,22)
k<-data.frame(Dist,Sr,volt)
k$volt<-k$volt/10
Now, fixing the data makes things easier for plotting, just melt your variables
library(reshape)
k_melt<-melt(k,id="Dist")
And plotting. With sec_axis you can create the second axis and rescale again the values
ggplot(k_melt, aes(x=Dist, y=value, fill=variable, colour=variable))+
geom_line(stat='identity', size=0.5)+
geom_point(stat='identity', size=2)+scale_color_manual(values=c("blue", "grey")) +
scale_y_continuous("SR", sec.axis = sec_axis(~ . *10, name = "Volt"))`
NOTE: You can add your theme and geom_hline to this code. they don't work for the simulated data I created

ggplot classic theme missing axes [duplicate]

I am trying to draw this following graph using ggplot2 package, but somehow the axis won't show up. the ticks are there, just not the axis line. I have used the theme(axis.line=element_line()) function, but it wouldn't work.
Here is my code:
library(ggplot2)
ggplot(data = soepl_randsub, aes(x = year, y =satisf_org, group = id)) +
geom_point() + geom_line() +ylab("Current Life Satisfaction") +theme_bw() +
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() ) +
theme(panel.border= element_blank()) +
theme(axis.line = element_line(color="black", size = "2"))
I am not sure what went wrong. Here is the chart.
The bug was fixed in ggplot2 v2.2.0 There is no longer a need to specify axis lines separately.
I think this is a bug in ggplot2 v2.1.0. (See this bug report and this one.) A workaround is to set the x-axis and y-axis lines separately.
library(ggplot2)
ggplot(data = mpg, aes(x = hwy, y = displ)) +
geom_point() +
theme_bw() +
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() )+
theme(panel.border= element_blank())+
theme(axis.line.x = element_line(color="black", size = 2),
axis.line.y = element_line(color="black", size = 2))
You don't need to specify axis-size for X and Y separately. When you are specifying size="2", R is considering value 2 as non-numeric argument. Hence, axis-line parameter is defaulted to 0 size. Use this line of code:
ggplot(data = mpg, aes(x = hwy, y = displ)) + geom_point() +xlab("Date")+ylab("Value of Home")+theme_bw() +theme(plot.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank()) + theme(panel.border= element_blank()) +
theme(axis.line = element_line(color="black", size = 2))
axis_line inherits from line in R, hence specifying size is mandatory for non-default values.

Resources